Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save image as PNG in Shiny with a download button #48

Closed
sumanthmutte opened this issue Sep 22, 2022 · 2 comments
Closed

Save image as PNG in Shiny with a download button #48

sumanthmutte opened this issue Sep 22, 2022 · 2 comments

Comments

@sumanthmutte
Copy link

Hi @swsoyee ,
I have been trying to export the image generated through m_png() through a download button in Shiny, but I am not able to get that work. I have tried png-dev.off(), png::writePNG and also htmlWidgets-webshot but none of them worked for me. They work normally in an R environment but not in a shiny app download button. Here is the corresponding code from the server section of my app.R:

expression <- {
  r3dmol( viewer_spec = m_viewer_spec(cartoonQuality = 10, lowerZoomLimit = 50, upperZoomLimit = 350) ) %>%
    # Add model to scene
    m_add_model(data = pdb_file, format = "pdb") %>%
    # Zoom to encompass the whole scene
    m_zoom_to() %>%
    # Set style of structures
    m_set_style(style = m_style_cartoon(color = "#00cc96")) %>%
    # Set style of specific selection (selecting by secondary)
    m_set_style( sel = m_sel(ss = "s"), style = m_style_cartoon(color = "#636efa", arrows = TRUE) ) %>%
    # Style the alpha helix
    m_set_style( sel = m_sel(ss = "h"), style = m_style_cartoon(color = "#ff7f0e") ) 
}

output$pdb <- renderR3dmol(expression)

data2 <- reactiveValues()

data2$imageToSave <- renderImage({
  expression %>% m_png()
  })

output$downloadImage <- downloadHandler(
  filename = "AuxPhosImage.png",
  content = function(file){ 
    png::writePNG(data2$imageToSave)
  }
  )

I am using 'pdb' and 'downloadImage' in the 'ui' part as fluidRow(column(6, align="center", downloadButton('downloadImage','Download Image'), r3dmolOutput("pdb")).

I tried the same procedure for to save a ggplot object using ggsave which worked perfectly. But I am not able to get this working. One difference that I found is that this png from r3dmol is rendered into the 'viewer' but not the 'plot' section, for which the reason the png saving options are not working I guess, I might be wrong.

Any help is highly appreciated.

Thanks, Sumanth.

@swsoyee
Copy link
Owner

swsoyee commented Sep 22, 2022

I found is that this png from r3dmol is rendered into the 'viewer' but not the 'plot' section

@sumanthmutte Yes, actually the m_png() function do not create a image, it just convert the widget to a base64 png.
The easiest way to do this is to write a help text that reminds the user that they can right-click to save the results of m_png()

@sumanthmutte
Copy link
Author

Sure. Thank you @swsoyee .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants