0

I am new to R so any comments, help would be greatly appreciated!

I am working on dynamic report, where I will feed raw data into R and than output summary tables in PDF, formatted using LaTeX. Data that I am working on is divided into segments and sometimes just one segment summary will be needed.

In order to do so I want to implement simple interactive drop down list, which will look something like this:

segments<- c("A","B","C") i <- menu(segments, graphics=TRUE, title="Choose segment")

However, knitr is non-interactive environment and I receive following error message:

menu() cannot be used non-interactive

Therefore, is there a way to force knitr to be interactive or maybe I could somehow source the interactive part from other file?

I really want to use it interactively because sometimes reports will be compiled by people not familiar with R, so I want to make it foolproof.

An economist
  • 1,301
  • 1
  • 15
  • 35
  • What about a file containing `i <- menu(...);knit2pdf(input = ...)`? Assuming that the "people not familiar with R" are able to source an R file ... – CL. Jan 30 '16 at 17:23
  • @CL. Hi, thank you for your comment! As I said I am also quite new to R so could you elaborate a bit because I do not fully follow. Thank you – An economist Jan 31 '16 at 02:01

1 Answers1

2

The question asks for a way to have the user interactively select an item from a list inside an RNW document (the same applies for other files that are knitted, like RMD):

%mydocument.Rnw

\documentclass{article}
\begin{document}
<<>>=
letterIndex <- menu(LETTERS, graphics = TRUE, title = "Select your favorite letter")
sprintf("My favorite letter is '%s'.", LETTERS[letterIndex])
@
\end{document}

This throws an error when the document is knitted using the "Compile PDF" button in Rstudio because menu needs an interactive R session but "Compile PDF" starts a new, non-interactive session to process the document.

Error in menu(LETTERS, graphics = TRUE, title = "Select your favorite letter"): menu() cannot be used non-interactively

To solve this issue, the "Compile PDF" button must be avoided. Instead the document can be knitted calling knit/knit2pdf. Note that this may have some unexpected side-effects, see here to get an idea about this.

knit2pdf("mydocument.Rnw") works (which I didn't expect when writing that comment). The menu of choices pops up in the middle of the knitting process. Nevertheless, I would prefer a solution that separates knitting and user interaction (as suggested in the comment):

#control.R
letterIndex <- menu(LETTERS, graphics = TRUE, title = "Select your favorite letter")
knit2pdf("mydocument2.Rnw")

%mydocument2.Rnw

\documentclass{article}
\begin{document}
<<>>=
sprintf("My favorite letter is '%s'.", LETTERS[letterIndex])
@
\end{document}

Here, the user interaction takes place before the document is knitted. The result letterIndex is saved in the global environment and the knitting process reads it from there.

In both cases, instead of opening the RNW file and clicking "Compile PDF", the user now opens an R script containing knit2pdf (and possibly the menu call) and clicks "Source". This should not increase the difficulty level too much.

Community
  • 1
  • 1
CL.
  • 14,577
  • 5
  • 46
  • 73