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"
)
})
Related
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)
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)
I wonderif there is a way to download 2 dataframes in the same excel file but in different sheet via shiny app.
library(shiny)
library(xlsx)
ui <- shinyUI(fluidPage(
titlePanel("Testing File upload"),
sidebarLayout(
sidebarPanel(
downloadButton("dl","Export in Excel")
),
mainPanel(
)
)
))
server <- shinyServer(function(input, output) {
output$dl <- downloadHandler(
filename = function() {
paste0("df_dmodel", "_Table", ".xls")
},
content = function(file){
tbl<-iris
tbl2<-mtcars
write.xlsx(tbl,tbl2 file,
sheetName = "Sheet1", row.names = FALSE)
}
)
})
shinyApp(ui = ui, server = server)
try changing your server code to this. Also, remember to open the app in your browser and not just the rstudio viewer (assuming your are using rstudio). Hope this helps!
server <- shinyServer(function(input, output) {
output$dl <- downloadHandler(
filename = function() {
paste0("df_dmodel", "_Table", ".xlsx")
},
content = function(file){
tbl<-iris
tbl2<-mtcars
sheets <- mget(ls(pattern = "tbl")) # getting all objects in your environment with tbl in the name
names(sheets) <- paste0("sheet", seq_len(length(sheets))) # changing the names in your list
writexl::write_xlsx(sheets, path = file) # saving the file
}
)
})
An alternative to Andrew's answer using write.xlsx from openxlsx with a list of dataframes.
library(shiny)
library(openxlsx)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
downloadButton("dl","Export in Excel")
),
mainPanel(
)
)
))
server <- shinyServer(function(input, output) {
output$dl <- downloadHandler(
filename = function() {
"test.xlsx"
},
content = function(filename){
df_list <- list(iris=iris, mtcars=mtcars)
write.xlsx(x = df_list , file = filename, row.names = FALSE)
}
)
})
shinyApp(ui = ui, server = server)
I have multiple tables in reactive objects in my shiny app (RA_s,Per), and I download each separately. Now, I'm trying to download one zip file containing all of this tables using a download button.
Here is my code, I don't know how to complete downloadHandler function. I want to download a zip file which contain 2 csv files RA_s and Per.
Code
shinyServer(function(input, output) {
RA_s <- reactive({
iris
})
Per <- reactive({
sepal1 <- RA_s()["Sepal_Length"]
sepal2 <- RA_s()["Sepal_Width"]
value = sepal1*sepal2
c = cbind(RA_s(),value)
})
output$downloadData <- downloadHandler(
filename = function() {
paste0("output", ".zip")
},
content = function(file) {
...
})
})
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Download", tabName = "d")
)
body<- dashboardBody(
tabItems(
tabItem(tabName = "d",
downloadButton('downloadData', 'Download')
)
)
dashboardPage(
dashboardHeader(title = "Valo"),
sidebar,
body
)
You can first save the zip files, and then zip them, as follows:
library(shiny)
server<- shinyServer(function(input, output) {
RA_s <- reactive({
iris
print(iris)
})
Per <- reactive({
sepal1 <- RA_s()["Sepal.Length"]
sepal2 <- RA_s()["Sepal.Width"]
value = sepal1*sepal2
c = cbind(RA_s(),value)
})
output$downloadData <- downloadHandler(
filename = 'two_csvs.zip',
content = function(fname) {
write.csv(RA_s(), file = "csv1.csv", sep =",")
write.csv(Per(), file = "csv2.csv", sep =",")
zip(zipfile=fname, files=c("csv1.csv","csv2.csv"))
},
contentType = "application/zip"
)
})
ui<- shinyUI(
downloadButton('downloadData', 'Download')
)
shinyApp(ui,server)
You can specify a subdirectory of course, to keep your directory clean. Hope this helps!
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)}
)})