R:无法下载交互式报告
我希望下载一个交互式R文档,并将其保存到一个文件中,供其他用户(不拥有R或Rstudio)查看和交互。R Shining文档运行正常,但当我尝试将下载的文件保存到某个位置时,下载按钮失败。这是我的密码;我使用了数据集“cars”,因此可以重现错误: 闪亮代码:R:无法下载交互式报告,r,shiny,shiny-reactivity,R,Shiny,Shiny Reactivity,我希望下载一个交互式R文档,并将其保存到一个文件中,供其他用户(不拥有R或Rstudio)查看和交互。R Shining文档运行正常,但当我尝试将下载的文件保存到某个位置时,下载按钮失败。这是我的密码;我使用了数据集“cars”,因此可以重现错误: 闪亮代码: library(knitr) library(shiny) library(ggplot2) library(readr) x <- cars x$dist_cut <- cut(x$dist, breaks = c(-1,
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)
使用此代码,我在单击下载按钮后收到以下警告和错误:
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闪亮的web应用程序,非常希望非R用户能够查看这些交互式报告,以帮助他们完成工作。我希望这些信息足以帮助我找到解决问题的方法
谢谢
更新:当我保存HTML文件并在internet explorer中打开它时,我只在页面顶部和侧栏面板(包含下拉框和下载按钮)看到标题(汽车数据)。此外,我收到一条提示:“Internet Explorer限制此网页运行脚本或ActiveX控件”,当我按“允许阻止内容”时,不会发生任何事情。这与问题有关吗
我很抱歉最初没有发布一个可复制的示例:这是我的第一篇stackoverflow文章 服务器回复:内部服务错误只是一个一般性错误,这意味着您的
.Rmd
无法正确呈现。您真正的问题可能是您的.Rmd
如果查看堆栈跟踪错误,它会显示:
Warning in min(w.l) : no non-missing arguments to min; returning Inf
单击下载按钮时,它会在.Rmd
上运行渲染
功能并尝试渲染它。从错误的外观来看,您正在尝试打印某些内容,但是您的plot函数尝试用于进行打印的部分或所有对象都找不到,因此它失败并出现错误
由于绘图数据来自闪亮的应用程序,因此必须将其传递到.Rmd
。更好的方法是将.Rmd
需要的数据作为列表直接传递给rmarkdown::render
的params=
参数
# 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())
)
}
)
然后通过访问params
对象来使用它。将对象作为参数
传递到.Rmd
后,可以使用参数列表中提供的名称在.Rmd
中访问该对象。因此,即使在您闪亮的应用程序中,您的数据帧名为x
,但由于您将其传递到参数data
,因此您将使用params$data
在.Rmd
中访问它
```{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
,并确保其具有正确的数据(尤其是在对象内容可能因用户输入而以意外方式更改的情况下)并防止名称空间冲突(环境中存在同名的意外对象)感谢您发布问题的真实答案!我很确定我现在能解决这个问题,这很有效!我有两个后续问题:1)为了让它起作用,我必须复制我的R标记文件并粘贴到一个临时目录(C:\Users\U1335567\AppData\Local\Temp\RtmpkHyWYg)中。如果没有,R无法找到报告。我是否每次尝试下载报告时都必须这样做,或者我是否可以更改代码,以便它从我的工作目录中读取标记文件?2)我有另一个带有交互式图形的报告。在数据框“cars”(使用R-shinny)中,假设我想显示特定“速度”的“dist”条形图(使用numeriInput()框选择速度)。条形图将根据我选择的速度而变化。下载时是否会丢失此功能,或者是否有办法保留此功能?如果我的问题不清楚,请让我知道您是否需要我给您一个可验证的示例。这些行:tempReport如果您有更多问题,您真的应该问另一个问题。这里的问答形式只有在你有一个特定的问题和一个特定的答案时才有效。你可以问多少问题没有限制,所以如果你还有其他问题,就再问一个问题
---
output: html_document
params:
data: NULL
limits: NULL
title: "Default title"
title: "`r params$title`"
---
```{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))
```