1

我正在寻找下载交互式 R 闪亮文档并将其保存到文件中以供其他用户(不拥有 R 或 Rstudio)查看和交互。R 闪亮文档运行良好,但当我尝试将下载的文件保存在某处时,下载按钮失败。这是我的代码;我使用了数据集“汽车”,因此可以重现错误:

闪亮的代码:

library(knitr)
library(shiny)
library(ggplot2)
library(readr)

x <- cars
x$dist_cut <- cut(x$dist, breaks = c(-1, 25, 50, 75, 100, 9999), 
                       labels = c("0-25", "26-50", "51-75", "76-100", ">100"))

t.cut <- table(x$dist_cut)

# Define UI for dataset viewer app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Cars Data"),

  # Sidebar layout with a input and output definitions ----
  sidebarLayout( position = "right",

                 # Sidebar panel for inputs ----
                 sidebarPanel(

                   # Input: Selector for choosing dataset ----
                   selectInput(inputId = "dataset",
                               label = "Choose a range for dist:",
                               choices = c("show all", ">100", "76-100", "51-75", "26-50", "0-25")),
                   downloadButton('downloadReport')

                 ),

                 # Main panel for displaying outputs ----
                 mainPanel(

                   # Output: Barplot ----
                   plotOutput(outputId = "distPlot"),

                   # Output: HTML table with requested number of observations ----

                   DT::dataTableOutput("view")


                 )
  )
)

# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {

  # Return the requested dataset ----
  datasetInput <- reactive({
    switch(input$dataset,
           "show all" = x,
           ">100" = x[x$dist_cut == ">100",],
           "76-100" = x[x$dist_cut == "76-100",],
           "51-75" = x[x$dist_cut == "51-75",],
           "26-50" = x[x$dist_cut == "26-50",],
           "0-25" = x[x$dist_cut == "0-25",])
  })

  # display the plot
  output$distPlot <- renderPlot({

    barplot(t.cut, beside = TRUE, col = c("green", "blue", "yellow", "orange", "red"), main = "Cars sorted by dist", ylim = c(0, 20), cex.main = 1.5, xlab = "dist", ylab = "Frequency")

  })

  # create the DT datatable
  output$view = DT::renderDataTable({
    DT::datatable(datasetInput(), filter = 'top', 
                  options = list(lengthMenu = c(5, 10, nrow(x)), pageLength = 5))

  })

  output$report <- downloadHandler(
    filename = function() paste0("report", ".html"),
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.rmd")
      file.copy("report.rmd", tempReport, overwrite = TRUE)

      # Set up parameters to pass to Rmd document
      params <- list(data = x,
                     title = "Plot Title",
                     limits = c(-10,10))
      # Knit the document, passing in the `params` list, and eval it in a
      # child of the global environment (this isolates the code in the document
      # from the code in this app).
      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }     
  )

}

# Create Shiny app ----
shinyApp(ui = ui, server = server)

降价代码:

---
output: html_document
params:
    data: NULL
    limits: NULL
    title: "Cars Data"
title: "`r params$title`"
---

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

```{r}
print(params$title)
barplot(table(params$x$dist_cut), beside = TRUE, col = c("green", "blue", "yellow", "orange", "red"), main = "Cars Split by dist", ylim = c(0, 20), cex.main = 1.5, xlab = "dist", ylab = "Frequency") 
library(DT) 
datatable(params$x, filter = 'top', options = list(lengthMenu = c(5, 10, nrow(x)), pageLength = 5))
```

使用此代码,单击下载按钮后,我会收到以下警告和错误:

Warning: Error in : path for html_dependency not provided
  [No stack trace available]
Warning in min(w.l) : no non-missing arguments to min; returning Inf
Warning in max(w.r) : no non-missing arguments to max; returning -Inf
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning: Error in plot.window: need finite 'xlim' values

我还收到一个标题为“下载失败”的弹出窗口,说明:

Error downloading {R shiny html link} - server replied: Internal Service Error

我花了大约一周的时间尝试调试它,但我还没有完全弄清楚我错过了什么。我是 R Shiny Web 应用程序的新手,我真的很想允许非 R 用户查看这些交互式报告以帮助他们完成工作。我希望这是足够的信息来帮助找到我的问题的解决方案。

谢谢!

更新:当我保存 HTML 文件并在 Internet Explorer 中打开它时,我只看到页面顶部的标题(汽车数据)和侧边栏面板(包含下拉框和下载按钮)。此外,我收到一条提示“Internet Explorer 限制此网页运行脚本或 ActiveX 控件”,当我按“允许阻止的内容”时没有任何反应。这和问题有关系吗?

对于最初没有发布可重现的示例,我深表歉意:这是我的第一篇 stackoverflow 帖子。

4

1 回答 1

1

server replied: Internal Service Error只是一个通用错误,这意味着您.Rmd无法正确呈现。您真正的问题可能与您的.Rmd.

如果您查看堆栈跟踪错误,它会显示:

Warning in min(w.l) : no non-missing arguments to min; returning Inf

当您单击下载按钮时,它会render在您的设备上运行该功能.Rmd并尝试渲染它。从错误的外观来看,您正在尝试绘制某些东西,但是您的绘图函数试图用来制作绘图的部分或全部对象无法找到,因此它失败并出现错误。

由于您的绘图数据来自闪亮的应用程序,因此您必须将其传递到.Rmd. 更好的方法是.Rmd直接将您需要的数据作为列表传递params=rmarkdown::render.

# NOTE: downloadHandler must be assigned to the ID of your downloadButton
output$downloadReport <- downloadHandler(
    filename = function() paste0("report", ".html"),
    content = function(file) {
        tempReport <- file.path(tempdir(), "report.rmd")
        file.copy("report.rmd", tempReport, overwrite = TRUE)

        # Set up parameters to pass to Rmd document
        #  these can be any time of R objects
        #  that you want your .Rmd to have access to
        params <- list(data = x,
                       title = "Plot Title",
                       limits = c(-10,10))
        # Knit the document, passing in the `params` list, and eval it in a
        #  child of the global environment (this isolates the code in the document
        #  from the code in this app).
        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
    }     
)

现在在您.Rmd的标题中定义这些参数:

---
output: html_document
params:
    data: NULL
    limits: NULL
    title: "Default title"
title: "`r params$title`"
---

然后通过访问params对象来使用它。一旦您将对象.Rmd作为 a传递给 ,您就可以使用您在 params-list 中提供的名称param访问它。.Rmd因此,即使您的数据框x在您的闪亮应用程序中命名,由于您将其传递给参数data,您将在.Rmdusing中访问它params$data

```{r}
print(params$title)
barplot(table(params$data$dist_cut), beside = TRUE,
        col = c("green", "blue", "yellow", "orange", "red"),
        main = "Cars Split by dist", ylim = c(0, 20),
        cex.main = 1.5, xlab = "dist", ylab = "Frequency")

library(DT)
DT::datatable(params$data, filter = 'top',
              options = list(lengthMenu = c(5, 10, nrow(params$data)),
                             pageLength = 5))
```

通过params参数显式传入对象(并在新环境中呈现.rmd)确保您需要的数据可用于您的.rmd,它具有正确的数据(在 Shiny 中尤其重要,因为您的对象的内容可能会以意想不到的方式发生变化由于用户输入)并防止命名空间冲突(在环境中您有意外的同名对象)

于 2018-08-16T16:40:32.000 回答