r - Send a text string containing double quotes to function -


i'm having problem using double quotes while formatting text strings being sent functions in r.

consider example function code:

foo <- function( numarg = 5, textarg = "** default text **" ){      print (textarg)     val <- numarg^2 + numarg     return(val)  } 

when running following input:

foo( 4, "learning r fun!" ) 

the output is:

[1] "learning r fun!" [1] 20 

but when try (in various ways, suggested here) write "r" instead of r, following outputs:

> foo( 4, "learning r fun!" ) [1] "learning r fun!" [1] 20 > foo( 4, "learning "r" fun!" ) error: unexpected symbol in "funfun( 4, "learning "r" > foo( 4, "learning \"r\" fun!" ) [1] "learning \"r\" fun!" [1] 20 > foo( 4, 'learning "r" fun!' ) [1] "learning \"r\" fun!" [1] 20 

using as.character(...) or dquote(...) suggested here seems break function because of different number of arguments.

you can try these approaches:

foo <- function(numarg = 5, textarg = "** default text **" ){      cat(c(textarg, "\n"))      val <- (numarg^2) + numarg     return(val)  }  foo <- function(numarg = 5, textarg = "** default text **" ){      print(noquote(textarg))      val <- (numarg^2) + numarg     return(val)  }  foo( 4, "learning r fun!" ) foo( 4, 'learning "r" fun!' ) 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -