I want to append a new line in a data frame and save it as csv file in R shiny.
df <- data.frame(x = 1:5, y = 1:5)
fn <- "Result.csv"
header <- "this is a new line"
writeLines(header, fn)
## Shiny server file------------
output$OutputFile <- downloadHandler(
content <- function() {
write.table(x = df,
file = fn,
sep = ",",
append = TRUE,
quote = FALSE,
col.names = TRUE,
row.names = FALSE
)
}
)
But it did not work, is it possible to use write.table in shiny?
I think it is not possible to use
append = TRUEargument inwrite.table()in case ofshiny::downloadHandler(). I have read a documentation for parameterappendand see:If I understand this correct,
write.tablewithappend = TRUElooks for the file with specified name and in specified directory to see if this file exists and if yes - it appends object to the content of this file. However, in case ofshiny::downloadHandler()functions using to write file do not write the files directly to the folder, but creates a downloadable file, which then can be download using the web browser. Below you can see the MRE showing how to usedownloadHandler():However, we can try to create something different to make use of
append = TRUE, but not usingdownloadHandler. We can do something like this:write.tablewith parameterappendset toTRUE):