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)
Related
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)
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 a simple shiny app which downloads a .txt file. My problem is that I want to be able to set the filename from the app and download it as filename.txt for example and not "download_button" as it is now.
library(shiny)
text=c("Line1", "Line2","Line3")
ui <- fluidPage(
sidebarPanel(
h4("Title"),
p("Subtitle",
br(),text[1],
br(),text[2],
br(),text[3]),
downloadButton("download_button", label = "Download")
)
)
server <- function(input, output, session){
output$download_button <- downloadHandler(
filename = function(){
paste("data-", Sys.Date(), ".txt", sep = "")
},
content = function(file) {
writeLines(paste(text, collapse = ", "), file)
# write.table(paste(text,collapse=", "), file,col.names=FALSE)
}
)
}
shinyApp(ui,server)
Hopefully this addresses your issue. I just included a text input with the default value set to your filename as above, and then set the filename in the download function to that text input.
text=c("Line1", "Line2","Line3")
ui <- fluidPage(
sidebarPanel(
h4("Title"),
p("Subtitle",
br(),text[1],
br(),text[2],
br(),text[3]),
textInput("filename", "Input a name for the file", value = paste0("data-", Sys.Date(),".txt")),
downloadButton("download_button", label = "Download")
)
)
server <- function(input, output, session){
output$download_button <- downloadHandler(
filename = function(){
input$filename
},
content = function(file) {
writeLines(paste(text, collapse = ", "), file)
}
)
}
shinyApp(ui,server)
I am looking for a way to download text displayed on an app by generation a .txt file. Here is my attempt, with no success unfortunately:
library(shiny)
ui <- fluidPage(
sidebarPanel(
h4("Title"),
p("Subtitle",
br(),"Line1",
br(),"Line2",
br(),"Line3"),
downloadButton("Download Metadata", label = "Download")
)
)
server <- function(input, output, session){
output$downlaodData <- downloadHandler(
filename = function(){
paste("data-", Sys.Date(), ".txt", sep = "")
},
content = function(file) {
write.txt(data, file)
}
)
Thank you for your help
You cannot write text that is displayed on your page like that. You could download text stored as data or as user input. There are also some issues in your code:
data is not defined, so you are not specifying what should be downloaded
write.txt is not a function, use write.table instead
The downloadbutton and the downloadHandler should have the same id.
Working example
library(shiny)
text=c("Line1", "Line2","Line3")
ui <- fluidPage(
sidebarPanel(
h4("Title"),
p("Subtitle",
br(),text[1],
br(),text[2],
br(),text[3]),
downloadButton("download_button", label = "Download")
)
)
server <- function(input, output, session){
output$download_button <- downloadHandler(
filename = function(){
paste("data-", Sys.Date(), ".txt", sep = "")
},
content = function(file) {
writeLines(paste(text, collapse = ", "), file)
# write.table(paste(text,collapse=", "), file,col.names=FALSE)
}
)
}
shinyApp(ui,server)
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"
)
})