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
Related
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.
I often include View() statements in my R scripts. If I accidentally forget the closing bracket at the end of the line, and then run the line of code from the script window using ctrl-enter, R just keeps trying to execute the remainder of my script. I don't know why it does that (rather than using the + symbol to prompt me to provide further input).
Moreover, I've tried to stop this by setting break points in my code - I can click on the LHS of the page and a little red circle appears. But the breakpoints don't seem to work - R just ignores them and keeps going.
The only way I can get out of it is by killing the process in the Windows task manager and then going back in afterwards. But it's wasting a lot of time.
Does anyone know how I can fix this please?
Thank you.
In effect, what your function is processing looks like that:
... %>% View(
lm(am~cyl, mtcars)
...
...
As R can't find the bracket for ) it includes remaining statements as input to View and searches for the bracket.
Solutions
Kind of depends on what you want to do with those scripts but if the intention is to run them in the background consider using callr. This package lets you run R from R and offers kill methods to kill the process you started that way.
On Windows pressing Esc should enable you to get back to the console but if it's a memory intense process it may be difficult.
You may try pressing Ctrl+c in order to kill the process.
I have a main R script which does a multiplication using a function called Multiply().
debugSource("C:/Users/R_debug_breakpoint/myFunction.R")
a<-1
b<-2
mult<-Multiply(a,b)
I write Multiply() in a different R script called "myFunction.R"
"myFunction.R" looks like this:
Multiply<-function(a,b){
c<-a+b
e<-a/b
d<-a*b
return(d)
}
If I want to set a breakpoint inside Multiply() function at the line:
d<-a*b and enter source mode to see what the value of c,e.
However, when I press Source option in my main.R, the debugger does not halt at the breakpoint I set inside the Muliply function. It just runs all the codes in main.R.
I search this problem in many webpages, and the most close one is
How to set a breakpoint in function body in R-studio?
However, I use debugSource here but it still fails.
Another blog about the breakpoint I found out useful is:
https://support.rstudio.com/hc/en-us/articles/200534337-Breakpoint-Troubleshooting
It says:In order to hit breakpoints during sourcing, you need to use the special debug sourcing command debugSource included in RStudio. If you are calling source manually on your file, breakpoints will still work, but will not be enabled until after all the code in the file has been executed.
I think this is reason why it doesn't work, but it doesn't mention a work around for it.
Much appreciated if any help from you guys.
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)
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.