在 Shiny 选项卡中使用多个 R Markdown 文件

Use multiple R Markdown files in Shiny tabs

我正在构建一个闪亮的应用程序,我想要多个标签集。到目前为止我得到的代码给了我:

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

我无法弄清楚的部分是如何为每个选项卡(1.1、1.2 等)获取单独的交互式文档(R markdown .Rmd 文件)。

我正在寻找 includeMarkdown() 函数的等价物,但对于本身包含 Shiny Apps 的 R Markdown 文件。

例如在 1.1 中,我可能想显示以下简单 .Rmd 文件的输出:

---
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 与我在多个选项卡方面尝试做的类似,但从他们的代码来看,我认为所有内容都在一个 R markdown 文件中提供,并以某种方式解析为不同的部分。

R Markdown 文档谈论 linking multiple pages 但我想要内容而不是链接。

唯一的 example in the Gallery for Tabsets 展示了如何将 server.R 的输出放入不同的选项卡而不是单独的 R Markdown 文件。

关于如何做到这一点有什么想法吗?

可能这没什么帮助,因为它没有回答关键问题,即如何将交互式降价文件(带有闪亮的)放入选项卡中。

RTutor 解析一个 Rmd 解决方案文件,将其分成不同的部分,然后使用大量动态 UI 填充由编织这些部分创建的 html 输出。

要将变量 txt 中的降价源动态编织到 html,您可以使用:

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

您可以通过相应的渲染函数将创建的 html 分配给 htmlOutputuiOutput(参见 http://shiny.rstudio.com/articles/dynamic-ui.html)。 (RTutor 使用包 shinyEvents 中的 setUI 函数来呈现 html,但这只是一个包装器。)

我不知道是否可以呈现具有以这种方式嵌套的闪亮应用程序的 Rmd 代码。在 RTutor 中,交互性由实时构建所有小部件的 RTutor 包执行。底层 Rmd 文件中没有闪亮的代码。

我目前的做法类似于@sebastian-kranz 的评论。我将 Rmd 文件分解为多个块,因为在闪亮的应用程序 (AFAIK) 中调用降价文档时,交互式元素将不起作用。下面的例子。如果您可以创建一个与 runtime: shiny 一起使用的动态降价文档,然后从 Shiny 应用程序调用它,那确实很棒。

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://github.com/vnijs/shiny-site 中说明的方法。这是一个概念证明,您可以在 shiny 中使用 Knitr 渲染 rmarkdown 文件,而无需应用程序将文件分解成多个部分。它通过使用 Shiny 的 renderUI 功能并在 shinyServer 环境中评估 rmarkdown 文件来工作。