Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in get_pkg_srcrefs(pkg) : Unexpected format for source refs. #134

Closed
caseybreen opened this issue Feb 2, 2022 · 7 comments
Closed

Comments

@caseybreen
Copy link

caseybreen commented Feb 2, 2022

I'm getting an error when I try to profile code with RStudio (following this tutorial).

Here's a reprex:

profvis({
     data(diamonds, package = "ggplot2") 
     plot(price ~ carat, data = diamonds)
     m <- lm(price ~ carat, data = diamonds)
     abline(m, col = "red")
 })

Error in get_pkg_srcrefs(pkg) : Unexpected format for source refs.

Thanks!

@gcpoole
Copy link

gcpoole commented May 10, 2022

I was getting this same message. It happened when I was using devtools::load_all(".") while building a package. This makes sense because the package I was working on was not in sync with the most recently built version. When I properly built the package (install and restart under build menu in RStudio), the error was resolved.

Perhaps profvis could somehow check to be sure that loaded version is that same as the most recent build and throw a friendlier error?

swpease added a commit to swpease/hotdeckts that referenced this issue Mar 18, 2024
This *appears* correct. I tested it by inspection with the SUGG and CRAM data on a set seed in CV. I think I *could* set up a test for this, but it'd be a lot of work, a time-consuming test, and amount to repeatedly re-doing said test-by-inspection.

As it is, this memoization looks like it cuts the run time of CV down to a quarter of what it was.

refs: https://memoise.r-lib.org/reference/memoise.html#details
https://rdrr.io/r/base/ns-hooks.html
r-lib/profvis#134
r-lib/memoise#79 (comment)
@hadley
Copy link
Member

hadley commented Aug 21, 2024

I haven't seen this problem for a while, so I'm going to assume it's fixed. Please let me know if you see it again and I'll investigate further.

@hadley hadley closed this as completed Aug 21, 2024
@hongyuanjia
Copy link

@hadley I still see this problem when a package contains a function created using checkmate::makeTestFunction(). checkmate::makeTestFunction() creates a new function based on the input function.

Below is a MRE:

  1. First create a package using {usethis}
usethis::create_package("profvis.test")
  1. Create an R file containing functions created using checkmate::makeTestFunction().
writeLines(
"check_same_len <- function(x, y) {
    if (NROW(x) == NROW(y)) TRUE else \"Must have same length\"
}
test_same_len <- checkmate::makeTestFunction(check_same_len)"
,
    "R/check.R"
)
  1. Load the package using devtools::load_all() and run profvis:
devtools::load_all()
profvis::profvis(test_same_len(mtcars, mtcars))
#> Error in get_pkg_srcrefs(pkg) : Unexpected format for source refs.

Digging a little bit deeper, I found that this error is caused by file_contents.R#L113

srcref <- utils::getSrcref(x)

utils::getSrcref(x) returns a list of two elements instead of one:

[[1]]
{

[[2]]
isTRUE(check_same_len(x, y))

Thus srcfile <- attr(srcref, "srcfile", exact = TRUE) will always return NULL in this case.

@hadley
Copy link
Member

hadley commented Oct 6, 2024

Thanks for the reprex, but it works for me 😞 (once I add a Sys.sleep() to check_same_len() so it doesn't run too quickly to profile).

@hadley
Copy link
Member

hadley commented Oct 6, 2024

What version of R are you using?

Does it reproduce for you with this simpler test case?

check_same_len <- function(x, y) {
  if (NROW(x) == NROW(y)) TRUE else "Must have same length"
}
test_same_len <- checkmate::makeTestFunction(check_same_len)
profvis::profvis(test_same_len(mtcars, mtcars))

It's possible due to the line body(new.fun) = parse(text = paste("{", body, "}")) in checkmate::makeTestFunction(). parse() returns an expression-list, but a function body should be a call. It's possible a more recent version of R handles this automatically so I don't see the problem.

@hongyuanjia
Copy link

I'm using R 4.4.0. You are right. The body part is the root cause.

Interesting. It also works fine for me inside R scripts. This error only occurs when using makeTestFunction inside a package and loading it using devtools.

@gcpoole
Copy link

gcpoole commented Oct 7, 2024

@hongyuanjia, this is consistent with the behavior I experienced, described in my May 10, 2022 post. I only experienced the error when using devtools to load a package during development. When I built the package, without modifying the code, and loaded the package, profvis worked just fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants