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.
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/.
Related
I've encountered a very bizarre problem with my R scripts. I had a bunch saved in a folder, and after reinstalling R (which was itself having some issues), the rather large scripts I had can easily open in R, but appear to have no text on them (this is despite it clearly labeled as an R document in the folder and being 26kb). Yet when I upload my scripts in a message on Slack, it appears perfectly fine.
Here is what my R script looks like presently:
And this is what it should look like:
I'm thinking it has something to do with the way R is reading the text in the script, but I couldn't find any answers online that were helpful. I would greatly appreciate any advice, as I dont' want to have to recreate all of these using Slack of all things...
I figured it out with some tinkering and it was a rather simple fix that mirrored what I thought the issue was. Apparently my RStudio program was set to read the text in CP936 format. I set it to system default:
And viola! My text is now back!
ctrl+R no longer works for executing script lines.
This is not a hardware/keyboard problem.
I have also restarted my PC.
I have tried on a different PC.
I have recently switched from using R to using RStudio. I thought this may have something to do with it, so I opened and resaved the script in RStudio, to no effect.
Furthermore, I have created an R-Project folder and have copied the files, including the script in question, into it. Then I started R-Studio and opened the project.
I would like to post my sessionInfo(), but do not know how to do that without executing the command.
Keep in mind that I only use R for stats purposes. I don't know much about informatics or other types of programming etc., so please try to keep it simple for me. Thanks!
In addition to the solution offered above, in Rstudio, Ctrl + Enter does not work if the chunk is broken in .Rmd files.
For example, if you press CTRL + Enter on the following line (2+2), it won't work:
```{r}
2+2
``
The chunk should end three Backtick characters, not two.
The problem was that the script file (for some reason unkown to me) did not have the correct extension (.R). When I added that to the script file extension, it worked fine again.
i have been trying to implement a tikz chunk in bookdown first by myself, then by following this thread.
unfornutately, i keep running into various errors and incompatibilities, such as this one:
Error in tools::texi2dvi(texf, pdf = !to_svg, clean = TRUE) :
unable to run 'pdflatex' on '.\tikzf702b605920.tex'
(even though by itself tinytex::pdflatex('test.tex') works fine)
on his blog and github Yihui Xie mentioned that it's definitely possible, but i wasn't able to find any working example, so i was hoping if perhaps anyone here could share one, please (or point out what am i doing wrong)
#PaulLemmens, i found what was the problem for me. disclaimer here, i use windows and none of my colleagues who use r on linux/macos have this problem.
issue was arising with knitr when engine=tikz is used in one of the chunks. For it to work installation of imagemagick (https://www.imagemagick.org/script/index.php) and GhostScript (https://www.ghostscript.com/) is needed. as well as that, i had to rewrite knitr (https://github.com/darthaline/knitr) slightly. it's a quick and dirty solution, but it worked for me. on line 281 of R/engine.R the path to imagemagick's convert is hardcoded cause otherwise it seems that windows interprets it as 'convert.exe' from System32 directory (ImageMagick PATH not being recognized with engine = "tikz" in knitr)
RStudio has a wonderful set of skeletons for packages and Rmd documents. But, I'd like to know if it's possible to change the defaults to a "skeleton" of your own design. If, like me, you package your research for yourself/clients, you quickly find yourself deleting and copying the same work over and over.
I suppose there are two related questions here:
Can you change the default package skeleton?
Can you change the default Rmd skeleton?
There is no supported way to do this. However, the skeletons are stored as ordinary files in your filesystem, so there's nothing stopping you from modifying them. For instance, if you're on the Mac, this file provides the default Rmd skeleton:
/Applications/RStudio.app/Contents/Resources/resources/templates/r_markdown_v2.Rmd
On Windows, it's here:
C:\Program Files\RStudio\resources\templates\r_markdown_v2.Rmd
I didn't realize it then, but I was actually looking for a custom format. The details of which are documented extensively on the rmarkdown rstudio site.
http://rmarkdown.rstudio.com/developer_custom_formats.html
I'm writing all my scripts on .R file using R for mac. It is convenient to me because there are colors to highlight the type of commands.
I have a many comments following the # symbol that are useful when I forget about the meaning of my script but they tend to blur my script so that it gets harder to find a given command line.
Is there a way to hide and show these comments ? (Using the programm I'm currently using or another one). What would you suggest as the best program to write R script ?
Thanks a lot !
RStudio supports code folding. You can standardize your comment blocks so that they are recognized as code blocks.
For example, enter this into your RStudio editor
#=======================================================
# this is a comment block
# more comments here
# comments upon comments
and then press Alt+L to fold, and Alt+Shift+L to unfold.
Try RStudio for mac. One of the greatest code writing environment for R there is.
You can also try Emacs, which is more like old-fashioned command line editor. You can find a good guide here.