How to create a shortcut to insert a particular string in R - r

For the purpose of uniformity in my code, I repeatedly type four hashes followed by a tab, like this (#### ). Is there a way to create a shortcut key-combination in R/Rstudio where such a string can be defined?

Although it doesn't answer your question, it might still be helpful for what you are trying to do.
You can organize your code in RStudio using "sections". A typical section looks like this:
# Some Section -------------------------
These can be manually entered (like the code chunk above, and with a few other variations) or you can use the convenient shortcut, Ctrl + Shift + R.
One great result of using these sections is that you can navigate to a particular section by its name, as shown in the screenshot below:

Related

How to create exam sheets with solutions in a non-English language with R exams

In order to create the exam sheets without solution, everything works fine with exams2nops (apart from the logo which is not taken for some reason). I can add the name of the institution, change the title, and setup a non-English language and the date. But if I want to create examsheets with the solutions, then, if I understand correctly, I cannot use anymore exams2nops, but I have to use exams2pdf with template="solution" which does not take into account the institution, the date, the language, nor the title.
Any help?
You are correct that exams2nops() does not provide a version of the PDF with the {solution} environment and the complete answer text. The nops_eval() only provides a HTML report with the correct boxes that needed to be checked.
So to produce a PDF as desired you can run exams2pdf(...) or exams2pdf(..., type = "solution") after setting the same random seed that you set before running exams2nops(). If you want to modify anything like layout/logo/language/etc. in exams2pdf() you have full flexibility but need to do the work yourself. That is you need to come up with a LaTeX template file that looks the way you want. As a starting point you can take solution.tex from the package. The easiest way to get all templates shipped with the package is to run exams_skeleton() and then look at the templates folder.
Background info: exams2nops() also just calls exams2pdf() internally after setting up a suitable LaTeX template file based on the arguments specified by the user.

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.

RStudio: Code sections and compiled notebooks

I really like RStudio's code sections because they help me keep my code organized and makes navigation very easy. So, the code/comment line for code sections look like this (the key for RStudio to recognize the code section is that the comment should end with four or more #, - or =).
# Section One ---------------------------------
# Section Two =================================
### Section Three #############################
Now, I also find very useful "Compiling Notebooks from R Scripts" because you can produce reports directly from your script (which I find much more comfortable working with) without having to write an RMarkdown document. You simply have to use roxygen2-style comments (using #' as prefix) and they will be converted to markdown and/or use #+ or #- prefixes to control RMarkdown chunk options.
Now, I want the best of both worlds but I cannot find a way to get it. I want to have code sections (recognizable for RStudio) in the report (with the proper markdown for headings #, but without the extra #### at the end of the line). Seems like an impossible task to me. If I use #' prefix, I get the section in the report but RStudio does not recognize it as a code section. If I do not use roxygen2-style comments, RStudio recognizes the code section but it does not appear in the report (ok, it appears but as an ugly comment in the formatted code). The best solution I have found so far is to use the #+ prefix and simulate a code chunk label. That way I can get RStudio to recognize it as a code section, and it does not appear in the report as an ugly comment.
Any ideas on how to do this better?

Rstudio autocomplete variables

I am new to both R and Rstudio. I like the autocompletion of functions, models and data.frames. My question concerns autocompletion of variables.
So lets say I load the well known iris data.
I can start typing "ir", click click tab and get the rest of the dataset to autocomplete.
I also know can get the names of the variables by various means such as names(iris). Which I can copy and paste.
Now, how can I do a good autocomplete with variables? I would like to be able to type "pet" and tab and it gives me the different possible variables options to autocomplete (like it does for functions).
Is something like that possible?
The only workaround I can see is type out the full dataset$variable name such as iris$ then tab, when then allows me to select iris$petalwitdth. But that it more typing and makes for some ugly code. I just want "petalwidth" to autocomplete.
Options? Suggestions?
I think iris$P tab is preferable, but if you must, you can attach the dataset to the search path
attach(iris)
Then, P tab will auto complete

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