Cannot add text to dataframe in downloadHandler

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
Piyali

2 Likes

Watching this thread. I'm having the same issue with more or less identical code, and having tried the same alternatives. Behavior is consistent across RStudio browser, running in RStudio using a desktop browser, and on Shiny Server. (recent R and Shiny)