Export/Download Data from R
https://data-flair.training/blogs/exporting-data-from-r/
https://mastering-shiny.org/action-transfer.html
content 의 인수인 file은 temp폴더에 생성한 파일을 나타냄
즉, temp폴더에 temp파일에 data를 넣고, filename을 부여하여 파일을 생성함.
RStudio viewer에서는 filename=function() 의 결과가 제대로 나오지 않아,
항상 browser를 사용하도록 runApp(launch.browser=T) 를 사용했다.
pacman::p_load("tidyverse","vroom")
ui <- fluidPage(
selectInput(inputId="dataset", "Pick a dataset", ls("package:datasets")),
tableOutput(outputId="preview"),
hr(),
radioButtons(inputId="format", "",inline=T,
c('CSV(comma)'="csv",
'FST(rdata)'='fst')),
downloadButton(outputId="download1", "DownButton", icon="download"),
downloadLink(outputId="download2", "DownLink", class=("fa fa-download"))
)
server <- function(input, output, session) {
data <- reactive({
outDf <- get(input$dataset, "package:datasets")
if (!is.data.frame(outDf)) {
validate(paste0("'", input$dataset, "' is not a data frame"))
}
return(outDf)
})
output$preview <- renderTable({
data() %>% head(6)
})
output$download1 <- downloadHandler(
#output$download2 <- downloadHandler(
filename = function() {
fileNm <- str_c(input$dataset,"_",Sys.Date(), switch(input$format, csv='.csv', fst='.fst') )
return(fileNm)
},
content = function(file) {
# out <- write.csv(data(), file)
data <- data()
out <- switch(input$format,
csv = write.csv(data, file),
fst = write_fst(data, file)
)
## print(data()) # for TEST whether data.frame
return(out)
}
)
}
shinyApp(ui, server) %>% runApp(launch.browser=T)
https://shiny.rstudio.com/gallery/file-download.html
https://shiny.rstudio.com/gallery/radiant.html
https://shiny.rstudio.com/gallery/download-knitr-reports.html
https://shiny.rstudio.com/gallery/generating-reports.html
download file zip : https://stackoverflow.com/questions/26881368/shiny-download-zip-archive
서버(로컬) 파일 다운로드
https://stackoverflow.com/questions/37241747/r-shiny-detect-and-delete-local-files-after-download
ui <- fluidPage(
downloadButton(outputId="download1", "DownButton", icon="download"),
)
server <- function(input, output, session) {
output$download1 <- downloadHandler(
filename = function() {
fileNm <- glue("output","_",as.numeric(Sys.Date()),".csv")
return(fileNm)
},
content = function(file) {
data <- path.expand("~/Documents/test.csv")
out <- file.copy(data, file)
return(out)
}
)
}
shinyApp(ui, server) %>% runApp(launch.browser=T)
다운로드가 오래걸리는 경우, 로딩메세지 활용
https://stackoverflow.com/questions/54277922/loading-message-for-slow-downloadhandler
output$downloadData <- downloadHandler(
filename = function() { str_c(input$dataset, ".csv", sep = "") },
content = function(file) {
write.csv(datasetInput(), file, row.names=FALSE)
}
)
output$downloadData <- downloadHandler(
filename = function() { str_c(input$dataset, ".csv", sep = "") },
content = function(file) {
shiny::withProgress( message=str_c("Downloading",input$dataset," Data"), value=0, {
shiny::incProgress(1/10)
Sys.sleep(1)
shiny::incProgress(5/10)
\t\t write.csv(datasetInput(), file, row.names=FALSE)
})
}
)
데이터 NULL 인 경우, 경고창 띄우기
https://community.rstudio.com/t/how-to-stop-download-in-downloadhandler/8268/2
ui <- fluidPage(
shinyjs::useShinyjs(),
actionButton(inputId="btn_download", "Download", icon=icon("download")),
downloadButton(outputId="download1", "", style="visibility: hidden;")
)
server <- function(input, output) {
data <- mtcars
#data <- NULL
observeEvent(input$btn_download, {
if (is.null(data)) {
#showModal( modalDialog(title='Info', p('No data!!!')))
alert("No data!!!")
} else {
shinyjs::runjs("document.getElementById('download1').click();")
}
})
output$download1 <- downloadHandler(
filename = function() { glue("output","_",as.numeric(Sys.Date()),".csv") },
content = function(file) {
out <- file.copy(path.expand("~/Documents/test.csv"), file)
# ------ zip for network traffic ------
# zipr(zipfile=file, files=path.expand("~/Documents/test.csv"), include_directories=F)
# out <- file
}
)
}
shinyApp(ui,server)