line by line debugging in R studio - r

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)

Related

Clean workspace, plot pane, and console on rerun in Julia

I am used to starting all my Matlab scripts with clear all; close all; clc to ensure I am not looking at any old data or plots. I found Julia's clearconsole() to be equivalent to Matlab's clc, but don't have working solutions for the other two Matlab commands yet. I mostly work in the Juno IDE and run scripts with the Play ("Run All") button.
The Revise.jl package is supposed to clear the workspace now that workspace() is deprecated, but it doesn't work for this simple test case. If I define x once and then comment that line out, it will continue to print each time I run without error.
using Revise
clearconsole()
#x=1
println(x)
I know I can hit "Stop" then "Play" to reset the workspace. However, that still doesn't close old plots, and the time to first plot issue makes this option undesirable.
I found the "Forget All Plots" button in Juno's plot pane, but I would like to have that functionality as a line in my script instead. Currently, it takes me three clicks to run a script again after I edit it (four if I include "Stop").
"Forget All Plots"
Somewhere in the editor to put focus back on my current file.
"Run All"
I would ideally like to rerun in a fresh environment with one click or keystroke, but any tips on a better Juno workflow would be appreciated.
My question was answered on the Julia discourse website: link.
Juno.clearconsole() may be used like Matlab's clc.
Writing a script within a module will clear the variables upon each run like Matlab's clear all.
A new function may be added to Juno.jl in the future which will work like Matlab's close all.

How can I run multiple lines of code in R

I have just switched from windows 7 to windows 10 on my pc.
When I was still using windows 7, using the combination CTRL+R or CTRL+ENTER would run the line of code that I was in. Moreover, if the code continued on one or more lines below or started above my current line, it would run the entire section without me having to select it.
After switching to windows 10, the same combinations will only run the line of code that I am currently in, or the piece of code that is selected by me. To give an example:
#Tryout
for(i in 1:3){
print(i)
}
This code would normally run at once without having to select it, even when I would start in the first line (#Tryout). Now, if I don't select, I will need to use CTRL+R or CTRL+ENTER three times to go through this code and I have to start in the correct line. Starting in the line #Tryout will not run any part of the code but will only result in the line #Tryout being shown in the console.
I'm not good with computers (in your answers please pretend to be adressing a five-year-old) so I had one of my colleagues look at it who's our expert on R. Here are the findings:
The problem occurs in both RStudio as the standalone version of R.
I'm using R version 3.3.2, the same as before the windows update. Switching to version 3.5 didn't solve the problem.
I have other colleagues also using R 3.3 in combination with windows 10 for whom this problem does not occur.
Going to Tools-Global Options-Code and checking the box of 'Focus console after executing from source' doesn't help
When we went to 'Modify Keyboard Shortcuts', the name attached to the shortcut (CTRL+R or CTRL+ENTER) was the same as it was for other colleagues for which the function does run all lines in a command: 'Run Current Line or Selection'
The problem is not preventing me from using R, but it is tedious and I hope someone can help me to solve it.
Thanks so much for reading!
Click on header Tools, then Global Options..., then Code, then Ctrl + Enter Executes and choose Multi-line R statement
#M Waz & #Just Burfi: I know I can select the code and then run it, that works fine. But I don't want to have to manually select the code all the time.
#Clemsang: your answer puzzled me for a moment because I didn't have the CTRL + Enter Executes that you were referring to. I went back to my colleague who had the bright idea to check the version of RStudio that I was using.
As it turns out, I was using an old version (0.99.903). Now that I've installed a newer version (1.1.463), running the code works as before! I also have the CTRL + Enter Executes now.

How to Run a Single Line in RStudio

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.

Shortcut for running a single line of code in R

Does anyone know how I could create a keyboard shortcut or something similar to run a single line of diagnostic code in R Studio? i.e. if I wanted to do something simple, like checking the dimensions of a data frame, but I wanted to do it a lot throughout the day and didn't want to be continually typing dim(data), how could I get dim(data) into a keyboard shortcut or some other quick easy way to call that single line of code?
R itself can’d do that. Your editor may be able to, though (I know that Vim + Vim-R can do something like this).
What you can do in R is bind a function to an active binding. That way, whenever you invoke the binding, it executes your piece of code. To illustrate:
makeActiveBinding('x', function () dim(data), globalenv())
Now whenever you enter x in the R console, it executes dim(data).
The plain R terminal has a reverse incremental search function to make repetitive things easy. Hit Ctrl-R and start typing, it will match against your history. In this example, I've typed "di" and its enough to find the last "dim" call I did:
> x=matrix(1:12,3,4)
> dim(x)
[1] 3 4
> y=runif(100)
> dim(x)
[1] 3 4
# hit Ctrl-R at the prompt and type "d"... "i"....
(reverse-i-search)`di': dim(x)
I can hit return now, and it will do dim(x) for me. In fact it found it at the "d" because there was nothing else starting with a "d!" in the history.
There's a similar things in Emacs-ESS but I don't suppose you are using that. I don't know if this is implemented in RStudio, StatET, Architect, RCmdr or any of the other R interfaces that you might be using. I think RStudio might have a quick history search.
You could try using the snippet feature in RStudio (Tools -> Global Options... brings up the menu below). You can then add a snippet such as the code chunk below.
snippet d
dim(data)
Once the snippet is saved you can type the d (or whatever other string you defined after snippet). Then press tab and RStudio will give you the option to replace the shortcut string with the code listed in the snippet (here dim(data)).
There might be other options but for something as simple as a dim statement. There would likely be more effort than value add.

keyboard shortcut for step out while debugging in rstudio

When I'm debugging, every time, I make a change to the source code, I find myself doing the following since it says, "Debug location is approximate because the source is not available.". Is this 3-step process necessary? Is there an easier way?
> debugSource('~/Desktop/foo1.R')
> debug(myfun1)
> myfun1()
Not sure that this is, what you want, but since you use the tag rstudio, it might help.
Clicking in the left border of the script window in RStudio (next to the line numbers) creates a break point. It is marked by a red circle (it it's inside a function) or a red dot (otherwise). If you now click the source button in RStudio, debugSource is run. If the program encounters a breakpoint, it stops and you are able to examine the state of the program. A breakpoint inside a function stops the code when the function is run (and not when it is sourced). The breakpoint will also work when you run the function from the console as in your example.
The breakpoint remains intact when you change your code. So you have, of course, to source your code after every change, but you don't need to rerun debug every time.
For more information on debugging with RStudio, the following link might be helpful: https://support.rstudio.com/hc/en-us/articles/200713843-Debugging-with-RStudio

Resources