swsoyee / r3dmol

🧬 An R package for visualizing molecular data in 3D
https://swsoyee.github.io/r3dmol/
Other
89 stars 4 forks source link

Easiest way to export svg/pdf/png? #54

Closed bananabenana closed 1 month ago

bananabenana commented 1 month ago

Hi,

Really love this tool and have made some awesome html-based figures. Do you have any guidance on the best way to export highres svg/pdf/png figures from this? Or natively from the r3dmol object?

# Load pdb file
pdb <- bio3d::read.pdb("I7AVL3.pdb")

# Generate r3dmol
ribbon_structure <- r3dmol() %>% 
  m_add_models(m_bio3d(pdb)) %>% 
  m_set_style(m_style_cartoon(color = "white")) %>% 
  m_set_background_color(gray(0.9)) %>% 
  m_add_outline()

# Save as html
htmltools::save_html(ribbon_structure, file = "ribbon_structure.html")

Thanks

swsoyee commented 1 month ago

Hi, you could use function m_png to export a png figure https://swsoyee.github.io/r3dmol/reference/m_png.html

bananabenana commented 1 month ago

Hi, thanks for your fast response. Apologies for not getting this yet.

So if I run the following:

ribbon_structure_png <- r3dmol() %>% 
  m_add_models(m_bio3d(pdb)) %>% 
  m_set_style(m_style_cartoon(color = "white")) %>% 
  m_set_background_color(gray(0.9)) %>% 
  m_add_outline()
  m_png()

How do I then save this object as a .png to disk and control resolution etc? I am using RStudio.

swsoyee commented 1 month ago

Sorry, it didn't support resolution and save to disk options. https://github.com/swsoyee/r3dmol/blob/0eefc1c2c430fff0122ae71ae8c9cc4e8912190c/inst/htmlwidgets/r3dmol.js#L200-L209

You could set the size of viewer (https://swsoyee.github.io/r3dmol/reference/init.html), right click and save the image directly.

bananabenana commented 1 month ago

Okay great, thanks for your help! Also once again, amazing software which we are using in an upcoming paper!