0

I have the following variables:

min.v<-5
max.v<-10

and i want to message the following

Test this. You entered "5 10" 

Is this possible to print this with message() or paste(), since both functions regard quotes as strings. The variables in the message should be inside double quotes

I have tried message(as.character(paste(min.v, " ",max.v))) but the double quotes are ignored.

This question is probably the exact opposite of this Solve the Double qoutes within double quotes issue in R

Community
  • 1
  • 1
ECII
  • 10,297
  • 18
  • 80
  • 121
  • 1
    It isn't so much that `paste` and `message` "regard quotes as strings" as it is that the R interpreter regards quotes as specifying object names. – IRTFM Jun 24 '12 at 17:11

3 Answers3

8

You have two three options of doing this

Option 1: Escape the quotes. To do this, you have to use \".

cat("You entered ", "\"", min.v, " ", max.v,"\"", sep="")
You entered "5 10"

Option 2: Embed your double quotes in single quotes:

cat("You entered ", '"', min.v, " ", max.v,'"', sep="")
You entered "5 10"

Edit: with acknowledgement to @baptiste, in an effort to make this answer comprehensive

Option3: Use the function dQuote():

options(useFancyQuotes=FALSE)
cat("You entered ", dQuote(paste(min.v, max.v)), sep="")
You entered "5 10"
Andrie
  • 176,377
  • 47
  • 447
  • 496
4
x = 5; y = "indeed"
message("you entered ", dQuote(x))
message("you entered ", dQuote(paste(x, y)))
baptiste
  • 75,767
  • 19
  • 198
  • 294
0

Although sprintf can get quite complex, I find the code is generally neater than most other options. Essentially, you have a message that you want to format where you want to insert variable values -- this is what sprintf is for:

min.v <- 5
max.v <- 10
msg <- 'Test this. You entered "%i %i"\n'
str <- sprintf(msg, min.v, max.v) #generates string
cat(str) #to output
# or message(str)

The %i are placeholders that expect integer values, see ?sprintf for more details. Although this solution still relies on surrounding double quotes with single quotes, you get much more readable code than when using paste or cat directly.

ssokolen
  • 468
  • 3
  • 13