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

reprex Error in (function (x) : object 'y' not found #241

Closed
zkamvar opened this issue Feb 7, 2019 · 5 comments

Comments

@zkamvar
Copy link

commented Feb 7, 2019

I'm trying to create a reprex example for a code review and I'm running into a weird issue where I can't create a reprex that seems like it should run. Am I just being thick?

Here's my reprex'd reprex attempt showing simultaneously that reprex works and doesn't work.

reprex::reprex({
  library(dplyr)
  library(rlang)
  headselect <- function(x, y) {
    head(dplyr::select(x, !!rlang::enquo(y)))
  }
  wrapper <- function(x, y) {
    headselect(x, y)
  }
  wrapper2 <- function(x, y) {
    headselect(x, !!rlang::enquo(y))
  }
  headselect(iris, Petal.Length)
  wrapper2(iris, Petal.Length)
  wrapper(iris, Petal.Length)
})
#> Error in (function (x) : object 'y' not found

Created on 2019-02-07 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.2 (2018-12-20)
#>  os       macOS Mojave 10.14.2        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Asia/Seoul                  
#>  date     2019-02-07                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                        
#>  assertthat    0.2.0      2017-04-11 [1] CRAN (R 3.5.0)                
#>  backports     1.1.3      2018-12-14 [1] CRAN (R 3.5.0)                
#>  callr         3.1.1      2018-12-21 [1] CRAN (R 3.5.0)                
#>  cli           1.0.1      2018-09-25 [1] CRAN (R 3.5.0)                
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.5.0)                
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 3.5.0)                
#>  devtools      2.0.1      2018-10-26 [1] CRAN (R 3.5.1)                
#>  digest        0.6.18     2018-10-10 [1] CRAN (R 3.5.0)                
#>  evaluate      0.12       2018-10-09 [1] CRAN (R 3.5.0)                
#>  fs            1.2.6      2018-08-23 [1] CRAN (R 3.5.0)                
#>  glue          1.3.0      2018-07-17 [1] CRAN (R 3.5.0)                
#>  highr         0.7        2018-06-09 [1] CRAN (R 3.5.0)                
#>  htmltools     0.3.6      2017-04-28 [1] CRAN (R 3.5.0)                
#>  knitr         1.21       2018-12-10 [1] CRAN (R 3.5.2)                
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.5.0)                
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.5.0)                
#>  pkgbuild      1.0.2      2018-10-16 [1] CRAN (R 3.5.0)                
#>  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.5.0)                
#>  prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.5.0)                
#>  processx      3.2.1      2018-12-05 [1] CRAN (R 3.5.0)                
#>  ps            1.3.0      2018-12-21 [1] CRAN (R 3.5.0)                
#>  R6            2.3.0      2018-10-04 [1] CRAN (R 3.5.0)                
#>  Rcpp          1.0.0      2018-11-07 [1] CRAN (R 3.5.0)                
#>  remotes       2.0.2      2018-10-30 [1] CRAN (R 3.5.0)                
#>  reprex        0.2.1      2018-09-16 [1] CRAN (R 3.5.0)                
#>  rlang         0.3.1      2019-01-08 [1] CRAN (R 3.5.2)                
#>  rmarkdown     1.11       2018-12-08 [1] CRAN (R 3.5.0)                
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.5.0)                
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.5.1)                
#>  stringi       1.2.4      2018-07-20 [1] CRAN (R 3.5.0)                
#>  stringr       1.3.1      2018-05-10 [1] CRAN (R 3.5.0)                
#>  testthat      2.0.1      2018-10-13 [1] CRAN (R 3.5.0)                
#>  usethis       1.4.0.9000 2018-10-06 [1] Github (r-lib/usethis@1e3c6a6)
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.5.0)                
#>  xfun          0.4        2018-10-23 [1] CRAN (R 3.5.0)                
#>  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.5.0)                
#> 
#> [1] /Users/zhian/R
#> [2] /Library/Frameworks/R.framework/Versions/3.5/Resources/library
@jennybc

This comment has been minimized.

Copy link
Member

commented Feb 7, 2019

Since rlang::enexpr() supports quasiquotation, I don't think you can provide code that uses !! to reprex() as an expression, as you do above. Input that comes as an expression is captured via rlang::enexpr().

This same code can be successfully reprex()ed by using the clipboard or a source file.

cc @lionel-

@jennybc

This comment has been minimized.

Copy link
Member

commented Feb 7, 2019

This makes me wonder if I should be capturing expression input with quote(), as I used to do 🤔

@lionel-

This comment has been minimized.

Copy link
Member

commented Feb 7, 2019

I think you're right, it makes sense to use substitute() in that case.

@jennybc

This comment has been minimized.

Copy link
Member

commented Feb 7, 2019

I think you're right, it makes sense to use substitute() in that case.

Yeah, sorry, that's what I meant and what I used to use. I had just been fiddling with top-level code and had quote() on the mind.

So shall I go back to substitute() here then?

x_expr <- enexpr(x)

@lionel-

This comment has been minimized.

Copy link
Member

commented Feb 7, 2019

Yes substitute() instead of enexpr() there should work exactly the same way, minus the unquoting. The !! should be properly processed by the relevant quoting functions when you evaluate later on.

This will also make the evaluation timing in reprex examples more accurate. Currently the unquoted stuff could depend on local state and go unnoticed because reprex only sees constants.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.