我希望下载一个交互式R闪亮文档,并将其保存到一个文件,供其他用户(不拥有R或Rstudio)查看和交互 . R闪亮文档运行正常,但当我尝试将下载的文件保存到某处时,下载按钮失败 . 这是我的代码;我使用了数据集'cars',因此可以重现错误:
闪亮的代码:
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)
Rmarkdown代码:
---
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)
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))
使用此代码,我在单击下载按钮后收到以下警告和错误:
```java
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
我还收到一个 Headers 为“下载失败”的弹出窗口,说明:
Error downloading {R shiny html link} - server replied: Internal Service Error
我花了大约一个星期试图调试这个,我还没弄清楚我错过了什么 . 我是R闪亮的Web应用程序的新手,并且非常希望允许非R用户查看这些交互式报告以帮助他们完成工作 . 我希望这是足够的信息,以帮助找到我的问题的解决方案 .
谢谢!
更新:当我保存HTML文件并在Internet Explorer中打开它时,我只看到页面顶部的 Headers (汽车数据)和侧边栏面板(包含下拉框和下载按钮) . 另外,我得到一个提示,说明“Internet Explorer限制此网页运行脚本或ActiveX控件”,当我按下“允许阻止的内容”时没有任何反应 . 这与问题有关吗?
我为最初没有发布可重现的示例而道歉:这是我的第一篇stackoverflow帖子 .
1 回答
server replied: Internal Service Error
只是一般错误,表示您的.Rmd
无法正确呈现 . 您的.Rmd
可能存在真正的问题 .如果查看堆栈跟踪错误,它会说:
当您单击下载按钮时,它会在
.Rmd
上运行render
函数并尝试渲染它 . 从错误的外观来看,您找到了're trying to plot something, but some or all of the objects your plot function is trying to use to make the plot can',因此失败并出现错误 .由于您的情节数据来自闪亮的应用程序,您必须将其传递给
.Rmd
. 更好的方法是将.Rmd
需要的数据直接传递给传递给rmarkdown::render
的params=
参数的列表 .现在在
.Rmd
中,您可以在 Headers 中定义这些参数:然后通过访问
params
对象来使用它 . 将对象作为param
传递给.Rmd
后,可以使用params-list中给出的名称在.Rmd
中访问该对象 . 因此,即使您的数据框在闪亮的应用程序中名为x
,因为您将其传递给参数data
,您将使用params$data
在.Rmd
中访问它 .