1

My usecase is as follows:

  • Display GUI to collect some R expressions
  • Generate a single source file containing my simulation framework, intermixed with the R expressions provided by the user
  • Allow the user to generate the single source file. If any of the expressions specified by the user contain errors (syntax or runtime), I want to display these in a user-friendly way.

In short, I want to do something as follows:

myExpression <- "3 * 5" myExprParsed <- tryCatch( parse(myExpression), .... ) 

My question is very simple: does R have some construct like \Q \E in regex, fully quoting a text string from beginning to end ? It is not unlikely that myExpression will contain the " character, thereby introducing a syntax error. I do not want to write the text contents to a separate text file and use source().

=Example=

myExpression <- " XXXXXXX " 

where XXXXXX is paste("my random value is ", runif(3)) would amount to

myExpression <- " paste("my random value is ", runif(3)) " 

which would give a syntax error. I want something like

myExpression <- verbatim@ paste("my random value is ", runif(3)) @ 
1
  • 1
    Dont understand your question. What has a regex got to do with it? Why would having quotes input by your users be a problem? They're being stored in strings, which isn't a problem. Commented Oct 17, 2012 at 9:04

1 Answer 1

2

Get some input from the user - you'd just be getting values from text boxes in your GUI, I guess. I'll use scan:

> dowhat = scan(what="") 1: x="hello world" 3: Read 2 items 

We have quoted strings:

> dowhat [1] "x=\"hello" "world\"" 

We can parse it:

> parse(text=dowhat) expression(x="hello world") 

And there's a line break because scan() splits things by lines. Not really a problem, and fixable if you care.

Now let's actually run it:

> eval(parse(text=dowhat)) 

And now we should have an x:

> x [1] "hello\nworld" > 

?

Sign up to request clarification or add additional context in comments.

1 Comment

So I will have to import the code from somewhere: either user input, or a file, or ... (it seems that I cannot just enter blocks of code like I can in e.g. XML using [CDATA )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.