Skip to content

Loading…

a bug in pv$visible #26

Merged
merged 3 commits into from

9 participants

@yihui
Collaborator

In evaluate_call(), pv might be an error object when an error occurs in withVisible(output_handler$value(ev$value)), which will in turn trigger this error when we use pv$visible:

Error in x$visible : $ operator is invalid for atomic vectors

A test case has been provided.

I hope the new version can be released as soon as possible, because this bug is pretty annoying. Thanks!

@hadley
Owner

Can you please remove commit 04613b0? That needs to be done in a separate step.

@yihui
Collaborator

Done.

@hadley hadley merged commit 508ad9b into hadley:master
@yonkeltron

Hate to say it but since upgrading to R 3.0, this bug has returned for me when using knitr_1.2 to compile an Rtex document. I get the following error (will gladly provide more info if possible):

Quitting from lines 88-95 (analysis.Rtex)
Error in pv$visible : $ operator is invalid for atomic vectors
Calls: knit ... call_block -> block_exec -> in_dir -> evaluate -> evaluate_call

The output from sessionInfo() as follows:

> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-apple-darwin12.3.0 (64-bit)

locale:
[1] C

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

other attached packages:
[1] knitr_1.2

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.3 formatR_0.7    stringr_0.6.2  tcltk_3.0.0
[6] tools_3.0.0

Please let me know if this provides sufficient information or if I can help move things along in any other way. Thanks!

@yihui
Collaborator

@hadley I think this is why you have to bump the patch version immediately after fixing a significant bug -- now I have no idea by looking at evaluate_0.4.3, and I have to ask if @yonkeltron installed this package from CRAN or Github...

@yonkeltron

@hadley and @yihui I installed this package from CRAN using install.packages this very morning.

@yihui
Collaborator

You need to install from Github at the moment:

library(devtools)
install_github('evaluate', 'hadley')

This bug has been fixed but the new version is not on CRAN yet.

@hadley
Owner

@yihui the official Hadley + Winston way is to increment the version after release to cran (so e.g. 1.2 goes to 1.2.99), but sometimes I forget. A future version of devtools will probably also record the git sha where possible.

@otherdave

Is there a workaround for this? I'm not sure how devtools & GitHub work with R. I saw this problem when using ggplot2 & knitr while trying to do a simple ggplot(...) + geom_bar() plot. Can I suppress some type of output or re-format my graph to get it to work?

@yihui
Collaborator

@otherdave see my comment above; just install the devtools package

@lahvak

I am not sure if I am doing something wrong, I did install the devtools package, and am still getting the error.

@yihui
Collaborator

@lahvak installing devtools is not enough -- you need to install evaluate using devtools:

library(devtools)
install_github('evaluate', 'hadley')
@lahvak

That's what I did, but it seems that the problem was actually caused by an old version of ggplot2. Upgrading that fixed the problem. However, knitr (or evaluate) was still obscuring the error message. I had to run the R code outside of knitr to figure out what was wrong.

@yihui
Collaborator

Interesting. Can you provide a minimal reproducible example? Thanks!

@fzwaeustc

I just ran into the same error when using knitr to knit pdf. Adding the following chunk in knitr-minimal template will trick the error (no chunk option except name is supplied, not shown correctly below).


<>=
  library(ggplot2)
  qplot(carat, price, data = diamonds, colour=color,
        geom = c("point", "smooth"), method="gam", formula = y ~ s(x))
@

but, if we change it to


<>=
  library(ggplot2)
  qplot(carat, price, data = diamonds, colour=color,
        geom = c("point", "smooth"))
@

with method and formula deleted, it works fine.

I think this may be a minimal example I can provide.

Thanks for your great package BTW.

@yihui
Collaborator

That sounds like a ggplot2 problem @wch @hadley

>   library(ggplot2)
Use suppressPackageStartupMessages to eliminate package startup messages.
>   qplot(carat, price, data = diamonds, colour=color,
+         geom = c("point", "smooth"), method="gam", formula = y ~ s(x))
Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'gam' of mode 'function' was not found
Error in if (nrow(layer_data) == 0) return() : argument is of length zero

If you use method="gam", I think you need library(mgcv) for gam() to be visible to ggplot2. At least for me, that works.

@wch

Thanks, it should be fixed now at hadley/ggplot2@b316892

@bbolker

thanks so much for maintenance. Had this problem and using install_github to install newest versions of ggplot2 and evaluate fixed it.

@yihui
Collaborator

@bbolker I'll try to release a new version of evaluate to CRAN in the next week.

@yihui yihui added a commit that referenced this pull request
@yihui yihui issue number of #26 abf3e1c
@rlzijdeman

Hi, I am sorry, but I am still experiencing the: Error in pv$visible: $ operator invisible for atomic vectors. I installed the latest version of ggplot2 and evaluate from github following these instructions:
http://stackoverflow.com/questions/9656016/how-to-install-development-version-of-r-packages-github-repository. I am using Rstudio 0.97.551 on MacOSX if that might have anything to do with it.

##Here's the error output:
label: fig-last (with options)
List of 5
$ fig.width : num 5.2
$ fig.height: num 3
$ out.width : chr ".7\linewidth"
$ fig.keep : chr "last"
$ message : logi FALSE

##Quitting from lines 118-122 (pop5_explore_02.Rnw)
Error in pv$visible : $ operator is invalid for atomic vectors
Calls: knit ... call_block -> block_exec -> in_dir -> evaluate -> evaluate_call

I hope this is useful feedback or perhaps you know where I am going in the wrong direction.

Best,

Richard

@yihui
Collaborator

@rlzijdeman I have released the new version of evaluate (0.4.4) to CRAN yesterday, but it may not have reached to your CRAN mirror yet. Anyway, please show library(evaluate); sessionInfo(), and a minimal reproducible example.

@rlzijdeman

Dear @yihui , my sincere apologies. I created a small dataset, with which the code worked fine. I'm afraid I made a mistake in my own code, which I failed to spot until I made a reproducible example. Thank you for suggesting to do so.

@yihui
Collaborator

@rlzijdeman That is OK :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 5 deletions.
  1. +3 −0 NEWS
  2. +5 −5 R/eval.r
  3. +9 −0 inst/tests/test-evaluate.r
View
3 NEWS
@@ -6,6 +6,9 @@ BUG FIXES
* Perspective plots from `persp()` are captured now (thanks to Harvey Lime
and Yihui Xie)
+* If an error occurs during printing a visible value, evaluate will halt on
+ a cryptic error "operator is invalid for atomic vectors" (fixed by Yihui Xie)
+
Version 0.4.3
------------------------------------------------------------------------------
View
10 R/eval.r
@@ -143,14 +143,14 @@ evaluate_call <- function(call, src = NULL,
# If visible, process and capture output
if (ev$visible) {
- pv <- try(withCallingHandlers(withVisible(output_handler$value(ev$value)),
- warning = wHandler, error = eHandler, message = mHandler), silent = TRUE)
+ pv <- list(value = NULL, visible = FALSE)
+ handle(pv <- withCallingHandlers(withVisible(output_handler$value(ev$value)),
+ warning = wHandler, error = eHandler, message = mHandler))
handle_output(TRUE)
# If return value visible, print and capture output
if (pv$visible) {
- try(withCallingHandlers(print(pv$value),
- warning = wHandler, error = eHandler, message = mHandler),
- silent = TRUE)
+ handle(withCallingHandlers(print(pv$value),
+ warning = wHandler, error = eHandler, message = mHandler))
handle_output(TRUE)
}
}
View
9 inst/tests/test-evaluate.r
@@ -33,6 +33,15 @@ test_that("S4 methods are displayed with show, not print", {
expect_equal(ev[[2]], "B")
})
+test_that("errors during printing visible values are captured", {
+ setClass("A", contains = "function")
+ setMethod("show", "A", function(object) stop("B"))
+ a <- new('A', function() b)
+
+ ev <- evaluate("a")
+ stopifnot("error" %in% class(ev[[2]]))
+})
+
if (dev.interactive()) {
test_that("output and plots interleaved correctly", {
Something went wrong with that request. Please try again.