Is there a way to insert (and evaluate) an RMarkdown script in a shiny application. (Note, I am not looking for a shiny application in RMarkdown that is explained here, nor am I looking for Markdown scripts in shiny (see Shiny Gallery Markdown))
I am building an application that has text, equations, code-chunks, plots, and interactive elements. For convenience I use Markdown files for the text and equations and would like to have a plot sometimes in between (i.e. write most stuff in RMarkdown). As the shiny-app is more complex (I use shinydashboard
including many of its unique features), I would prefer an option that does not use the approach described in the first link.
A minimum working example would be:
R-file:
library(shiny) ui <- shinyUI( fluidPage( includeMarkdown("RMarkdownFile.rmd") ) ) server <- function(input, output) {} shinyApp(ui, server)
and "RMarkdownFile.rmd" in the same folder:
This is a text $$ E(x) = 0 $$ ```{r, eval = T} plot(rnorm(100)) ```
What I want to have is the output if I knit the rmd
-file:
Specifically, I want to get the evaluation of the code-chunks (plot something...), and I want to get the rendered math equations.
Any ideas?
Thanks to the input of @Bunk, I chose to render all rmd
files to md
files with the command knit
and then include the md
files in the shiny app (I use markdown instead of html as the latter produced some issues with equations). Lastly, the includeMarkdown
is wrapped in withMathJax
to ensure the proper display of equations.
The final code looks like this:
library(shiny) library(knitr) rmdfiles <- c("RMarkdownFile.rmd") sapply(rmdfiles, knit, quiet = T) ui <- shinyUI( fluidPage( withMathJax(includeMarkdown("RMarkdownFile.md")) ) ) server <- function(input, output) { } shinyApp(ui, server)
There are two ways to do this: Defining the application inline using the shinyApp() function; or. Referring to an external application directory using the shinyAppDir() function.
It generally comes down to the amount of interactivity you need in your app. For a full solution where data is updated and processed in real-time, Shiny is your best option. If you just need a nice format for presentation offline, then RMarkdown can produce some very nice looking formats.
If a chunk works in R but not when you knit, it is almost always because you've changed a variable in your global working environment not using code in a chunk. Try restarting your R session and running each chunk sequentially to make sure all your variable values are up to date.
To insert an image, you can also use an R code chunk. --- title: "R Markdown Tips and Tricks" output: html_document --- You can also insert an image using R code: ```{r} knitr::include_graphics("img/rmarkdown_hex. png") ``` The image looks smaller by default though.
I think knitting it and rendering a UI should work.
library(shiny) library(knitr) ui <- shinyUI( fluidPage( uiOutput('markdown') ) ) server <- function(input, output) { output$markdown <- renderUI({ HTML(markdown::markdownToHTML(knit('RMarkdownFile.rmd', quiet = TRUE))) }) } shinyApp(ui, server)
As per @elevendollar's request, this is what I ended up using:
library(shiny) library(knitr) rmdfiles <- c("RMarkdownFile.rmd") sapply(rmdfiles, knit, quiet = T) ui <- shinyUI( fluidPage( withMathJax(includeMarkdown("RMarkdownFile.md")) ) ) server <- function(input, output) { } shinyApp(ui, server)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With