Create macros/shortcuts in R(studio) - r

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.

Related

Hiding JupyterLab cell's output by default

I am using JupyterLab to build a bioinformatics pipeline that uses both bash and python scripts.
The first bash script results gives a lot of feedback on every step of the process. However, this feedback is not helpful (unless there was an error) and makes the document less readable.
I would like to be able to hide this cell's output by default, but also to be able to open it when necessary to troubleshoot. I know it's possible to click 3 times on the output to collapse it; I was just wondering whether there is a way to do so by default.
I tried to add the tag specified on here (https://jupyterbook.org/features/hiding.html#Hiding-outputs) to the cell, but it does not seem to work for me.
Thanks for your help.
You may just want to suppress the output using %%capture cell magic as illustrated here. Then you simply remove that magic command from the first line of the cell for times you want to see the output, such as when troubleshooting.
If you want to make it so every time you run the cell, you can later decide to review what was captured you can use the %%capture magic command more as it was meant to be used. By assigning what is captured you can also do something like what the %%bash cell magic allows with handling output streams (see here), too. As described and illustrated here using the utils object you can easily get the stdout and/or stderr as a string, see http://ipython.readthedocs.io/en/stable/api/generated/IPython.utils.capture.html.
So say you put the following at the top of you cell to assign what was captured to out:
%%capture out
You can review the stdout stream later with the following:
print(out.stdout)
Or if you just want part of it, something like print(out.stdout[1:500]). I have some fancier handling illustrated in some blocks of code here.

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

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

Atom editor: list and jump to definition(s) in project

As already mentioned I'm using the Atom text editor.
I'm currently working on a project written in c++. Of course it is desirable to jump to the definition of a function (in another project file), or other uses of this function (within the project). As far as I know this can be achieved with the packages I'll mention below. I want the package to display me the definition along with the path to the corresponding file which holds the definition and ideally the line where it occurs.
I'll welcome any comments and suggestions on how to solve the below mentioned problem(s) I have with (one of) the packages. Moreover I'm also thankful about pointers to possible solutions or posts concerning my problem(s), or how I can achieve this with another package.
Here is what I found / tried / did so far.
goto
Currently I'm using this package, although it is rather slow and does not show the arguments of the function as e.g. atom-ctags does, but it's the only package which displays me the files I need to see.
It shows me where the function is defined as well as where it is also used. However it does not show me the path to the file corresponding file it refers to.
atom-ctags
I also tried this package, building the tags is quite fast and moreover it show me the path to the file. But this package only lists the .cc files and not the .h files. It appears to me as if it only shows me the other uses but not the definition, which is obviously a problem.
I also tried generating the ctags myself and changing the command options in the settings of the package, unfortunately without any success.
Atoms built-in symbols-view
In order to get this to work, one needs to generate the symbols. This can be, for example, achieved with the symbol-gen package. However, it shows me some of the definitions, but also no .h files. Moreover, jumping to the definition results in a Selected file does not exist., therefore it is not usable at all.
goto-definition
Just for completeness, there is also this package. It does not work for me, since c++ is not supported but maybe others will find it useful.
symbols-plus
Again, for completeness, this should be a replacement for the atom built-in, but when disabling the build-in it does not show me any jump functionality nor is a short cut mentioned.
So, basically, nothing really works well. I have tried Symbol Tree View but it but barely works.

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.

Rstudio editor snippets

Does Rstudio have a mechanism to configure snippets of code, like Geany for example? For faster writing whole sections of user predefined frequent code.
It is not exactly the same as TAB completion already built in rstudio.
Example by mimicking geany snippets
While snippet definition is like line below:
fun = %cursor% <- function(x, ...)\s{\n\n}\n
the usage is like this:
fun<TAB> (like bash style completion)
# will end up in following insertion:
<- function(x, ...) {
}
so the user can then faster write the code by using her own snippets definition. And user can define ANY snippet of any size for completion by TAB.
It is not the Rstudio extract cmd, nieder Rstudio existing TAB context browser.
Code snippets are available in RStudio version 0.99.
https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets
The "Extract Function" feature in RStudio may be what you're looking for. Scroll down to the Extract Function section and accompanying screenshot on this page of rstudio.com's documentation: http://www.rstudio.com/ide/docs/using/source
The text of the section reads, "RStudio can analyze a selection of code from within the source editor and automatically convert it into a re-usable function. Any 'free' variables within the selection (objects that are referenced but not created within the selection) are converted into function arguments."
Also see this screenshot: http://www.rstudio.com/images/screenshots/rstudio-code-transform.png
I do not know of such functionality. However, if you want to quickly want to implement functionality with small changes you could also achieve this using functions.
Ok, your question is now clear to me. To my knowledge, Rstudio currently does not have this kind of functionality. You could, however, post a request on their forum for this feature. They respond quite actively to these kinds of requests, so you could give it a try.

Resources