Use multiple R Markdown files in Shiny tabs

丶灬走出姿态 提交于 2019-12-04 12:54:24

As an alternative to the approach mentioned in earlier answer you may want to try the approach illustrated in this repo https://github.com/vnijs/shiny-site. This is a proof of concept that you can render rmarkdown files using Knitr within a shiny without app having to break up the file into parts. It works by using Shiny's renderUI functionality and evaluating the rmarkdown file in the shinyServer environment.

Probably this is of little help, since it does not answer the key question, how to get interactive markdown files (with shiny) into the tabs.

RTutor parses one Rmd solution file, separates it into different parts and then uses a lot of dynamic UI which is filled with html output that is created by knitting these parts.

To dynamically knit markdown source in a variable txt to html you can use:

ktxt = knit(text=txt) html= markdownToHTML(text=ktxt, fragment.only=TRUE)

You can assign the created html to an htmlOutput or uiOutput via the corresponding render functions (see http://shiny.rstudio.com/articles/dynamic-ui.html). (RTutor uses the setUI function in the package shinyEvents for rendering the html, but that is just a wrapper.)

I don't know whether it is possible to render Rmd code that has shiny apps nested in this fashion. In RTutor the interactivity is performed by the RTutor package that builds all widgets on the fly. There is no shiny code in the underlying Rmd file.

My current approach is similar to the comment by @sebastian-kranz. I break-up an Rmd file into chunks because interactive elements will not work when calling a markdown document from within a shiny app (AFAIK). Example below. It would indeed be great if you could create a dynamic markdown document that works with runtime: shiny and then call it from a Shiny app.

output$mini_case_1 <- renderUI({
  tagList(
    rmarkdown::render("./cases/case1/01_test.Rmd", html_document()),
    inclRmd("./cases/case1/01_mini_case_1.Rmd"),
    sliderInput("price_coeff", label = "Adjust price sensitivity:", min = -20, max = 0, value = -6, step = 1),
    plotOutput("mc1_demand"),
    inclRmd("./cases/case1/02_mini_case_1.Rmd"),
    sliderInput("price", label = "Adjust price:", min = 0, max = 12, value = 3, step = 1),
    plotOutput("mc1_profit"),
    inclRmd("./cases/case1/03_mini_case_1.Rmd")
  )
})

inclRmd <- function(path) {
  paste(readLines(path, warn = FALSE), collapse = '\n') %>%
  knitr::knit2html(text = ., fragment.only = TRUE, options = "",
                   stylesheet=file.path(r_path,"../www/empty.css")) %>%
    gsub("&lt;!--/html_preserve--&gt;","",.) %>%
    gsub("&lt;!--html_preserve--&gt;","",.) %>%
    HTML %>%
    withMathJax
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!