这是我的第一个堆栈溢出问题,所以如果您认为我没有正确完成此操作,请告诉我。
我正在使用 flexdashboard 和闪亮的模块来创建一个交互式文档,我想包括一个 rPivotTable 和一个下载按钮,它将数据透视表作为 csv 下载。此 csv 应包含用户更改 rPivotTable 的输入时创建的数据表。
我使用了其他堆栈溢出答案(例如这里的答案:download rpivotTable ouput in shiny)来编写代码,我觉得我很接近了。但是,当我运行下面的代码时,在我的 Chrome 浏览器中打开交互式文档,然后单击“下载”按钮会发生错误。Chrome 页面底部弹出的小下载栏不显示 csv 文件,而是显示消息“失败 - 服务器问题”。要查看是否aSummaryTable
完全生成,我将输出包含在 UI ( DT::dataTableOutput(ns("aSummaryTable"))
) 中,但是当我运行 Rmd 时,生成的文档仅显示 rPivotTable 和下载按钮。
我对闪亮还是很陌生,从未使用过java脚本,所以我不太了解onRefresh
我的代码部分:
onRefresh = htmlwidgets::JS("function(config) {
Shiny.onInputChange('myData', document.getElementById('pivot').innerHTML);
}")
这是我完整的(希望可重现的)Rmarkdown 代码。(如果您有任何想法为什么我可能会收到此错误以及如何解决此问题,我将非常感激)
---
title: "Report"
output:
flexdashboard::flex_dashboard:
theme: simplex
orientation: rows
social: menu
source_code: embed
runtime: shiny
---
```{r setup, include=FALSE}
## Installing packages if not already installed & require(package)
usePackage <- function(p) {
if (!is.element(p, installed.packages()[,1]))
install.packages(p, dep = TRUE)
require(p, character.only = TRUE)
}
usePackage('flexdashboard')
usePackage('plotly')
usePackage('data.table')
usePackage('rpivotTable')
usePackage('knitr')
usePackage('Rcpp')
usePackage('stringi')
usePackage('tidyr')
usePackage('chron')
usePackage('DT')
usePackage('caroline')
usePackage('rvest')
usePackage('htmlwidgets')
```
Demo
=========================
### Demo
```{r, include=FALSE}
# UI function
demoUI <- function(id) {
ns <- NS(id)
fillCol(height = 600, flex = c(NA, 1),
DT::dataTableOutput(ns("aSummaryTable")),
rpivotTableOutput(ns("pivot"))
,
inputPanel(
downloadButton(ns('downloadData'), 'Download ')
)
)
}
# Server function
demoServer <- function(input, output, session) {
output$pivot <- renderRpivotTable({
library(htmlwidgets)
library(rpivotTable)
rpivotTable(data = datasets::mtcars, aggregatorName = "Count", rendererName = "Bar Chart",
onRefresh = htmlwidgets::JS("function(config) {
Shiny.onInputChange('myData', document.getElementById('pivot').innerHTML);
}"))
})
# Clean the html and store as reactive
summarydf <- eventReactive(input$myData,{
library(htmlwidgets)
input$myData %>%
read_html %>%
html_table(fill = TRUE) %>%
.[[2]]
})
# show df as DT::datatable
output$aSummaryTable <- DT::renderDataTable({
DT::datatable(summarydf(), rownames = FALSE)
})
output$downloadData <- downloadHandler(
filename <- "rpivottable_mtcars.csv",
# This function should write data to a file given to it by
# the argument 'file'.
content = function(file) {
# Write to a file specified by the 'file' argument
write.csv(summarydf(), file,
row.names = FALSE)
}
)
}
```
```{r}
demoUI("demo")
callModule(demoServer, "demo")
```
编辑:我刚刚自己解决了这个问题。使用 Shiny Modules 时,似乎无法将 rPivotTable 切片的数据从客户端发送回服务器。但是,当使用内联应用程序或外部应用程序时,它确实有效。这是一个可重现且最终有效的代码(以防万一有人遇到此问题):
---
title: "Report"
output:
flexdashboard::flex_dashboard:
theme: simplex
orientation: rows
social: menu
source_code: embed
runtime: shiny
---
```{r setup, include=FALSE}
## Installing packages if not already installed & require(package)
usePackage <- function(p) {
if (!is.element(p, installed.packages()[,1]))
install.packages(p, dep = TRUE)
require(p, character.only = TRUE)
}
usePackage('flexdashboard')
usePackage('plotly')
usePackage('data.table')
usePackage('rpivotTable')
usePackage('knitr')
usePackage('Rcpp')
usePackage('stringi')
usePackage('tidyr')
usePackage('chron')
usePackage('DT')
usePackage('caroline')
usePackage('rvest')
usePackage('htmlwidgets')
```
Demo
=========================
### Demo
```{r}
shinyApp(
ui = fillPage(
fillCol(flex = c(NA, 1),
DT::dataTableOutput("aSummaryTable"),
rpivotTableOutput("pivotTab")
,
inputPanel(
downloadButton('downloadData', 'Download ')
)
)),
server = function(input, output, session) {
output$pivotTab <- renderRpivotTable({
rpivotTable(data = datasets::mtcars, aggregatorName = "Count", rendererName = "Table",
onRefresh = htmlwidgets::JS("function(config) {Shiny.onInputChange('myData', document.getElementById('pivotTab').innerHTML);}")
)
})
# Clean the html and store as reactive
summarydf <- eventReactive(input$myData,{
input$myData %>%
read_html %>%
html_table(fill = TRUE) %>%
.[[2]]
})
# show df as DT::datatable
output$aSummaryTable <- DT::renderDataTable({
DT::datatable(summarydf(), rownames = FALSE)
})
output$downloadData <- downloadHandler(
filename <- "rpivottable_mtcars.csv",
content = function(file) {
write.csv(summarydf(), file,
row.names = FALSE)
}
)
}
)
```