将图像从剪贴板复制并粘贴到Rmarkdown / .rmd代码

时间:2019-04-05 18:23:25

标签: r markdown r-markdown clipboard

如何将图像复制到剪贴板,然后直接将其插入/粘贴到.Rmd文件中?

另请参见:https://github.com/rstudio/rstudio/issues/4038

enter image description here

剧透:我想发布这个问题,然后决定自己尝试一下。 (问一个问题并自己回答这个问题有点怪异,但是当我读到这篇文章时,我感到很有动力:)https://stackoverflow.com/help/self-answer

到目前为止,我已经通过python找到了一种解决方法(请参见下面的答案),但是它有效并且为我节省了很多愚蠢的工作。因此,我决定我可能不是唯一一个搜索该问题并分享我的结果的人。

1 个答案:

答案 0 :(得分:1)

当我在问题中困扰时,我找到了解决方法。它仍然需要改进,因为它在后端使用Python,因此欢迎任何有关仅R集成的建议。 (也许python部分是用C编写的,也可以用R包装)。

enter image description here

我写了一个RStudio插件,并在这里共享:https://github.com/Timag/imageclipr

演练:

我基本上是从这里开始的:How do I read a jpg or png from the windows clipboard in python and vice versa?

我使用以下代码保存图像:

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

然后我用library(reticulate)编写了一个包装程序,以运行R中的python代码。 那就是gif的“正确部分”的内容:将图像复制到目录中。

对于“左侧”,我使用library(rstudioapi)。 我把当前的

  • 文件:getActiveDocumentContext()$id
  • 行:getActiveDocumentContext()$selection[[1]]$range$start
  • 目录:getActiveDocumentContext()$path

然后编写了一个小的RStudio插件:https://rstudio.github.io/rstudioaddins/

相关问题