Download Table in RMarkdown

I am attempting to add a simple download button to an Rmarkdown report but it seems like the button downloads the entire report and not the table of interest. For example, in my RMarkdown I have:

shiny::downloadHandler(
  filename = "mtcars.csv",
  
  content = function(file) {
    write.csv(mtcars, file)
  }
)

with a YAML header of:

runtime: shiny
output: 
  html_document:
    number_sections: true

The handler generates a button but the button does not download the mtcars data, it downloads the entire html report. I'm running rmarkdown::render to generate the report.

Any assistance on adding download buttons to RMarkdown reports would be greatly appreciated.

Don't know if this is related but with shiny document, you should use rmarkdown::run

or the Knit button...

If you want further debug, can you share a full minimal reproducible example and not only pieces of code ?
Thank you!

Ah, right - using shiny functions, you can't generate a standalone html output. I found a solution using xfun::embed_file, it's not as elegant but it works.

Thanks!

2 Likes

I find it elegant ! That is a perfect solution to include a file in a static file. If you want a button rather than a link, you can try this :package:

And

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

2 Likes

Oh - awesome. The downloadthis package looks to solve this issue completely. I implemented some custom CSS to change the xfun::embed_file link into a button but it requires the extra step of saving out a file first to pass into the function. downloadthis looks like it works directly with data frames in R by wrapping the saving/button creation into one step, which is perfect.

Thanks for the recommendation!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.