3

I would like to know if it is possible to pass a variable inside a string with R like in php. For example:

args <- commandArgs(TRUE)
variable1 <- args[1]

variable2 <- "I am argument:",variable1,"text continue"
3
  • 2
    I'd use sprintf. Commented Feb 17, 2020 at 9:42
  • I tried with sprintf but with no success. I mean I didn't get what I wanted. variable2 <- sprintf("I am a string %s %s",variable1,"text continue") Commented Feb 17, 2020 at 9:58
  • sprintf("I am a string %s text continue", variable1) Commented Feb 17, 2020 at 10:09

2 Answers 2

7
variable2 <- paste0("I am argument:",variable1,"text continue")
Sign up to request clarification or add additional context in comments.

Comments

1

There are variety of ways to get the output :

variable1 <- args[1]

1) Using sprintf as mentioned by @Roland

sprintf("I am argument: %s text continue",variable1) 

2) Using glue

glue::glue("I am argument: {variable1} text continue")

3) a) str_c

stringr::str_c("I am argument:",variable1,"text continue")

b) stri_c

stringi::stri_c("I am argument:",variable1,"text continue")

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.