Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom html with download data button in Shiny

I'm wondering how to make the Shiny downloadHandler work with a custom html UI.

In my index.html I have the following:

<a id="downloadproject" class="shiny-download-link shiny-bound-output">export</a>

And in the server.R I have:

output$downloadproject <- downloadHandler(
    filename = "test.csv",
    content = function(file) {
        test_data <- c(1,2,3,4,5,6,7)
        write.csv(test_data, file)
    }
 )

However, I can't get it working. I've noticed inspecting the source on the demo page: http://shiny.rstudio.com/gallery/file-download.html that the link there points to a resource:

<a id="downloadData" class="btn shiny-download-link shiny-bound-output" href="session/58c63083742fd00d75ac37732eb224bc/download/downloadData?w=299e8cd2e7b56a2507a31ddbe72446fd2ce5d51f5940ea0a" target="_blank">
      <i class="fa fa-download"></i>
      Download
</a>

However, I guess that this it to be set by the downloadHandler from the server side. My a-tag in however does not get any href at all. Is what I'm looking to do even possible? Am I'm making some mistake here? Any ideas on how to fix this would be much appreciated.

like image 597
Johan Avatar asked Sep 30 '22 04:09

Johan


1 Answers

I think the A tag is being modified by some javascript. If you just download the HTML source for that (which is in an iframe wrapper) then you don't see the long href.

So I further think your custom HTML UI doesn't include the right javascript that adjusts the tag.

I think its done by downloadLinkOutputBinding in shiny.js, line 1402 or thereabouts.

Those demos load a lot of js and css, some of it is clearly crucial!

like image 77
Spacedman Avatar answered Oct 05 '22 09:10

Spacedman