Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving .R script File Using Script

Tags:

r

rstudio

I am using R Studio and I want to save my script (i.e., the upper left panel). However, the only ways that I can find to do it are by either clicking the blue floppy disk icon to save or using the drop down menu File > Save > name.R

Is there any way besides using these shortcuts to save the script to a .R file or is the shortcut the only way?

Thanks.

like image 513
user44992 Avatar asked Jul 12 '16 14:07

user44992


1 Answers

You can use rstudioapi::documentSave() to save the currently open script file to disk.

From the source documentation, one can see that it can be used in conjunction with the id returned with getActiveDocumentContext()$id to make sure the document saved is the one running the script.

For your intended use, try:

rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)

For future reference, here is the reference manual of rstudioapi: https://cran.rstudio.com/web/packages/rstudioapi/rstudioapi.pdf

like image 51
André Pinto Avatar answered Sep 21 '22 03:09

André Pinto