Close a script file in Rstudio using code instead of point clicking - r

How can force close an open script file - or in general an open tab - in RStudio using code?
As pointed here I already know that file.edit('test.R') can open a given file.
This is how I usually end a given script file before moving to an other section.
What I'd like to do, is to start the new section (script file) from closing what was used before.
For example:
import_data.R has code that does all file reading / importing and when that's done, it opens data_wrangle.R by calling file.edit("data_wrangle.R") where all the transforming, mutating etc is done.
Since I don't need the import code any more, I'd like to close previous script (import_data.R in this case) with a line of code.
I did some guesswork calling
close("./script/test.R")
or
rm("./script/test.R") but they clearly don't serve this purpose.

You can have a look to the .rs.api.documentClose() function.
Regards

The rstudioapi package doesn't have a function to close a file, but it does have one to navigate to it. So your script could end something like
rstudioapi::navigateTo("data_wrangle.R")
rstudioapi::navigateTo("import_data.R")
and then I think a Ctrl-W as suggested by #Joyvalley would do what you want.

Ctrl - w does the job
Its not exactly what you ask for, but does what you want.
https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts

Here in 2023, 'executeCommand' is your friend:
rstudioapi::executeCommand('closeAllSourceDocs')

Related

RDCOMClient log file

I have been using RDCOMClient for a while now to interact with vendor software. For the most part it has worked fine. Recently, however, I have the need to loop through many operations (several hundred). I am running into problems with the RDCOM.err file growing to a very large size (easily GBs). This file is put in C: with no apparent option to change that. Is there some way that I can suppress this output or specify another location for the file to go? I don't need any of the output in the file so suppressing it would be best.
EDIT: I tried to add to my script a file.remove but R has the file locked. The only way I can get the lock released is to restart R.
Thanks.
Setting the permissions to read only was going to be my suggested hack.
A slightly more elegant approach is to edit one line of the C code in the package in src/RUtils.h from
\#define errorLog(a,...) fprintf(getErrorFILE(), a, ##__VA_ARGS__); fflush(getErrorFILE());
to
\#define errorLog(a, ...) {}
However, I've pushed some simple updates to the package on github that add a writeErrors() function that one can use to toggle whether errors are written or not. So this allows this to be turned on and off dynamically.
So
library(RDCOMClient)
writeErrors(FALSE)
will turn off the error logging to the file.
I found a work around for this. I created the files C:\RDCOM.err and C:\RDCOM_server.err and marked them both as read-only. I am not sure if there is a better way to accomplish this, but for now I am running without logging.

What exactly does Source on Save mean or do?

Despite numerous searches, I can't seem to find a clear explanation as to what "Source on Save" means in RStudio.
I have tried ?source and the explanation there isn't clear, either.
As far as I can tell, it seems to run the script when I hit Save, but I don't understand the relevance/significance of it.
In simple terms, what exactly does Source on Save do and why would/should I use it?
This is kind of a shortcut to save and execute your code. You type something, save the script and it will be automatically sourced.
Very useful for short scripts but very annoying for time consuming longer scripts.
So sourcing is basically running each line of your file.
EDIT:
SO thinking of a scenario where this might be useful...
You developing a function which you will later put into a package... So you write this function already in an extra file but execute the function for testing in the command line...
Normally, you have to execute the whole function again, when you changed something. While using "Source on Save" the function will be executed and you can use Ctrl + 2 to jump into command line and test the function directly.
Since I am working with R, my datasets are much bigger. But I am remembering starting coding in python and vi, I updated my setting in a way to execute the code on save, since these little scripts where done in less then 10 seconds...
So maybe it is just not standard to work with small datasets... But I can still recommend it, for development, to use only 10% of a normal dataset. It will speed up the graphics creation and a lot of other things as well. Test it with the complete dataset every now and then.

How to change toolkit in Octave?

My Octave crashes when I execute plot command. I found a solution in Assad Ebrahim's answer. He mentioned to switch the default toolkit to gnuplot, and change it in octave.rc file if I want to make the change permanently but I'm not clear about the permanent change in octaverc. When I open my octaverc with notepad++, it looks like this:
## System-wide startup file for Octave.
##
## This file should contain any commands that should be executed each
## time Octave starts for every user at this site.
EXEC_PATH (cstrcat (fullfile (OCTAVE_HOME, 'notepad++'), pathsep,
EXEC_PATH));
EXEC_PATH (cstrcat (fullfile (OCTAVE_HOME, 'bin'), pathsep, EXEC_PATH));
EDITOR (fullfile (OCTAVE_HOME, 'notepad++', 'notepad++.exe'));
What should I change and how?
First, the direct answer to your question is to append any command you want executed on startup to the end of the .octaverc file. So, to set a particular graphics toolkit you would add the line:
graphics_toolkit("gnuplot")
Or
graphics_toolkit("qt")
Or
graphics_toolkit("fltk")
For whichever toolkit you want.
Now, as pointed out by #Andy, if you are using Windows, it may be that you are misinterpreting a long delay for a crash. A still not entirely resolved windows bug concerns the fact that on the first plot Windows might need to create a font cache file. This can take a long time. Once this is complete, most subsequent plots will be much faster. Some info can be found about it at the following bug report page:
https://savannah.gnu.org/bugs/?45458
EDIT: in the time since this answer was posted, the bug linked above has been largely resolved. Part of the installation process now updates the font-cache file. If using a zip package rather than an executable installer, there is a batch file that should be run after extracting octave to ensure that this is done. Details are available at:
http://wiki.octave.org/Octave_for_Microsoft_Windows

Create macros/shortcuts in R(studio)

Is there a way to write your own macro in R(studio). Many times in my code i need to comment out a line/piece of code, run it, and then comment it in again.
I know that a shortcut to comment in a line/piece of code is ctrl+shift+c, so I would like to create a shortcut e.g. ctrl+alt+c to comment in/out+run+comment in/out
You can create an Addins doing exactly what you want and assign a shortcut to it.
For exemple, that Addin is used to create upgraded shortcut for pipe.
To do what you want, a solution may be possible but require a few constrain.
Three, at least:
First, you need to select the code you want to comment
Secondly, in the same time you use the shortcut, the file should be saved.
Thirdly in your file, you can't have two time the same selected piece of code.
The commented code will be the input of the Addins.
The code is as simple as read your file, replace the code by commented code, then run the modified code.
It is different in what you want to achieve in the sense of in that case, it is more create a temporary copy of the code, comment the undesirable code, run the temporary modified code.
For shortcuts see here. Macros might be found here.
E.g. commenting in/out = Ctrl +Shift + C (Both)
If you want a to have a new shortcut, you have to ask RStudio. For an example, where it was already solved, see here. From the list of available shortcuts it is clear that "your" shortcut does not exist.

Hide and show comments

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.

Resources