简体   繁体   English

如何从闪亮的仪表板上下载条形图数据?

[英]How to download data of bar chart from shiny dashboard?

I am getting error file, while downloading data of bar chart rendered in shiny UI. 下载发光的UI中呈现的条形图数据时,出现错误文件。 There is a download button just below the chart. 图表下方有一个下载按钮。 While click on the 'Download data' button, data should be downloaded into csv format. 在点击“下载数据”按钮时,数据应下载为csv格式。

Code: 码:

library(shiny)
library(ECharts2Shiny)

dat <- data.frame(c(1, 2, 3), c(2, 4, 6))
names(dat) <- c("Type-A", "Type-B")
row.names(dat) <- c("Time-1", "Time-2", "Time-3")

ui <- fluidpage( loadEChartsLibrary(),
tags$div(id="test", style="width:50%;height:400px;"),
deliverChart(div_id = "test"), downloadButton("test", "Download Data"))

server <-  function(input, output) {
renderBarChart(div_id = "test", grid_left = '1%', direction = "vertical",               
data = dat)
}
shinyApp(ui = ui, server = server)

I want to download the data of that bar chart in ".csv" format. 我想以“ .csv”格式下载该条形图的数据。 Can anyone help me out to correct the code? 谁能帮助我更正代码?

Thank you. 谢谢。

I think you cannot use the same "renderTable" server output for all of your UI inputs. 我认为您不能为所有UI输入使用相同的“ renderTable”服务器输出。 AKA, you need to make a separate input and output for your image in the form of a downloadable file. AKA,您需要以可下载文件的形式为图像进行单独的输入和输出。 Here is an example of what I did to download a .csv file: 这是我下载.csv文件的示例:

output$downloadData <- reactive({
output$downloadData <- downloadHandler(
filename = function() {
  paste("ProcessedData-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
  write.csv(DataTable, file)
}
)

Therefore in your download button you would replace "test" with "downloadData" so that it connects to a new server output. 因此,在下载按钮中,将“ test”替换为“ downloadData”,以便它连接到新的服务器输出。 Notice that I am creating the file here with write.csv, but not actually writing to any disk just generating the file and passing it into the downloadData output for users to trigger and then save to their own disk when they hit the downloadData input button. 请注意,我在这里使用write.csv创建文件,但实际上并没有写入任何磁盘,只是生成了文件并将其传递到downloadData输出中,以便用户触发,然后在单击downloadData输入按钮时保存到自己的磁盘中。 You are trying to save a picture so I suggest trying something along the lines of generating an image file inside of the content call. 您正在尝试保存图片,因此我建议尝试在内容调用内部生成图像文件的方法。 Recall that you can save images as objects: 回想一下,您可以将图像另存为对象:

#up in the UI somewhere
ui <- fluidpage(loadEChartsLibrary(),
tags$div(id="test", style="width:50%;height:400px;"),
deliverChart(div_id = "test"), downloadButton("downloadData", "Download Data"))  

server <-  function(input, output) {
output$test <- renderBarChart(div_id = "test", grid_left = '1%', direction = "vertical",               
data = dat)

output$downloadData <- reactive({
output$downloadData <- downloadHandler(
filename = function() {
  paste("Image.png", sep="") #just the file name you want to have as default
},
content = function(file) {
  write.csv(DataTable, file)
)
}

I don't have your actual code so this may not work as a plug and play solution, but I believe this is the right direction. 我没有您的实际代码,因此这可能无法作为即插即用的解决方案,但我相信这是正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM