renaming the output name when saving file using downloadButton removes ".csv" extension - r

In the code below, I am just using simple downloadButton() example. It works just fine. But, Why changing the output file name after the "Save File" window pops up, will save the file with no extension (removes the .csv)?
library(shiny)
downloadUI <- function(id){
ns <- NS(id)
downloadButton(ns("downloadData"), "Download")
}
downloadServer <- function(id){
moduleServer(id,
function(input, output, session){
# Our dataset
data <- iris
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
)
}
ui <- fluidPage(
downloadUI("irisDownload")
)
server <- function(input, output, session) {
downloadServer("irisDownload")
}
shinyApp(ui, server)

Related

Download file as word document in a shiny app

Is it possible to download an empty word document via shiny app?
ui <- fluidPage(
downloadButton("downloadData", "Download")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)

Download the generated table in a csv format - R shiny

My shiny app generates a table that I want to make available for download in a csv format.
ui = fluidPage( ...
tableOutput("contents"),
downloadButton("download", "Download results in csv format")
)
server <- function( input, output, session ) {
output$contents <- renderTable( ... )
output$download <- downloadHandler(
filename = function() {
paste(contents, ".csv", sep = "")
},
content = function(file) {
write.csv(contents(), file, row.names = FALSE)
}
)
I understand that I have to create a reactive object, but the renderTable itself uses another reactive object (uploaded dataset), so it looks like I need to nest one reactive object into another, and it does not seem to work. Will appreciate any help. Thank you!
EDIT: Added an example using renderTable instead of renderDataTable, as requested by the question and in the comment section:
Here is an example using the iris dataset. I also added a table from the DT package. You should not paste the data in the filename function, only in the in the write.csv function.
```{r}
library(shinydashboard)
library(dplyr)
library(DT)
```
setosa <- filter(iris, Species == "setosa")
ui = fluidPage(
downloadButton("download", "Download results in csv format") ,
column(12,
DT::dataTableOutput ("content"),
style = " overflow-y: scroll;overflow-x: scroll;")
)
server <- function(input, output, session) {
output$content <-
renderDataTable(head(setosa))
output$download <-
downloadHandler(
filename = function () {
paste("MyData.csv", sep = "")
},
content = function(file) {
write.csv(content, file)
}
)
}
shinyApp(ui, server)
```
Using renderTable instead of renderDataTable
library(shiny)
library(dplyr)
library(DT)
setosa <- filter(iris, Species == "setosa")
ui = fluidPage(
downloadButton("download", "Download results in csv format"),
tableOutput("table")
)
server <- function(input, output, session) {
data <- data.frame(setosa)
output$table <-
renderTable(data)
output$download <-
downloadHandler(
filename = function () {
paste("MyData.csv", sep = "")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)

Export user input and table out put to one Excel file with Shiny

I have written an app allowing users to provide some inputs. The app will call a function to do some calculations and generate an output in table format.
I would like to add a button that allows users to download both the inputs and outputs into an Excel spreadsheet (with two tabs)
Below is a simplified version of the code where I want to download the inputs and the example table. I have tried the following code but failed:
library(shiny)
library(openxlsx)
somefunction <- function() {
data.frame(text = c("sample1","sample2"))}
server <- function(input, output, session) {
dataReactive <- reactive({
data.frame(text = c(input$text1, input$text2, input$text3))
})
observeEvent(input$goButton,{
output$exampleTable <- DT::renderDataTable({somefunction()})
})
output$downloadExcelSheet <- downloadHandler(
filename = function() {
paste("result",Sys.Date(), ".xlsx",sep="")
},
content = function(file) {
write.xlsx(list(dataReactive(),exampleTable), file)
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("text1","Text 1:",value="Input 1"),
textInput("text2","Text 2:",value="Input 2"),
actionButton("goButton", "Calculate"),
downloadButton("downloadExcelSheet", "Download Data")
),
mainPanel(
DT::dataTableOutput("exampleTable")
)
)
)
shinyApp(ui = ui, server = server)
server <- function(input, output, session) {
dataReactive <- reactive({
data.frame(text = c(input$text1, input$text2, input$text3))
})
data <- reactiveValues()
observeEvent(input$goButton,{
output$exampleTable <- DT::renderDataTable({
data$df <- somefunction()
})
})
output$downloadExcelSheet <- downloadHandler(
filename = function() {
paste("result",Sys.Date(), ".xlsx",sep="")
},
content = function(file) {
write.xlsx(list(dataReactive(),data$df), file)
})
}
It's better to move data$df <- somefunction() to observeEvent and move DT::renderDataTable outside observeEvent like so
observeEvent(input$goButton,{
data$df <- somefunction()
})
output$exampleTable <- DT::renderDataTable({data$df})
Use reactiveValues as an intermediate state to save variables and in order to reuse them later.

How to download data directory from www directory of shiny

I would like to provide a link to the user to download a list of test data files stored in WWW directory of my shiny app. I tried something displayed below.
library(shiny)
# server.R
server <- function(input, output) {
output$downloadData <- downloadHandler(
filename = 'data',
content = function(fname) {
testdata
})
}
# ui.R
ui <- shinyUI(fluidPage(
titlePanel('Downloading Data'),
sidebarLayout(
sidebarPanel(
downloadLink ('downloadData', 'Download')
),
mainPanel()
)
)
)
shinyApp(ui = ui, server = server)
However, it does not work. How to do.
There is main question how you read data? how you get testdata?
for example if you have data.csv in your www
shinyServer(function(input, output) {
testdata=read.csv2('www\\data.csv',header = F)
output$downloadData <- downloadHandler(
filename =function() { 'data.csv'},
content = function(file){
fname <- paste(file,"csv",sep=".")
write.csv2(testdata,fname)
file.rename(fname,file)
}
)
})
for me work only in browser
You can also try to create zip of all files( cant test zip not work on my R)
shinyServer(function(input, output) {
wd=getwd()
testdata=c("data.csv","data1.csv")
testdata_full_path=path.expand(paste0(wd,"\\www\\",testdata))
output$downloadData <- downloadHandler(
filename = 'data.zip',
content = function(fname) {
tmpdir <- tempdir()
lapply(testdata_full_path,function(i) file.copy(i,tmpdir))
setwd(tmpdir)
zip('data.zip',files= testdata)
setwd(wd)
unlink(tmpdir)
},
contentType = "application/zip"
)
})

Shiny (R): Download selection in selectInput to local machine

I am trying to create a shiny app where users can view a list of files in the directory, select one of the files, and then download it to their computer. I may be over-complicating this, but I can't seem to find a solution.
ui.R
filenames <- list.files(path=".",pattern="\\.txt")
shinyUI(navbarPage("Download page",
tabPanel("Download",
sidebarLayout(
sidebarPanel(
selectInput("filenames", "Select the file you want to download:", filenames), downloadButton('downloadData', 'Download')
),
mainPanel(
p("Preview of sheet."),
tableOutput('table')
)))))
server.R
library(shiny)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$filenames,
filenames)
})
output$table <- renderTable({
datasetInput()
})
output$downloadData <- downloadHandler(
filename = function() { paste(input$dataset, '.csv', sep='') },
content = function(file) {
write.csv(datasetInput(), file)
}
)})
When I run the app, I can view the list of files in my directory, but the download function does not result in the selected file being downloaded.
Assuming a bunch of 'csv' files in your working directory the code below will list and preview the 'csv' files and download the selected file to your desired directory.
ui.R
shinyUI(navbarPage("Download page",
tabPanel("Download",
sidebarLayout(
sidebarPanel(
selectInput("filenames", "Select the file you want to download:", list.files(pattern = '.csv')),
downloadButton('downloadData', 'Download')),
mainPanel(
p("Preview of sheet."),
tableOutput('table')
)))))
server.R
library(shiny)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$filenames,
filenames)
})
output$table <- renderTable({
read.csv(input$filenames, header=TRUE)
})
output$downloadData <- downloadHandler(
filename = function() {input$filenames},
content = function(file) {write.csv(read.csv(input$filenames, header=TRUE),file)}
)})

Resources