我正在寻找下载交互式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)
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)
```
```{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帖子。
答案 0 :(得分:1)
server replied: Internal Service Error
只是一个一般性错误,表示您的.Rmd
无法正确呈现。您的真正问题可能出在您的.Rmd
。
如果查看堆栈跟踪错误,它会显示:
Warning in min(w.l) : no non-missing arguments to min; returning Inf
当您单击下载按钮时,它将在render
上运行.Rmd
函数并尝试呈现它。从错误的外观来看,您正在尝试绘制某些东西,但是您的plot函数试图用来使该图出现的某些或所有对象均无法找到,因此它会因错误而失败。
由于绘图的数据来自闪亮的应用程序,因此您必须将其传递到.Rmd
中。更好的方法是将.Rmd
需要的数据直接作为列表传递给params=
的{{1}}参数。
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
后,就可以使用在params-list中指定的名称在param
中对其进行访问。因此,即使您的数据框在闪亮的应用程序中被命名为.Rmd
,由于将其传递给参数x
,您仍将使用{{1}在data
中对其进行访问}。
.Rmd
通过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
可以使用所需的数据,并且该数据具有正确数据(在Shiny中尤其重要,在其中对象的内容可能由于用户输入而以意外方式更改),并防止名称空间冲突(在环境中存在具有相同名称的意外对象)