【发布时间】:2019-04-29 03:47:24
【问题描述】:
我尝试使用此处Download filtered data from renderDataTable() in Shiny 和此处R - Download Filtered Datatable 提供的代码语法。就我而言,我使用的是自己的 .csv 文件,而不是标准的“mtcars”数据。 出于某种原因,如果要下载该文件(我在浏览器中打开它),我无法找到该文件。 代码如下:
library(shiny)
library(ggplot2)
library(DT)
library(readr)
tbl <- read.csv(file.choose(new = FALSE), header = TRUE, sep = ",", stringsAsFactors=TRUE)
# Define UI -------
ui <- navbarPage(
title = "Data Table Options",
tabPanel("Lot Dataset",
DT::dataTableOutput("dt"), #datatable
div(h3("Download"), style = "color:blue"),
helpText(" Select the download format"),
radioButtons("type", "Format type:",
choices = c("Excel (CSV)", "Text (Space Separated)", "Doc")),
br(),
helpText(" Click on the download button to download the Lot Dataset"),
p("Below are the row indices of the data."),
verbatimTextOutput("filtered_row"),
br(),
helpText(" Click on the download button to download the Lot Dataset"),
downloadButton("download_filtered", "Download Filtered Data"),
br()
)
)
以及带有下载处理程序的服务器功能:
server <- function(input, output) {
thedata <- reactive({datatable(tbl, filter = "top",options = list(pageLength = 25))})
output$dt <- DT::renderDataTable({
thedata()
})
#bottom panel with row indices
output$filtered_row <-
renderPrint({
input[["dt_rows_all"]]
})
#file extension for download
fileext <- reactive({
switch(input$type,
"Excel (CSV)" = "csv", "Text" = "txt", "Doc" = "doc")
})
#downloadHandler() for file download of Lot Dataset
output$download_filtered <- downloadHandler(
filename = function() {
file_name <- paste("MLdataset_test", fileext(), sep=".") #filename
},
content = function(file) {
write.csv(thedata()[input[["dt_rows_all"]], ],
file)
}
)
}
# Run the app ----
shinyApp(ui = ui, server = server)
我希望能够下载过滤后的数据表,但由于某种原因,当我要下载时找不到文件
每次我尝试下载它时,控制台都会出现以下错误:
Warning: Error in [: incorrect number of dimensions
[No stack trace available]
.csv文件的维度:
暗淡(tbl) [1] 19100 56
我真的很感激任何帮助,我已经尝试修复这个问题几个小时但没有成功!
【问题讨论】:
标签: r datatable download shiny