【问题标题】:Saving a Navbar Shiny App In Rmarkdown在 Rmarkdown 中保存导航栏闪亮的应用程序
【发布时间】:2026-01-26 23:15:02
【问题描述】:

我正在尝试将一个闪亮的应用程序保存在一个 Rmarkdown 文件中作为一个独立的 HTML 页面。

我可以用一个简单的 DT::datatable() 来做到这一点:

---
title: "Test4"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r t4, echo=FALSE, message=FALSE, echo=FALSE}
DT::datatable(iris)

紧随其后

rmarkdown::render(input = "Test4.Rmd", output_file = "Test4.html", runtime = "shiny")

给我一​​个包含 iris 数据集的 html 文件,我可以根据需要将其粘贴到文件服务器上。 $Employer 喜欢它,并感谢 Joe Cheng 等人向我指出该解决方案。

( 另外,Joe Cheng 在 google Shiny 群组上发了这个: 如果您只有一个 DT::datatable() 对象(称为“x”),那么您可以调用 htmlwidgets::saveWidget(x, "filepath.html") 将其保存为 HTML 页面 )

但是,$employer 现在要求我将其中两个以标签格式放在一起。

当我使用此代码时,如果我使用 RStudio 中的“运行文档”,则 Rmd 页面会正确呈现:

---
title: "Test3"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Embedded Application

Test To Try and Render This Out As Standalone Tabbed Shiny App With Two DT::Dataframes.

```{r tabsets, echo=FALSE, warning=FALSE, message=FALSE}
shinyApp(
  ui <- (
  navbarPage(
  title = 'Testing Saving Shiny',
  tabPanel('MTCars', DT::dataTableOutput('mtcarz')) ,
  tabPanel('Irises', DT::dataTableOutput('iriz'))
  )
  )
  , 
  server <- (function(input, output) {
  output$mtcarz <- DT::renderDataTable({
  DT::datatable(
  mtcars,
  escape = FALSE,
  rownames = FALSE,
  options = list(
  pageLength = 25,
  autoWidth = TRUE
  )
  )
  })

  output$iriz <- DT::renderDataTable({
  DT::datatable(
  iris,
  escape = FALSE,
  rownames = FALSE,
  options = list(
  pageLength = 25,
  autoWidth = TRUE
  )
  )
  })
  })

)
```

但是当我在其上使用 rmarkdown::render 时,HTML 页面给了我预期的框架(标题等),但其中没有任何选项卡/数据框。

我正在使用 DT 的 v.1、rmarkdown 的 v.0.9.2 和闪亮的 v.0.12.2 以及 R 3.2.1。

【问题讨论】:

    标签: r datatables shiny r-markdown dt


    【解决方案1】:

    我可能遗漏了一些东西,但我不知道Shiny 应用程序可以在没有Shiny 服务器的情况下工作。这会是动态的吗?如果没有,你可以这样做。

    ```{r echo = FALSE, warning = FALSE}
    library(shiny)
    navbarPage(
      title = 'Testing Saving Shiny',
      tabPanel('MTCars', DT::datatable(
        mtcars,
        escape = FALSE,
        rownames = FALSE,
        options = list(
          pageLength = 25,
          autoWidth = TRUE
        )
      )),
      tabPanel('Irises', DT::datatable(
        iris,
        escape = FALSE,
        rownames = FALSE,
        options = list(
          pageLength = 25,
          autoWidth = TRUE
        )
      ))
    )
    ```
    

    【讨论】:

      最近更新 更多