1

I'd like for end-users to be able to export a .png of a visNetwork graphic built and deployed with Shiny. I'm able to do that by using a visExport line at the end of my renderVisNetwork code, but I don't like the look of the button that gets generated in the output, the button itself gets included on the exported .png, and the quality of the .png itself isn't all that great. I like the look of the button generated by downloadButton in the UI, but I'm doing something wrong with downloadHandler on the server end. While running in the browser, I click the download button, get the download dialog, and am able to save the file, but the file is blank (file size of 318 bytes). Trying as .pdf doesn't work either. Here's my code:

ui.R:

downloadButton('ConDL', label='Download png')

server.R:

# Create network
ConNet <- function(){
    visNetwork(Nodes(), Edges()) %>%
    #all the other stuff to create the network which works fine
}

# Draw visualization - works fine
output$ConNet <- renderVisNetwork({
    ConNet()
})    

# Download png of visualization - no errors displayed in browser or
# console when testing, but exports blank png
output$ConDL <- downloadHandler(
    filename="Con.png",
    content= function(file) {
        png(file)
        ConNet()
        dev.off()
    }
)

I've followed the advice given here about generating the object with a function instead of a reactive: Downloading png from Shiny (R). Turning the preceding reactives that feed into ConNet to functions was also ineffective. I am having the app open in a browser (Firefox) rather than running through the preview pane. Trying print(ConNet()) in the content section doesn't help, either, so I'm not sure what I'm missing.

Bonus if I can get the .png to exclude the navigation buttons produced with visInteraction when I create the network - they're useful for the interactive part, but clutter a static graphic.

Community
  • 1
  • 1
bcarothers
  • 824
  • 8
  • 19

1 Answers1

1

I don't understand your problem. This could help you to download network from shiny API

B.Gees
  • 1,125
  • 2
  • 11
  • 28
  • Yeah, I'd stumbled upon that link a while ago and ultimately settled on that solution. The exported graphic is indeed a much higher quality, and the code can be modified to add titles that may not be needed in the interactive version but useful in a download, subtract navigation icons that wouldn't be needed in the download version, etc. The only drawback is that I still get the little "Export as png" button in the .png itself, but that's cropped out easily enough with other software. – bcarothers Jun 01 '17 at 14:48
  • I have found a very simple solution to export network with higher quality. If you save your network into html page, you can open this file. After that, "right click" and "save image as...". Amazing as it may sound the resolution is better that visExport O_O. – B.Gees Jun 01 '17 at 15:46
  • Hah! This ^^^^ is the real answer. Heck, I don't even have to save into html first - right clicking on the app itself when it's running works just as well and removes all the extraneous stuff all in one step. One line of code in the UI to instruct the end-user to right click to download the image sure beats the 60 lines or so on the server side to set everything up. Thanks! – bcarothers Jun 02 '17 at 14:49