i new r comments, appreciated!
i working on dynamic report, feed raw data r , output summary tables in pdf, formatted using latex. data working on divided segments , 1 segment summary needed.
in order want implement simple interactive drop down list, this:
segments<- c("a","b","c") <- menu(segments, graphics=true, title="choose segment")
however, knitr non-interactive environment , receive following error message:
menu() cannot used non-interactive
therefore, there way force knitr interactive or maybe somehow source interactive part other file?
i want use interactively because reports compiled people not familiar r, want make foolproof.
the question asks way have user interactively select item list inside rnw document (the same applies other files knitted, rmd):
%mydocument.rnw \documentclass{article} \begin{document} <<>>= letterindex <- menu(letters, graphics = true, title = "select favorite letter") sprintf("my favorite letter '%s'.", letters[letterindex]) @ \end{document}
this throws error when document knitted using "compile pdf" button in rstudio because menu
needs interactive r session "compile pdf" starts new, non-interactive session process document.
error in
menu(letters, graphics = true, title = "select favorite letter")
:menu()
cannot used non-interactively
to solve issue, "compile pdf" button must avoided. instead document can knitted calling knit
/knit2pdf
. note may have unexpected side-effects, see here idea this.
knit2pdf("mydocument.rnw")
works (which didn't expect when writing that comment). menu of choices pops in middle of knitting process. nevertheless, prefer solution separates knitting , user interaction (as suggested in comment):
#control.r letterindex <- menu(letters, graphics = true, title = "select favorite letter") knit2pdf("mydocument2.rnw")
%mydocument2.rnw \documentclass{article} \begin{document} <<>>= sprintf("my favorite letter '%s'.", letters[letterindex]) @ \end{document}
here, user interaction takes place before document knitted. result letterindex
saved in global environment , knitting process reads there.
in both cases, instead of opening rnw file , clicking "compile pdf", user opens r script containing knit2pdf
(and possibly menu
call) , clicks "source". should not increase difficulty level much.
Comments
Post a Comment