2

I am passing plots generated from shiny to rmarkdown to generate html report. The trouble is wordcloud and pie chart are being accepted in params in rmarkdown document.

Question : How to pass wordcloud and pie plots in rendered html via shiny ?

abc.Rmd

title: "Report using R Markdown"
subtitle: "ABC "
author: "Author name"
output:
     prettydoc::html_pretty:
     theme: architect
params:
    wc : 'NULL'
    table: 'NULL'
    pie: 'NULL'

app.R(snippet)

rmarkdown::render(input = "report.Rmd",
                  output_file = "report.html",
                  params = list(wc=getWordcloud(),
                                table=getTab(),
                                pie=getPie()))

Note : getWordcloud(),getTab(),getPie() function are returning plots perfectly in shiny app.

YCR
  • 3,794
  • 3
  • 25
  • 29
parth
  • 1,571
  • 15
  • 24

1 Answers1

2

You can't have as a parameter type a function.

See here in parameter type: http://rmarkdown.rstudio.com/developer_parameterized_reports.html

All of the standard R types that can be parsed by the yaml::yaml.load function are supported including character, integer, numeric, and logical.

I strongly recommend to find a way to make your code working without the need to pass a function in the parameter. Maybe you can pass options of the function and include the function in the rmd?

But, there is ways to bypass that:

One is to use in the parameter the name of the function as a string and to use eval() to evaluate the string as code.

abc.Rmd

title: "Report using R Markdown"
subtitle: "ABC "
author: "Author name"
output:
     prettydoc::html_pretty:
     theme: architect
params:
    wc : wc_function
    table: table_function
    pie: pie_function


 eval(paste0(param$wc_function, "(", my_options_as_string, ")")) 

app.R(snippet)

rmarkdown::render(input = "report.Rmd",
                  output_file = "report.html",
                  params = list(wc="getWordcloud",
                                table="getTab",
                                pie="getPie"))

Another one is to have another r script with the functions, called in the rmarkdown with source.

That way, you can pass the path of the file as a parameter and it allows you to get access to your function inside the rmarkdown (but it implies the name of the functions are fixed)

YCR
  • 3,794
  • 3
  • 25
  • 29
  • my plots are being generated in server side of shiny app, so even if i source my file Reading objects from shinyoutput object not allowed. – parth Apr 28 '17 at 11:01
  • you want to include a call to a shiny app in your rmarkdown, like in [that exemple](http://rmarkdown.rstudio.com/demos/14-shiny.Rmd)? – YCR Apr 28 '17 at 11:05
  • actually it's vice versa, i have added **download** button in my shiny app which renders markdown document with my generated plots in shiny as html document – parth Apr 28 '17 at 11:08
  • Why not directly include the parameters in the url, like [here](http://stackoverflow.com/questions/32872222/how-do-you-pass-parameters-to-a-shiny-app-via-url)? That way, you can include a set of buttons in your main page to define your parameter then a redirect link to the generated rmarkdown with the parameters. It is possible to include js in your rmarkdown quite easily. – YCR Apr 28 '17 at 11:13
  • This is a good way but as soon as i download it as html, plots display in saved html file. – parth Apr 28 '17 at 11:25
  • Try the option `self_contain:yes` – YCR Apr 28 '17 at 11:27
  • Thanks @YCR that helped. i encoded images into html using --self-contained – parth Apr 28 '17 at 11:33
  • you know what to do :) – YCR Apr 28 '17 at 11:35