tidyverse / reprex

Render bits of R code for sharing, e.g., on GitHub or StackOverflow.
https://reprex.tidyverse.org
Other
741 stars 80 forks source link

Output when following documentation instructions does not match expected result #451

Closed jwhendy closed 1 year ago

jwhendy commented 1 year ago

This is carried over from an issue reported in tidyr.

Hadley asked me for a reprex, I hadn't used this package before, attempted to follow the instructions, and Hadley thought my result was incorrect. I repeated the process and got the same result, so I offered to post it here.

Here is my process. I'm using Rstudio 2023.06.1+437 (2023.06.1+437) with R 4.2.3 (2023-03-15) on Mac OS, 13.6.2.

Let’s say you copy this code onto your clipboard (or, on RStudio Server or Cloud, select it):

library(dplyr)
library(tidyr)

tmp <- data.frame(id1 = c("a", "a", "b", "b"),
                  unused = c(NA, NA, NA, NA),
                  type = c("c", "d", "c", "d"),
                  values = c(1, 2, 3, 4))
tmp %>% pivot_wider(id_cols = -c(type, values, unused), names_from = type, values_from = values)

Then call reprex(), where the default target venue is GitHub:

Now that I've copied, I go back to R console and run reprex().

The relevant bit of GitHub-flavored Markdown is ready to be pasted from your clipboard.

Now I cmd + v to paste:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)

tmp <- data.frame(id1 = c("a", "a", "b", "b"),
                  unused = c(NA, NA, NA, NA),
                  type = c("c", "d", "c", "d"),
                  values = c(1, 2, 3, 4))
tmp %>% pivot_wider(id_cols = -c(type, values, unused), names_from = type, values_from = values)
#> Error in `pivot_wider()`:
#> ! `id_cols` can't select a column already selected by `names_from`.
#> ℹ Column `type` has already been selected.
#> Backtrace:
#>      ▆
#>   1. ├─tmp %>% ...
#>   2. ├─tidyr::pivot_wider(...)
#>   3. ├─tidyr:::pivot_wider.data.frame(., id_cols = -c(type, values, unused), names_from = type, values_from = values)
#>   4. │ └─tidyr:::build_wider_id_cols_expr(...)
#>   5. │   └─tidyr:::select_wider_id_cols(...)
#>   6. │     ├─rlang::try_fetch(...)
#>   7. │     │ └─base::withCallingHandlers(...)
#>   8. │     └─tidyselect::eval_select(...)
#>   9. │       └─tidyselect:::eval_select_impl(...)
#>  10. │         ├─tidyselect:::with_subscript_errors(...)
#>  11. │         │ └─rlang::try_fetch(...)
#>  12. │         │   └─base::withCallingHandlers(...)
#>  13. │         └─tidyselect:::vars_select_eval(...)
#>  14. │           └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#>  15. │             └─tidyselect:::eval_minus(expr, data_mask, context_mask, error_call)
#>  16. │               └─tidyselect:::eval_bang(expr, data_mask, context_mask)
#>  17. │                 └─tidyselect:::walk_data_tree(expr[[2]], data_mask, context_mask)
#>  18. │                   └─tidyselect:::eval_c(expr, data_mask, context_mask)
#>  19. │                     └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#>  20. │                       └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#>  21. │                         └─tidyselect:::as_indices_sel_impl(...)
#>  22. │                           └─tidyselect:::as_indices_impl(...)
#>  23. │                             └─tidyselect:::chr_as_locations(x, vars, call = call, arg = arg)
#>  24. │                               └─vctrs::vec_as_location(...)
#>  25. ├─vctrs (local) `<fn>`()
#>  26. │ └─vctrs:::stop_subscript_oob(...)
#>  27. │   └─vctrs:::stop_subscript(...)
#>  28. │     └─rlang::abort(...)
#>  29. │       └─rlang:::signal_abort(cnd, .file)
#>  30. │         └─base::signalCondition(cnd)
#>  31. ├─rlang (local) `<fn>`(`<vctrs___>`)
#>  32. │ └─handlers[[1L]](cnd)
#>  33. │   └─rlang::cnd_signal(cnd)
#>  34. │     └─rlang:::signal_abort(cnd)
#>  35. │       └─base::signalCondition(cnd)
#>  36. └─rlang (local) `<fn>`(`<vctrs___>`)
#>  37.   └─handlers[[1L]](cnd)
#>  38.     └─tidyr:::rethrow_id_cols_oob(...)
#>  39.       └─tidyr:::stop_id_cols_oob(i, "names_from", call = call)
#>  40.         └─cli::cli_abort(...)
#>  41.           └─rlang::abort(...)

Created on 2023-11-10 with reprex v2.0.2

You can compare with Hadley's output in this comment. Is it just the included backtrace that's unexpected?

Edit: adding sessionInfo():

> sessionInfo()
R version 4.2.3 (2023-03-15)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.6.2

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

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] reprex_2.0.2

loaded via a namespace (and not attached):
 [1] rstudioapi_0.14   knitr_1.42        magrittr_2.0.3    R.cache_0.16.0    R6_2.5.1          rlang_1.1.1       fastmap_1.1.1    
 [8] fansi_1.0.4       styler_1.10.2     tools_4.2.3       xfun_0.39         R.oo_1.25.0       utf8_1.2.3        cli_3.6.1        
[15] clipr_0.8.0       withr_2.5.0       htmltools_0.5.5   yaml_2.3.7        digest_0.6.31     tibble_3.2.1      lifecycle_1.0.3  
[22] processx_3.8.2    callr_3.7.3       purrr_1.0.1       ps_1.7.5          vctrs_0.6.3       R.utils_2.12.2    fs_1.6.1         
[29] glue_1.6.2        evaluate_0.20     rmarkdown_2.21    compiler_4.2.3    pillar_1.9.0      R.methodsS3_1.8.2 pkgconfig_2.0.3  
hadley commented 1 year ago

The output looks fine when you copied and pasted it this time.

jwhendy commented 1 year ago

Bah, I apologize. Given your feedback @hadley I think your observation was just on the footnote, then (that's all that seems different). I was comparing to what you pasted, so I thought it was that mine had a ton of other content vs. yours was super short.

If it's just about the footnote, I recall having gotten tripped up by the preview render, likely due to me having inserted backticks before pasting, then pasting (and reprex includes its own backticks) and then I went to clean things up and deleted the wrong set (before the footnote).

Does that all track? If so, sorry for the time waste, I think this was just user error + confusion over what, exactly, wasn't looking right to you (and me to inexperienced to know).