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

后端 未结 1 332
轮回少年
轮回少年 2021-01-12 00:49

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

1条回答
  •  执笔经年
    2021-01-12 01:19

    I wrote a little RStudio addin and shared it here: https://github.com/Timag/imageclipr. Edit: It works now without Python across plattforms (Mac, Windows, Linux), thanks to great contributions.

    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/.

    0 讨论(0)
提交回复
热议问题