2

I have a shiny app that allows the user to download an HTML file (knitted from a .Rmd file) that includes the code used to run the analysis based on all the user inputs. I am trying to write the base .Rmd file that gets altered when user inputs vary. I am having trouble including user input variables (e.g. input$button1) into R code chunks. Say the user input for input$button1 = "text1".

```{r} results <- someFun(input$button1) ```

And I'd like to have it knitted like this:

```{r} results <- someFun('text1') ```

Every time I download the knitted HTML though, I get input$button1 getting written to file. I would also like to be able to produce an .Rmd file that is formatted with this substitution. It seems like knit_expand() might be the key, but I can't seem to relate available examples to my specific problem. Is the proper way to knit_expand() the whole .Rmd file and specify explicitly all the parameters you want subbed in, or is there a more elegant way within the .Rmd file itself? I would prefer a method similar to this, except that instead of using the asis engine, I could use the r one. Any help would be greatly appreciated. Thanks!

Community
  • 1
  • 1
ndimhypervol
  • 479
  • 1
  • 5
  • 17
  • the point, though, is to have it written to the HTML. – ndimhypervol Dec 19 '15 at 23:38
  • ```echo = FALSE``` hides the R code in the output, or am I missing something here? – ndimhypervol Dec 20 '15 at 01:49
  • Isnt that what you want? Try `echo=F, results='asis'` – CuriousBeing Dec 20 '15 at 01:50
  • no, I want to display the code, as in my example above. I'd like the user input to be displayed as whatever was input. – ndimhypervol Dec 20 '15 at 04:34
  • Please paste your entire R code chunk. – CuriousBeing Dec 20 '15 at 04:36
  • You need to pre-process the code chunk to resolve `input$button1` to its value. There are many ways to pre-process source documents, e.g. the `knit_expand()` function in **knitr**. – Yihui Xie Dec 24 '15 at 05:02
  • Thanks, Yihui. This allows me to generate a result with the proper text, but instead of the evaluation, I want that text to be within the code block in the knitted output. Is there some code chunk option in addition to echo=FALSE that will show the evaluated expression as a line of code in a block? – ndimhypervol Dec 24 '15 at 08:20
  • Yihui's past [answer](http://stackoverflow.com/questions/32944715/conditionally-display-block-of-markdown-text-using-knitr) solved it. Modifying the `asis` definition like he recommends allows me to use `{asis, eval = x, echo = x}` to get the conditional behavior I want, while allowing me to input evaluated inline code. Thanks Yihui... just took me a while to find this. – ndimhypervol Dec 29 '15 at 18:53
  • Spoke too soon. Edited the above to rephrase the ongoing problem. – ndimhypervol Dec 30 '15 at 19:04

1 Answers1

2

Got it. Solution below. Thanks to Yihui for the guidance. The trick was to knit_expand() the whole .Rmd file, then writeLines() to a new one, then render. With hindsight, the whole process makes sense. With hindsight.

For the example, p1 is a character param 'ice cream' and p2 is an integer param 10. There is a user-defined param in ui.R called input$mdType that is used to decide on the format provided for download.

Rmd file:

Some other text.
```{r}
results <- someFun("{{p1}}", {{p2}})
``` 

in the downloadHandler() within server.R:

  content = function(file) {
    src <- normalizePath('userReport.Rmd')
    # temporarily switch to the temp dir, in case you do not have write
    # permission to the current working directory
    owd <- setwd(tempdir())
    on.exit(setwd(owd))
    file.copy(src, 'userReport.Rmd')
    exp <- knit_expand('userReport.Rmd', p1=input$p1, p2=input$p2)
    writeLines(exp, 'userReport2.Rmd')

    out <- rmarkdown::render('userReport2.Rmd', switch(input$mdType, 
             PDF = pdf_document(), HTML = html_document(), Word = word_document()))
    }
    file.rename(out, file)
  }

Resulting userReport2.Rmd before rendering:

```{r}
results <- someFun("ice cream", 10)
```
ndimhypervol
  • 479
  • 1
  • 5
  • 17