无法在R Shiny应用中呈现R Markdown

3
我希望您能够翻译以下内容:

我正尝试生成一份报告,希望从Shiny应用程序中输出跨多个选项卡的表格和图表。下面是我的Shiny应用程序和markdown代码。 当我尝试下载报告时,我没有得到绘图,只有R代码。 如何在可下载的报告中呈现输出。

app.R

library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(DT)
library(plotly)
library(rmarkdown)


sidebar <- dashboardSidebar(
  sidebarMenu(id = "tab",
              menuItem("1", tabName = "1"),
              menuItem("2", tabName = "2"),
              downloadButton("report", "Generate report")

  )
)
body <-   ## Body content
  dashboardBody(box(
    tabItems(
      tabItem(tabName = "1",
              fluidRow(DT::dataTableOutput("atc_topline"))

      ),
      tabItem(tabName = "2",
              fluidRow(  plotlyOutput("stack"))
      )
    )
  ))

ui <-   dashboardPage(dashboardHeader(title = "Scorecard"),
                      sidebar,
                      body)

# Define the server code
server <- function(input, output,session) {
  delta_plot <- data.frame(structure(list(Month = structure(c(17257, 17287, 17318, 17348,
 17379, 17410, 17440, 17471, 17501, 17532), class = "Date"),  Interacting = c(11.326863, 11.21184,
  11.004666, 11.045412, 11.413678, 11.487366, 11.069, 11.177319,12.214473, 11.965812), Switcher = c(1.065518, 1.2053, 1.078746,
                                      0.792034, 0.945035, 1.086651, 1.334486, 0.919187, 0.683544, 0.875269
  )), row.names = c(NA, -10L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
 vars = "Month", drop = TRUE, indices = list( 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), group_sizes = c(1L,
                  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
                   Month = structure(c(17257, 17287, 17318, 17348, 17379, 17410,
                   17440, 17471, 17501, 17532), class = "Date")), row.names = c(NA,  -10L),
                  class = "data.frame", vars = "Month", drop = TRUE), na.action = structure(c(`11` = 11L), class = "omit")))

  output$stack <- renderPlotly({
    p <- plot_ly()

    ToAdd <- setdiff(colnames(delta_plot),"Month")

    for(i in ToAdd[2:3]){
      p <- p %>% add_trace(x = delta_plot$Month, y = delta_plot[[i]], name = i,
                           type = 'scatter',
                           mode = 'lines') %>% layout(yaxis = list( side = "right"))
    }
p
  })

  output$atc_topline <- renderDataTable({
    delta_plot

  })

  output$report <- downloadHandler(
    # For PDF output, change this to "report.pdf"
    filename = "report.html",
    content = function(file) {

      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      params <- list(n = delta_plot)


      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )
}

shinyApp(ui = ui, server = server)

report.Rmd

---
title: "Dynamic report"
output: html_document
params:
  n: NA
---

```{r}
library(plotly)
renderPlotly({
      p <- plot_ly()

    ToAdd <- setdiff(colnames(params$n),"Month")

    for(i in ToAdd[2:3]){
      p <- p %>% add_trace(x = params$n$Month, y = params$n[[i]], name = i,
                           type = 'scatter',
                           mode = 'lines') %>% layout(yaxis = list( side = "right"))
    }
p
  })
```

1
你是否期望RMD输出在shiny服务器上运行?我认为你需要从YAML中删除runtime: shiny。或者将文件放置在可以从服务器上运行RMD文件的目录中。 - Ryan Morton
最终它可能会托管在 r connect servershinyappsio 上。我已经删除了运行时,但仍然得到下载的报告,在那里我得到代码而不是渲染的图。 - SNT
仍然不起作用。 - SNT
我刚刚使用参数并添加库修改了我的问题,但似乎仍然无法正常工作。 - SNT
请在你的 YAML 中添加 params。教程在这里:https://rmarkdown.rstudio.com/developer_parameterized_reports - Ryan Morton
显示剩余2条评论
1个回答

5
使用以下的Markdown文件,我可以获取R代码和plotly绘图。我只是去掉了renderPlotly({ })title前面的空格,这导致了Yaml问题,如这里所述:

警告:Error in yaml::yaml.load: 解析器错误:在第2行、第1列找不到预期的“-”。

---
title: "Dynamic report"
output: html_document
params:
  n: NA
---

```{r}
library(plotly)

p <- plot_ly()

ToAdd <- setdiff(colnames(params$n),"Month")

for(i in ToAdd[2:3]){
  p <- p %>% add_trace(x = params$n$Month, y = params$n[[i]], name = i,
                       type = 'scatter',
                       mode = 'lines') %>% layout(yaxis = list( side = "right"))
}
p

```

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接