将图像从剪贴板复制并粘贴到Rmarkdown/.rmd代码 [英] Copy and paste an image from clipboard to Rmarkdown / .rmd code

查看:888
本文介绍了将图像从剪贴板复制并粘贴到Rmarkdown/.rmd代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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)

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

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.

推荐答案

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

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

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

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

演练:

基本上,我从这里开始:

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')

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

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.

对于左侧",我使用library(rstudioapi). 我把当前的

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

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

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

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

这篇关于将图像从剪贴板复制并粘贴到Rmarkdown/.rmd代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆