这是我的第一次堆栈溢出问题,如果您认为我没有正确完成,请告诉我。
我正在使用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)
}
)
}
)
```