2

I'm writing a vignette documentation for an R package, i.e. using Sweave to generate a tex file (and finally a PDF).

I want to document a function that requests to the user some input in the console (standard input). To simplify the function is:

foo<-function() {
    value=readline('Insert some value:')
    return(value)
}

To include an example of this function in the documentation, I write in the .Rnw file:

<<>>=
result=foo()
@

Now we are on a document file, so it is obviously not possible to insert a value in the console, actually the output pdf looks like this:

Some introduction...

> foo()

Insert some value: 

Some conclusion...

Since the compilation of the tex/pdf succeeds, I suppose that Sweave executes the function, and automatically redirects to the stdin a carriage return (or simply stops the R process).

My question is if it is possible to simulate an user input.

A workaround might be to add another 'hidden' chunk:

<<echo=FALSE>>=
result='Some value'
@

The echo is set to false, so this instruction will not appear on the document, and the variable result takes the desired value. This is useful if I need to reuse this variable in some future chunks, but in this way the reader can be confused by seeing in the future chunks the value of the variable appearing from nowhere: I should explain in plain text that I set the value manually because it is not possible to request an user input in a documentation. This is not really elegant.

What I would like at the end in the pdf is something like:

Some introduction...

> foo()

Insert some value: Here you can insert your desired value...

Some conclusion...

So maybe in the .Rnw file something like:

<<input="Here you can insert your desired value...">>=
result=foo()
@

or I don't know if it is necessary to load the data from an external file, it could also be fine, but I have not found anything like this.

WoDoSc
  • 2,598
  • 1
  • 13
  • 26

0 Answers0