Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"could not find function" only when in the R debugger

Tags:

r

debugging

I've been running into this from time to time but for the first time I have simple reproducible example. The fact that the example involves lubridate is, I believe, coincidental. In R --vanilla session

library(lubridate)
ymd("2001-02-02")
# [1] "2001-02-02 UTC"
# so far so good
debug(ymd)
ymd("2001-02-02")
# debugging in: ymd("2001-02-02")
# Error in ymd("2001-02-02") : could not find function ".parse_xxx"

There is nothing wrong with ymd, I was trying to learn some things. But when you are actually debugging and this happens, it's very annoying. Debugger bug or am I missing something?

Version info

    > R.version
               _                           
platform       x86_64-apple-darwin13.4.0   
arch           x86_64                      
os             darwin13.4.0                
system         x86_64, darwin13.4.0        
status                                     
major          3                           
minor          1.2                         
year           2014                        
month          10                          
day            31                          
svn rev        66913                       
language       R                           
version.string R version 3.1.2 (2014-10-31)
nickname       Pumpkin Helmet  

And session infor per @Joshua Urlich comment

  > sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lubridate_1.3.3

loaded via a namespace (and not attached):
[1] digest_0.6.4  memoise_0.2.1 plyr_1.8.1    Rcpp_0.11.3   stringr_0.6.2

Thanks

like image 997
piccolbo Avatar asked Mar 27 '15 16:03

piccolbo


People also ask

Why can't R find my function?

This error usually occurs when a package has not been loaded into R via library . R does not know where to find the specified function. It's a good habit to use the library functions on all of the packages you will be using in the top R chunk in your R Markdown file, which is usually given the chunk name setup .

What does the red dot on R mean?

R will treat the break point like a browser statement, going into browser mode when it encounters it. You can remove a break point by clicking on the red dot. The dot will disappear, and the break point will be removed.

What does debug function in R do?

debug() The function debug() in R allows the user to step through the execution of a function, line by line. At any point, we can print out values of variables or produce a graph of the results within the function. While debugging, we can simply type “c” to continue to the end of the current section of code.

Why could not find the function in R?

The error “could not find function” occurs due to the following reasons − Function name is incorrect. Always remember that function names are case sensitive in R. The package that contains the function was not installed. We have to install packages in R once before using any function contained by them.

How do I debug a function in R?

Use the R function debugonce () to set the debug flag on a function. For instance, if you want to debug devtools::install (): debugonce () sets a one-shot breakpoint–that is, the function will enter the debugger the very next time it runs, but not after that. If you want to debug a function every time it executes, call debug (...) on the function.

How do I debug an error in Python?

When you encounter an error, your first course of action should be to run the function traceback (). traceback () will list the chain of functions that led to the error. This might be enough to figure out the solution and if not you’ll at least get a good idea of which function to debug.

Why doesn’t RStudio debug my own code?

To keep the the debugger from being invoked whenever any error anywhere happens, RStudio does not invoke the debugger if it looks like none of your own code is on the stack. If you find that this is excluding an error you want to catch, go to Tools -> Global Options and uncheck “Use debug error handler only when my code contains errors”.


2 Answers

Looks like it might have to do with the functions not being defined using {}. The current implementation is:

ymd <- function(..., quiet = FALSE, tz = "UTC", locale = Sys.getlocale("LC_TIME"),  truncated = 0)
  .parse_xxx(..., orders = "ymd", quiet = quiet, tz = tz, locale = locale,  truncated = truncated)

Your debug attempt works if I unpack the source, and change the implementation to:

ymd <- function(..., quiet = FALSE, tz = "UTC", locale = Sys.getlocale("LC_TIME"),  truncated = 0)
{  .parse_xxx(..., orders = "ymd", quiet = quiet, tz = tz, locale = locale,  truncated = truncated)}

I was able to replicate the issue before making the above change. After making it, I get:

> require(lubridate); debug(ymd); ymd("2015-01-01")
Loading required package: lubridate
debugging in: ymd("2015-01-01")
debug: {
    .parse_xxx(..., orders = "ymd", quiet = quiet, tz = tz, locale = locale, 
        truncated = truncated)
}
Browse[2]> 
debug: .parse_xxx(..., orders = "ymd", quiet = quiet, tz = tz, locale = locale, 
    truncated = truncated)
Browse[2]> 
exiting from: ymd("2015-01-01")
[1] "2015-01-01 UTC"
like image 181
Joshua Ulrich Avatar answered Oct 01 '22 09:10

Joshua Ulrich


Not a solution or explanation but a potential work around:

trace(ymd, browser)

Then:

> ymd("2001-02-02")
Tracing ymd("2001-02-02") on entry 
Called from: eval(expr, envir, enclos)
Browse[1]> n
debug: .parse_xxx(..., orders = "ymd", quiet = quiet, tz = tz, locale = locale, 
    truncated = truncated)
Browse[2]> 

I am able to recreate your error on Win7 R3.1.2 / RStudio. I was hoping to recreate the problem with trace to try to poke around, but any trace of the function destroys the problem...

like image 27
BrodieG Avatar answered Oct 01 '22 08:10

BrodieG