Copy and paste an image from clipboard to Rmarkdown / .rmd code

夙愿已清 提交于 2019-12-19 22:27:34

问题


How can I copy an image to the clipboard and then directly insert/paste it in an .Rmd file?

See also here: https://github.com/rstudio/rstudio/issues/4038

Spoiler: I wanted to post this question and then decided to give it a shot myself. (It is a bit weird asking a question and answering it yourself, but when i read this i felt motivated enough :) https://stackoverflow.com/help/self-answer)

So far i got a workaround via python (see answer below), but it works and saved me quite some silly work. So i decided i might not be the only one googling that question and share my result.


回答1:


As i spoilered in the question i found a workaround. It still requires improvement as it uses Python in the backend, so any suggestions for a R only integration are welcome. (Maybe the python part is written in C and can be also wrapped in R).

I wrote a little RStudio addin and shared it here: https://github.com/Timag/imageclipr.

Walkthrough:

Basically i started here: How do I read a jpg or png from the windows clipboard in python and vice versa?.

I use this code to save the image:

from PIL import ImageGrab
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')

Then i wrote a wrapper with library(reticulate) to run the python code from R. Thats what covers the "right part" of the gif: To copy the image to a directory.

For the "left part" I use library(rstudioapi). I take the current

  • file: getActiveDocumentContext()$id
  • line: getActiveDocumentContext()$selection[[1]]$range$start
  • directory: getActiveDocumentContext()$path

And then wrote a small RStudio addin: https://rstudio.github.io/rstudioaddins/.



来源:https://stackoverflow.com/questions/55541345/copy-and-paste-an-image-from-clipboard-to-rmarkdown-rmd-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!