Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

r - RMarkdown in Shiny Application

Problem

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))
```

Result:

Shiny App

Target

What I want to have is the output if I knit the rmd-file: RMarkdown HTML page

Specifically, I want to get the evaluation of the code-chunks (plot something...), and I want to get the rendered math equations.

Any ideas?

Edited Solution

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...