What mistake would trigger a diagnostic using this option in R Studio? - r

In the Global Options of R Studio, under the Code Panel and Diagnostics Tab, there's an option that reads "Enable diagnostics within R function calls."
I had assumed that this option would trigger diagnostic warnings any time one made a mistake within a function call. For example, if I accidentally omit a comma between arguments, I get a diagnostic warning:
c(x, rep(y each = 3))
However, this warning appears in this instance whether I have the option mentioned above on or off.
When you Google this option, you get a lot of hits from people also wondering what this option does. See here. You also get a lot of hits describing this option with this exact text:
Controls whether diagnostics are performed within function calls, e.g. dplyr::select(mtcars, mpg, cyl). Toggle this if your code makes heavy use of non-standard evaluation, and RStudio is unable to produce correct diagnostics for you.
See here as one example.
I've tried reading Hadley Wickham's article on what non-standard evaluation is, and I tried "breaking" the functions he shows in that article to see if I could come up with an example of this option triggering a diagnostic warning that wouldn't already come up, and I was unsuccessful.
Can someone show an example of a mistake that trigger this specific option to trigger, i.e. the warning comes up only if this option is checked and not if it isn't?

Related

Annoying RStudio Behavior in debugging

When I am in debug mode in a function and uses "CTRL + Enter" on an object/variable, R takes me back on line 1 of the function and prints the object variable to the console. Is this a bug or an expected behavior?
Another bug in debug mode:
- If there is a print(variable1) somewhere in the function, R will not only print to the console variable1 but also the remaining function code that comes after "print(variable1)"
Are there any ways of avoiding those behaviors of R Studio?
Thanks,
This may be worth reporting to rstudio directly as it may be a bug that was left over from a change of behavior in debug mode. I looked through the browser help file and didn't come across this behavior.
An rstudio article from April 2016 on debugging here, states that
Simply pressing Enter at the console will execute the current statement and move on to the next one. This is a convenient way to step through statements quickly.
Note that most of the commands listed in this section directly match with the behavior of browser.
However, I've been reading Hadley's Advanced R recently and came across the following in section 3.2.2:
There are two other slightly less useful commands that aren’t available in the toolbar:
* Enter: repeats the previous command. I find this too easy to activate accidentally, so I turn it off using options(browserNLdisabled = TRUE).
...
Since Advanced R was published in 2014, my thinking is that this is legacy behavior that you should report to the makers of rstudio, complete with a reproducible example.

Is there a fast way to copy and paste function arguments to R console

using R and debugging, I often might have a function with several arguments set by default.
e.g.
foo <- function(x=c(3,4,5), y= 'house', dateidx = '1990-01-01'){}
Often I just want to manually run through some lines in the function, while using the pre-set parameters. If the parameter list is long, I have to type or paste each argument to the console manually before stepping through the function.
x=c(3,4,5)
y= 'house'
dateidx = '1990-01-01'
It's ok if the list of arguments is small but if there is a long list of arguments, it gets tedious. Is there some way to just copy the whole set of arguments, paste to console, and do something like unlist, so that all the arguments are passed to the console as if I manually passed each one?
p.s. I'm weakly familiar with the debug tool, but sometimes I find it easier and faster to just troubleshoot lines quickly and manually as above.
There is no easy pre-existing way to do this--mainly because this a problem solved by the debugger.
One could imagine hacking something together that might parse these parameters with a regex and set them automatically--or something like that. However, the effort would be much better spent learning how to use the debugger.
It should be quite quick to test the part of the code you are interested in with the debugger if you learn how to use it. RStudio has a visual debugger. Using this, you can simply mark the command you are interested in testing with a breakpoint and run the script. The script will run until it reaches the breakpoint, then stop there so you can inspect what is happening.

Disabling output has no effect

I noticed that, under some, unknown to me circumstances, some functions, whether from base R (for example, gc()), or from external packages (for example, getCurlHandle() from RCurl), still produce output, even after explicitly disabling it via verbose = FALSE. I am curious about reasons for such behavior. The only workaround I found on SO is the recommendation to call invisible(), but for me it worked only for gc(), but not for getCurlHandle(). Would appreciate any comments and answers.
The command gc(verbose=TRUE):
prints some statistics and percentages,
AND prints the matrix that is returned by the function.
The command x=gc(verbose=TRUE) only prints the statistics.
The command gc(verbose=FALSE) only prints the returned matrix.
The command x=gc(verbose=FALSE)prints nothing.
While preparing a reproducible example, I figured this out. The source of the questionable output was not getCurlHandle(). It has been producing by another (next) function: curlSetOpt(). I took care of disabling its output by using invisible().
It was really not bad, comparing with the effort I've made for figuring out my previous R and RCurl problem. But, it is always fun and educational.

R: Equivalent command to Matlab's keyboard function?

Does R provide a similar command for debugging like Matlab's keyboard?
This command provides an interactive shell and can be used in any function.
This gives access to all variables allowing one to verify that the input data is really what it should be (or test why it's not working as expected).
Makes debugging a lot easier (at least in Matlab...).
It sounds like you're looking for browser().
From the description:
A call to ‘browser’ can be included in the body of a function.
When reached, this causes a pause in the execution of the current
expression and allows access to the R interpreter.
It sounds like you're new to debugging in R so you might want to read Hadley's wiki page on debugging.
Have a look at ?recover, this function provides great debugging functionality.

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