How to Run a Single Line in RStudio - r

I am used to using the standard R GUI but I'm trying to make the switch to RStudio. However, it's driving me nuts that I can't figure out how to run a single line with a keyboard shortcut. For instance, if I have the two lines,
c <- a +
b
I would like to press ^R to run the first line and then have to press ^R again to run the second line to complete the assignment. This is the default setting in the standard R GUI. Unfortunately, RStudio only seems to want to execute a statement to it's completion. Is there any way around this?

If you want this to be the default behavior, go to "Tools -> Global Options -> Code -> Editing" and uncheck the box "Execute all lines in a statement" under "Execution".

Select the first line (by clicking on it three times), then Ctrl-Enter will run the selection, not the statement.

Related

don't echo to R console

Is there a way to turn off/on echoing to the R console (without using source()?)
For example, let's say I have a long .R script and I wish to run only one line from it. Say that line is x <- 9.
In RStudio, I can go to the line in question and use the "Run Selected Line(s)" command from the "Code" menu (or keyboard shortcut ctrl-enter on my PC). What'll happen upon doing that is it the console will print x <- 9 (and then obviously, R will create a variable called x and assign it the value 9).
Is there a way to not have this line echoed in the console (but still create the variable)?
The reason I ask is that I have lengthy lines of code that just define functions and every time I want to update a function, it echoes the whole thing to the console, and that burns a lot of time.
Thanks.

R (Rstudio) I forget a ) and my whole script is executed...Is there a way to stop that?

I have a question about R (personnaly working on it via RStudio). Is there is a magic trick to stop my 2600+ line script to be fully executed whenever I launch a line (ctrl+enter) in which I have forgotten a )
Whenever it happens, I find myself clicking like a maniac on the red "STOP" button on Rstudio but still lose precious minutes.
Thanks a lot!
I guess it partially depends on how you're running your script.
If you highlight a selection of code and then do something like ctrl+enter to run those lines then... no. You're kind of stuck doing what you're doing.
If you switch to using ctrl+shift+enter (which is the shortcut for "Source with echo") then you can press the Stop button once and it will cease executing completely.
If you're too used to using something like Ctrl+enter you can modify the keyboard shortcuts to use "Source with echo" instead of just "Run current line or selection"

paste single line command in R console without executing

Since I have started using R I have noticed inconsistent behaviours when pasting a complete single command line in the R GUI console. Sometimes the command is executed, sometime is not. I tried to use the "paste commands only" but the command is still executed and the option does not seem to have any effect. I want to past a complete command and not execute it.
How do I control what happens when I paste a single line?
EDIT: i think the issue is that I am also copying from notepad the "enter" character, so that's what make the command run right away. Is there a way to avoid this?
If you do not copy a newline character at the end of the command, it will not be executed when you paste it. You will need to enter one manually.
Rather than copy/paste, you might want to use an editor where you can highlight the code you want to run and then send it to the R console via a button or shortcut key. The Windows R GUI has this feature, as does Rstudio.

line by line debugging in R studio

Is there a way to debug the code line by line in R studio environment ??
I know there are breakpoints, Next, continue etc to debug. But I am looking for a line by line debug option like the one in Visual Studio.
Thank you
For R-Studio newbies like me that are used to other IDEs:
a) Set a break point by clicking on the border or pressing Shift+F9 (=>red break point dot is shown)
b) As equivalent to "Debug" in other IDEs:
Click source or
Press Ctrl+Shift+Enter or
Activate source on save and save
c) Have a look at the Console view. There are common debug options:
Execute Next Line F10
Step Into Function Shift+F4
Finish function Shift+F6
Continue Shift+F5
Stop Debugging Shift+F8
(Unfortunately I did not find a way to adapt the key shortcuts for those options. They are not listed under Tools=>Modify Keyboard shortcuts.)
d) There does not seem to be a "hover over expression" feature while debugging. You can have a look at the Environment view to see the value of a variable and use the Console to evaluate expressions while debugging.
If you want to run the script without debugging and without clearing the break points, select all lines Ctrl+A and use the Run button. (Seems to be complicated to me.... I would expect an extra run button or key shortcut for that but could not find one.)
If there is no selection, the Run button only executes the current line. You can press that button several times to step through the code and see the corresponding console output (=pseudo debugging).
Also see documentation at
https://support.rstudio.com/hc/en-us/articles/200484448-Editing-and-Executing-Code
https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio
and related questions:
Disable all breakpoints in RStudio
Debugging a function in a different source file in R
Rstudio debug - missing step into button
https://support.rstudio.com/hc/en-us/community/posts/202156378-how-do-i-clear-the-console-
The debug package is probably what you want. If you are debugging via this package an extra window opens in which your code is displayed and then you can debug line by line in combination with RStudio.
EDIT:
See below example code for how to debug with the debug package:
install.packages("debug")
library(debug)
fun <- function(x) {
y <- x + 1
z <- y + 1
return(z)
}
mtrace(fun)
fun(2)

Is there a hack to be able to run the current line or selection in RStudio without moving the cursor?

UPDATE (April 2013): As per answer below, RStudio no longer jumps cursor on selection.
I'm running RStudio 0.97.168.
I like to use the script editor in RStudio like a console. Thus, I run a line of code and then edit it a little bit and re-run it. I often also explore objects by selecting some of the code and running the selection and then progressively altering the selection. At present RStudio always moves the cursor after running a line of code. The cursor can move to a variety of places. Typically the cursor moves to the next line of R code, but depending on the context, it could move to the end of the code block or the next line. It's really frustrating having to constantly move the cursor back to where I want it.
While I often appreciate the default cursor movement behaviour, I'd like to have the option to run the selection or the current line without the cursor moving.
I've raised this as a suggestion on RStudio support.
I'd like to be able to have a shortcut key like "Cmd+Alt+Enter" that runs the current line or selection and does not move the cursor in the script editor.
I realise that this is not currently supported, but I was wondering whether there might be some creative hack that could enable the cursor not to move after running a command or even a patch or perhaps some sort of external macro.
For anyone who ends up here in 2020:
Ctrl(or Cmd) + Enter: Will run current line and jump to the next one. If a code portion is selected, run the selected code without jumping further.
Alt + Enter: – Will run the current line of code without moving the cursor to the next line, useful if you want to run it multiple times.
(Source)
For this kind of flexibility, I suggest you use the editor Sublime Text 2, add in the package installer by Will Bond and then install the SublimeREPL package which will allow you to use an R interpreter within ST2 (or BASH prompt, Python / Ruby / whatever interpeter, concurrently if you wish).
You can then alternate between your code and the interpreter without lifting your fingers from the keyboard and your cursor will be at the same point every time when you want to switch back.
Sublime Text will also allow you to write a custom keybinding to automate this task.
I cannot recommend using Sublime Text 2 highly enough when coding for R. You can even pass files directly from ST2 into RStudio very easily if you like using the plot panes (very easy to do with the SidebarEnhancements package in ST2).
RStudio is awesome for many things -- especially now with Knitr, builds etc etc. But ST2 with an R REPL is many orders of magnitude more powerful for general code writing / editing than RStudio.
Sorry it's not RStudio specific, but it is a nice workaround!
I updated to version 0.98.83 of RStudio using the daily build section.
It appears that at some point in recent versions of RStudio, the cursor no longer jumps when code is run from a selection in the script window.
That's great news.

Resources