Use multiple R Markdown files in Shiny tabs

这一生的挚爱 提交于 2019-12-06 06:05:55

问题


I am building a Shiny App and I'd like to have multiple tabsets. The code I've got so far gives me that:

shinyUI(navbarPage("OEI Grant",
  tabPanel("Part 1 - Organization",
           tabsetPanel("1.x",
                       tabPanel("1.1"),
                       tabPanel("1.2"),
                       tabPanel("1.3")
                       ))))

The part I cannot figure out is how to get a separate interactive document (R markdown .Rmd file) for each tab (1.1, 1.2, etc.).

I am looking for the equivalent of the includeMarkdown() function but for R Markdown files that themselves contain Shiny Apps.

For example in 1.1 I may want to display the output from the following simple .Rmd file:

---
runtime: shiny
---
# Data visualization

Example visualization

```{r read-in-data, echo = FALSE, eval=TRUE, message=TRUE}
library(ggplot2)
data(OrchardSprays) # Use this data                                                                                                                                                                            
head(OrchardSprays)
```

## Histogram

We can also look at this data in an interactive histogram.

```{r histogram, echo = FALSE, eval=TRUE}
shinyAppDir(
    "histogram/",
    options=list(width="100%", height=450)
  )
```

This RTutor Shiny App is something similar to what I'm trying to do as far as multiple tabs but from looking at their code, I think everything is provided in one R markdown file and somehow parsed into different sections.

R Markdown documentation talks about linking multiple pages but I want the content and not links.

The only example in the Gallery for Tabsets shows how to put output from server.R into different tabs but not separate R Markdown files.

Any ideas on how to do this?


回答1:


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.




回答2:


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.




回答3:


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
}


来源:https://stackoverflow.com/questions/28551267/use-multiple-r-markdown-files-in-shiny-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!