Cannot add text to dataframe in downloadHandler - r

I am trying to add source information to a downloaded table which contains a dataframe. I am using the download file handler to download the file. I have implemented downloading of just the dataframe without appending additional text and that works fine. On attempting to add text with the table, I get the csv formatted download file but the source information is missing and the last row has NA in the 1st and 2nd column respectively. Rest of the last row is empty.
Here is my code for the download handler that do not produce the appended text output:
downloadHandler(
filename = function() {
paste("Estimation_", Sys.Date(), ".csv", sep="")
},
content = function(file) {
df <- localTable()
df[nrow(df)+1,] <- NA
df[nrow(df),1] <- "Source: This is the source information for the table"
write.csv(df,
file,
na = "",
row.names=FALSE,
append = TRUE)
},
contentType = "text/csv",
outputArgs = list(label = "Download Table")
)
On the other hand I have tried the same functionality separately that works fine. Here is the code I used in a separate R script that does the same thing.
df = data.frame(hello=rnorm(1:5), world=rnorm(1:5));
df[nrow(df)+1,] <- NA
df[nrow(df),1] <- "Source: This is the source information for the table"
write.csv(df,
file ="abc.csv",
na = "",
row.names=FALSE)
I do not know if the issue is with Shiny in particular or something else. Please note that I am using this code as part of Flexdashboard that runs a RMD file to generate the output.
I have tried several other ways of creating the dataframe like the one below; and then passing to the downloadHandler but no luck.
dlTable <- reactive({
df_results <- localTable()
df_notes <- "Source: This is the source information for the table"
df_to_dl <- rbindlist(list(df_results,df_notes))
return(df_to_dl)
})
Any suggestion and help is highly appreciated.
Thanks

Related

R shiny downloaded data has index added

In R Shiny, the data frame looks like below without the index.
However, after downloading the data, index column is automatically added to the data frame, please see below for illustration. How can I avoid this index being added? Because the downloaded template will be for user to populate the data, ideally would want no unnecessary fields.
My code for the download is listed below.
# UI
downloadButton("download1", "Download Template")
# reactive data frame df1 which contains other input from UI, can edit
# it to be a generic data frame for this to be reproducible
df1 = reactive({tibble(Year=strftime(seq(date_seq1(),
date_seq2(),
by = '1 week'), format = "%G"),
Week=strftime(seq(date_seq1(),
date_seq2(),
by = '1 week'), format = "%V"),
Date=seq(date_seq1(),
date_seq2(),
by = '1 week'),
!!input$urgency1 := "",
!!input$urgency2 := "",
!!input$urgency3 := "")})
# download the data frame
output$download1 <- downloadHandler(
filename = function(){"user_template.csv"},
content = function(file){
write.csv(df1(), file)
}
)
You have to set the option row.names = FALSE in the write.csv function.

Shiny save multiple reactive data frames in one xlsx file R

Im trying to save two data frames in one xlsx file, each one in a different sheet. The thing is that each data frame is a reactive object that depends on the same action button to generate them. The code in the download is like this:
output$downloadtable <- downloadHandler(
filename = function(){
paste("file.csv")
},
content = function(file){
write.xlsx(dataframe1(), file, row.names = FALSE, sheetName = "Hoja1")
write.xlsx(dataframe2(), file, row.names = FALSE, append = TRUE, sheetName = "Hoja2")
}
)
The problem is that I only get in my excel file the "Hoja2" sheet with the second data frame and not the first one... Somebody knows what might be wrong with this?
library(openxlsx)
filename = function() {
"mydata.xlsx"
},
content = function(file) {
g= openxlsx::createWorkbook()
openxlsx::addWorksheet(wb,"Hoja1")
openxlsx::writeData(wb,"Hoja1",dataframe1())
openxlsx::addWorksheet(wb,"Hoja2")
openxlsx::writeData(wb,"Hoja2",dataframe2())
openxlsx::saveWorkbook(g,file)
}

Saving multiple data-frames to single csv file using Shiny downloadHandler

I have an RShiny application where I'm displaying multiple data-frames across different tab-setted panels. I want to be able to write data-frames appearing across each panel to one csv file per panel. I am currently trying to render this using Shiny's downloadHandler option.
Did a little bit of research and found out I could use the sink() function to divert R output to a file.
Here's my attempt at trying to incorporate sink() into one such button on the server.R side
output$downloadDemographic <- downloadHandler(
filename = function() {
paste('Demographics', '.csv', sep='')
},
content = function(file) {
sink("Demographics.csv")
cat('Population Demographics')
write.csv(Population)
cat('Geography Demographics')
write.csv(Geography)
sink()
}
)
where Population and Geography are two data-frames with the same number of columns but different names.
The sink function automatically writes the above dataframes to a csv file 'Demographics.csv' into my working directory but the downloadHandler option on Shiny prompts the user to save the csv file on the fly. Kind of contradicting what I am trying to achieve here. I'm probably missing something or making an obvious mistake, I don't know.
Desired output - https://i.stack.imgur.com/yddrE.jpg
I could render multiple download buttons, write each of the data-frames to multiple csv files but some of my tab setted elements have as many as 8 data-frames and this would make the UI messy.
or
I could coerce multiple data-frames into a single one before writing to a csv file but they are all unequal sized dataframes and formatting them into the desired output would be a huge pain.
Any thoughts on how I could establish this any other way?
Thanks!
Other options:
Write an Excel workbook with one sheet per
dataframe
Zip together multiple csv
files
Here's a sample of both options using four dataframes from the R Datasets Package.
library(shiny)
library(xlsx)
shinyApp(
ui = fluidPage(
downloadButton("downloadExcelSheet", "Download Excel Workbook with Multiple Sheets"),
downloadButton("downloadZippedCSV", "Download zipped csv files")
),
server = function(input, output) {
#### Write an Excel workbook with one sheet per dataframe ####
output$downloadExcelSheet <- downloadHandler(
filename = function() {
"excelWorkbook.xlsx"
},
content = function(file) {
# write workbook and first sheet
write.xlsx(mtcars, file, sheetName = "mtcars", append = FALSE)
# add other sheets for each dataframe
listOtherFiles <- list(iris = iris,
airquality = airquality,
sleep = sleep)
for(i in 1:length(listOtherFiles)) {
write.xlsx(listOtherFiles[i], file,
sheetName = names(listOtherFiles)[i], append = TRUE)
}
}
)
#### Zip together multiple csv files ####
output$downloadZippedCSV <- downloadHandler(
filename = function() {
"zippedCSV.zip"
},
content = function(file) {
# go to temp dir to avoid permission issues
owd <- setwd(tempdir())
on.exit(setwd(owd))
# create list of dataframes and NULL value to store fileNames
listDataFrames <- list(mtcars = mtcars,
iris = iris,
airquality = airquality,
sleep = sleep)
allFileNames <- NULL
# loop through each dataframe
for(i in 1:length(listDataFrames)) {
# write each dataframe as csv and save fileName
fileName <- paste0(names(listDataFrames)[i], ".csv")
write.csv(listDataFrames[1], fileName)
allFileNames <- c(fileName, allFileNames)
}
# write the zip file
zip(file, allFileNames)
}
)
}
)

Exporting R shiny output to multiple sheets single excel.

I have built an app which gives an output data table based on the filter selected.
Suppose I change the filter 10 times and it gives me 10 results.
I need to store the results in 10 sheets in single excel.
For example:
Select filter option: A
Get the Output: Output1
Click on Download button.
Save results as sheetname: test1, excel: sample1.xlsx
Now I Select filter option: B
Get the Output: Output1
Click on Download button.
Save results as sheetname: test2, excel: sample1.xlsx
This is my code
output$downloadData1 <- downloadHandler(
filename = function()
{ paste('Sample','_', Sys.Date(), ".xlsx", sep="") },
content = function(file) {
write.xlsx(makeTable(), file, sheetName =input$sampleselect, row.names = FALSE , append = TRUE)
})
sampleselect is the filter selection and I am also passing this as the sheet name.
currently, this code throws out unique Excel files.
i.e For filter A: it creates an Excel Sample.xlsx and for filter B: it creates an Excel also named Sample.xlsx
Thanks in advance.

Shiny Download File based on File Path

I have a file which i generate in shiny
The user clicks a button and the file should download. However nothing happens
The function export_report generates the excel file and saves it to a location. The function then passes back the file location to the download handler so it will download the file. The problem seems to be that it isnt being returned correctly. I have tested the function (export_report) outside of shiny and it returns everything perfectly so I'm clearly doing something wrong from the shiny perspective.
The file itself is created where it is supposed to be on the server because i can download it within RStudio and see it in the file explorer. Can anyone help
# UI Section
downloadButton("downloadRpt", "Download Report")
# Server Section
output$downloadRpt <- downloadHandler(
filename = function() {
mydf <- report()
dateRange <- input$dates_report
selection <- input$selection
myfile <- export_report (mydf, selection, dateRange)
},
content = function(file) {
file.copy(myfile, file)
}
)
I have seen other examples R Shiny: Download existing file which is what my code is based on
EDIT 1: Adding the export_report function with some fake data to run it
export_report <- function(mydf,selection,dateRange) {
# Template for where the template excel file is stored
myoutputTemplate <- '/home/shiny_tutorials/Save to Database/templates/output_template.xlsx'
start_date <- dateRange[1]
end_date <- dateRange[2]
date_range <- paste(start_date ,end_date, sep = " - " )
# Load workbook the template workbook
wb <- loadWorkbook(myoutputTemplate)
# write to the workbook the data frame
writeWorksheet(wb, mydf, sheet="Details",
startRow=8, startCol=2,
header=FALSE)
# add the the customer the user selected
writeWorksheet(wb, selection, sheet="Details",
startRow=3, startCol=3,
header=FALSE)
# date
writeWorksheet(wb, date_range, sheet="Details",
startRow=5, startCol=3,
header=FALSE)
# Create The file Name
filename <- paste(selection, Sys.Date(), sep = " - ") %>%
paste(.,"xlsx", sep = ".")
# removes the % sign and extra qoutes
filename <- gsub (pattern = '\'|%','', x = filename)
# output directory
myoutput <- paste('/home/shiny_tutorials/Save to Database/output/',
filename, sep = '')
# Save workbook
saveWorkbook(wb, myoutput)
# Return File Path
myoutput
}
To call the function you can use the data below
dateRange <- c("2011-09-23","2016-09-23")
selection = "COMPANY_A"
mydf <- iris
myfile <- export_report(mydf,selection,dateRange)
EDIT 2 I have now managed to get an error out of it. When i cat(myfile) in the filename = function() { section of the code i get the error after the correct file path has been returned
Warning in rep(yes, length.out = length(ans)) :
'x' is NULL so the result will be NULL
Warning: Error in ifelse: replacement has length zero
Stack trace (innermost first):
1: runApp
Error : replacement has length zero
This error is basically because my file path does not get passed to the segment myfile so
if someone can tell me how to get the filepath generated by my function to the server section of the code below, that should fix my problem
content = function(file) {
file.copy(myfile, file)
}
Thank you to everyone who commented and clarified my thinking a bit on how the download handler works.
In the end, i created a new function which split up the export function above
The new function i used is called generate_file() which simply returns the file name
generate_file_name <- function(selection) {
# Create The file Name
filename <- paste(selection, Sys.Date(), sep = " - ") %>%
paste(.,"xlsx", sep = ".")
# removes the % sign and extra qoutes
filename <- gsub (pattern = '\'|%','', x = filename)
# output directory
myoutput <- paste('/home/shiny_tutorials/Save to Database/output/',
filename, sep = '')
# Return File Path
myoutput
}
Then in the server side
output$downloadRpt <- downloadHandler(
filename = function() {
selection <- input$company
generate_file_name(selection)
},
content = function(file) {
mydf <- report()
dateRange <- input$dates_report
selection <- input$company
export_report(mydf,selection,dateRange)
myfile <- generate_file_name(selection)
file.copy(myfile, file)
}
)
This then finds the newly created file and exports it for the user
I just checked your problem with this example code and it worked:
output$downloadData <- downloadHandler(
filename = function() {
data <- mtcars
myfile <- "data.csv"
write.csv(data, myfile)
myfile
},
content = function(file) {
print(file) //prints: [...]\\Local\\Temp\\RtmpEBYDXT\\fileab8c003878.csv
file.copy(file, file)
}
)
myfile is the filename of the downloaded file. You cannot use it in file.copy as input, this variable is out of scope. It seems that R creates a temp file name (see the print()).
Try to use the filename function to define your path or a custom file name, and the write.csv in the content part. Example code:
output$downloadData <- downloadHandler(
filename = function() {
paste(<user_name or date for eg>, ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
I noticed in your comment above, you have asked how the application would generate the correct file when used by multiple users. For this part, you need to use the session.
So if your business logic functions were to come from an R file called foo.R, the server code should look something like:
shinyServer(func = function(input, output, session) {
source("./foo.R", local=TRUE)
......
And this would separate out the session for each user, thereby generating files specific to each, when downloading. Hope this gives you some pointers.

Resources