I am currently developping a Shiny app that lets a user import a data set, performs analysis on it and then displays the results. I would like the user to be able to download the R script used to perform the calculations, for reproductibility purposes. In order to do this, I would like to print the imported data set directly into the script, and here's how I was thinking to do it:
library(shiny)
df <- data.frame("time" = c(0,1,2,3), "conc" = c(0,0.1,0.2,0.3))
ui <- fixedPage(
fixedRow(
downloadButton("download_button", label = "Download")
)
)
server <- function(input, output, session){
output$download_button <- downloadHandler(
filename = function(){
paste0("script_", Sys.Date(), ".R")
},
content = function(file) {
writeLines(paste("df <- ", df), file)
}
)
}
shinyApp(ui,server)
The issue is that the result file looks like this:
df <- c(0, 1, 2, 3)
df <- c(0, 0.1, 0.2, 0.3)
And I want it to look like this (or at least something similar):
df <- data.frame("time" = c(0,1,2,3), "conc" = c(0,0.1,0.2,0.3))
Is it even possible?
Thank you for your time.
dput()in yourwriteLinesstatement for a more descriptive look atdf? Instead of justdftrydput(df)?dput(df, control = "niceNames")capture.outputin yourpaste(). When you usedputthe result is actually adata.frame--- you need to have this as a string when you usepaste()