1

I come from a python background but I'm new to R (need to learn it in a hurry), stuck at an issue:

I am developing a function to print out the variables passed into my function, kinda simple.

hello_world <- function(fn, num) {
    print("The stuff you sent in were "$fn" and "$num")
} 

so if I call hello world as such:

hello_world("foo", 3:5)

I should see

The stuff you sent in were "foo" and "3:5"

I tried print("$foo", "$num")...still having issues. How can I do this without using sprint?

1
  • Remove the $, from fn and num and use paste i.e. paste("The stuff you sent in were", fn, ' and ', num) Commented Jun 3, 2022 at 18:57

4 Answers 4

1

We could use deparse/substitute to get the input as it is from 'num', and then create a single string with paste

hello_world <- function(fn, num) {
      num <- deparse(substitute(num))
      cat(paste0("The stuff you sent in were ", '"', fn, '"',
              " and ", '"', num, '"'), "\n")
                    }

-testing

> hello_world("foo", 3:5)
The stuff you sent in were "foo" and "3:5" 
Sign up to request clarification or add additional context in comments.

Comments

1

Use paste to assemble the string to be printed. And note that since 3:5 is a vector, the first paste uses argument collapse.
I have also included 3 different ways of printing.

hello_world <- function(fn, num) {
  num_string <- paste(num, collapse = ",")
  msg <- paste("The stuff you sent in were", dQuote(fn), "and", dQuote(num_string))
  # 3 ways of printing
  print(msg)
  cat(msg, "\n")    # needs the newline character
  message(msg)      # this will default to printing in red
} 
hello_world("foo", 3:5)
#> [1] "The stuff you sent in were “foo” and “3,4,5”"
#> The stuff you sent in were “foo” and “3,4,5”
#> The stuff you sent in were “foo” and “3,4,5”

Created on 2022-06-03 by the reprex package (v2.0.1)

Comments

1

Just use cat instead of print ,

hello_world <- function(fn, num) {
  cat("The stuff you sent in were \"" , fn , "\" and \"" , num , "\"" , sep = "")
} 

hello_world("foo", 3:5)
#> The stuff you sent in were "foo" and "345"

hello_world("foo", "3:5")
#> The stuff you sent in were "foo" and "3:5"

Created on 2022-06-03 by the reprex package (v2.0.1)

Comments

0

Here's an option where you don't have to specify your parameters:

library(purrr)

hello_world <- function(fn, num) {
  params <- as.character(match.call()[-1])
  out <- sub(paste(map_chr(params, ~paste("`", ., "`",  " and")), collapse = ' '), pattern = " [[:alpha:]]*$", replacement = "")
  paste0('The stuff you sent in were ', out)
} 

hello_world('foo', 3:5)
#> [1] "The stuff you sent in were ` foo `  and ` 3:5 ` "

So you could extend this to something like:

library(purrr)

hello_world <- function(fn, num, apple, banana, cheeseburger) {
  params <- as.character(match.call()[-1])
  out <- sub(paste(map_chr(params, ~paste("`", ., "`",  " and")), collapse = ' '), pattern = " [[:alpha:]]*$", replacement = "")
  paste0('The stuff you sent in were ', out)
} 

hello_world('foo', 3:5, 'yes', 'no', 'definitely')
#> [1] "The stuff you sent in were ` foo `  and ` 3:5 `  and ` yes `  and ` no `  and ` definitely ` "

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.