我正在寻找下载交互式 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 帖子。