stop() in package function doesn't end debug mode - r

I've created very simple package, using RStudio, to demonstrate my problems, the steps follow.
Create package with only one function
write <- function(text) {
print(text)
if (is.numeric(text))
stop("Text cannot be numeric.")
}
Build -> Install and Restart
In a new R Script, write following code
pckgname::write(1)
Result in Console:
tempPackage::write(1)
[1] 1
Error in tempPackage::write(1) : Text cannot be numeric.
Called from: tempPackage::write(1)
Browse[1]>
Problems
5a) I don't want to see "Called from: tempPackage::write(1)".
5b) More importantly, I don't want to end with "Browse[1]> " meaning debug mode is still open (also buttons like "Continue" and "Stop" are visible) so now I need to click on button "Continue" to finish debug mode to end on line with ">" but I want to end this debug mode without clicking on button "Continue" meaning when I run that one line code "tempPackage::write(1)" I want to see result in Console with finished debug mode.
Additional information:
When I use this stop in function directly in the same R Script (not called from a package), it works like I want.
Solution using following method also doesn't help with previous problems.
opt <- base::options(show.error.messages = F)
on.exit(base::options(opt))
When I found some solution e.g. on GitHub e.g. https://github.com/jakesherman/easypackages/blob/master/R/package.R and installed them and intentionally made an error then again I get what I wanted (debug mode ended and no message Called from shown).
Thank you for any help in advance.

I just did exactly as you instructed and I see the expected result:
pckgname::write(1)
[1] 1
Error in pckgname::write(1) : Text cannot be numeric.
I'm not sure what's wrong when you try it, but would recommend starting a fresh package in case something is not working correctly. You could even try reinstalling R and RStudio, as it worked first time for me and I can't think of why it wouldn't for anyone else.
For reference, here's exactly what I did:
I started the package from RStudio -> File -> New Project -> New Directory -> New Package
The only file I changed was hello.R - I added the code in your question exactly as-is.
I built the package with RStudio -> Build -> Build from source
And then installed it with install.packages("../pckgname_0.1.0.tar.gz", repos = NULL, type="source")
And loaded with library(pckgname)

Related

How do I turn off degugging of `[.data.frame` in R?

I am stuck in the debugger. I had an error in a function I ran using RStudio. I clicked the rerun with debugger and now every time I run it, it starts the debugger and I can't stop it.
It tells me that the function being debugged is [.data.frame however if I try `undebug('[') or undebug('[.data.frame') I get
Warning message:
In undebug([) : argument is not being debugged
I have restarted RStudio, but that does not help either. I have tried running some of the functions from this SO question to no avail.
Any ideas?
Then try this
"Debug" -> "On Error" menu and select "Message only"
click here for more informations

What is this error in RStudio: "breakpoints will be activated when the file or function is finished executing"?

I am trying to set a break point inside a function from a package I wrote. I am unsuccessful when clicking next to the line number in Rstudio, the error message looks like:
I am not executing anything. Reloading the package did not help either. What is this error and what can I do about it?
Check out this documentation from RStudio that explains what to do.
Basically, make sure you save your R file, then click the Source button on the toolbar:
Your breakpoint should then turn from a hollow red circle to a full one.
Usually running rm(list=ls()) will do the job.

Installing / Using R Packages in Sublime Text 3

I am trying to use the R function dmvnorm (found in the mvtnorm package) in Sublime Text 3. I installed it and ran my code in RStudio, so I know the code is fine. In sublime, I entered:
install.packages('mvtnorm',repos='http://cran.us.r-project.org')
library(mvtnorm)
It looked as though it worked, but when I ran my code it said:
Error: could not find function "dmvnorm"
I'm using a Mac and my hunch is this is somehow related to specifying the path in Preferences -> Package Settings -> Sublime REPL -> Settings - User. The current path displayed as part of the error reads:
[path: /usr/bin:/bin:/usr/sbin:/sbin]
Thanks!
I had a hard time with this recently as well. I'm very new to R so I'm not sure this is the best answer, but to hold you off until someone gives a better one, did you include library('package.name') in the file (in sublime)? require('package.name') also works, but this appears not to be best practice for reasons described, e.g., here.
I hope this helps!

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)

Make R project Automatically open Specific Scripts

I am working in team, we mainly use R, I am quite used to use R project in Rstudio, which I like because when I open them I have all my scripts and everything at the right place. However when another member of the team opens one of my project it loads the values and data but does not open the R script (one can see that by physically clicking on the project through the windows explorer rather than using the menu at the top right in R). I guess something can be done in the .Rprofile but I did not find any command to open physically a script, I tried
file.edit("./Main.R")
but it did not open anything. It just got me the message :
Error: could not find function "file.edit"
As always,
Thanks for your help !
**EDIT
I tried to use
file.show
file.edit
shell.exec(file.path(getwd()), "Main.R"))
in the .Rprofile. Nothing worked.
Romain
You can use the following code in the .Rprofile file.
setHook("rstudio.sessionInit", function(newSession) {
if (newSession)
rstudioapi::navigateToFile('<file name>', line = -1L, column = -1L)
}, action = "append")
The rstudioapi library has the function navigateToFile to open a file in Rstudio. The problem is that the code in the .Rprofile runs before Rstudio initialization. To deal with this problem you can use the setHook function (from base package) to make the code execute after the Rstudio initialization.
file.edit requires the utils package
library(utils)
file.edit("Master.R")
However, if it opens in Notepad rather than RStudio you have the same problem as me. I've tried editing the editor= in all possible places: .RProfile, RProfile, RProfile.sites, with and without .First() function statements and calls. However, RStudio does not load the .R file in RStudio if told to. It may be linked to the .RData file being loaded after .RProfile. Bug? Or at least a feature RStudio should incorporate in their RProject file specification.

Resources