Select column that are exactly the names captured unevaluated from .... This is to provide a simple interface that reliably uses non-standard captured names (and not consequences of further evaluation). Please see https://win-vector.com/2018/09/23/a-subtle-flaw-in-some-popular-r-nse-interfaces/ for some discussion. Also accepts -name notation, but not integers or functions of columns. Does not look at argument names (so can not be used to rename columns).

select_nse(.data, ...)

Arguments

.data

data frame or tbl to select columns from.

...

unevaluated symbols to use as column names.

Examples

y <- "x" # returns y-column dplyr::select(data.frame(x = 1, y = 2), y)
#> y #> 1 2
# returns x-column (very confusing!) dplyr::select(data.frame(x = 1), y)
#> Note: Using an external vector in selections is ambiguous. #> Use `all_of(y)` instead of `y` to silence this message. #> See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>. #> This message is displayed once per session.
#> x #> 1 1
# returns y-column select_nse(data.frame(x = 1, y = 2), y)
#> y #> 1 2
# deletes wrong column! dplyr::select(data.frame(x = 1, z = 3), -y)
#> z #> 1 3
# throws when y is not the name of a column (good) tryCatch( select_nse(data.frame(x = 1), y), error = function(e) { e } )
#> <error/vctrs_error_subscript_oob> #> Can't subset columns that don't exist. #> x Column `y` doesn't exist. #> Backtrace: #> 1. pkgdown::build_site(new_process = FALSE) #> 2. pkgdown:::build_site_local(...) #> 3. pkgdown::build_reference(...) #> 4. purrr::map(...) #> 5. pkgdown:::.f(.x[[i]], ...) #> 7. pkgdown:::data_reference_topic(...) #> 8. pkgdown:::run_examples(...) #> 9. pkgdown:::highlight_examples(code, topic, env = env) #> 10. downlit::evaluate_and_highlight(...) #> 11. evaluate::evaluate(code, child_env(env), new_device = TRUE) /Users/johnmount/Documents/work/downlit/R/evaluate.R:25:2 #> 12. evaluate:::evaluate_call(...) #> 22. [ base::eval(...) ] with 1 more call #> 28. seplyr::select_nse(data.frame(x = 1), y) #> 29. seplyr::select_se(.data, colNames) #> 31. dplyr:::select.data.frame(.data, !!!colSyms) #> 32. tidyselect::eval_select(expr(c(...)), .data) #> 33. tidyselect:::eval_select_impl(...) #> 41. tidyselect:::vars_select_eval(...) #> 42. tidyselect:::walk_data_tree(expr, data_mask, context_mask) #> 43. tidyselect:::eval_c(expr, data_mask, context_mask) #> 44. tidyselect:::reduce_sels(node, data_mask, context_mask, init = init) #> 45. tidyselect:::walk_data_tree(new, data_mask, context_mask) #> 46. tidyselect:::as_indices_sel_impl(...) #> 47. tidyselect:::as_indices_impl(x, vars, strict = strict) #> 48. tidyselect:::chr_as_locations(x, vars) #> 49. vctrs::vec_as_location(x, n = length(vars), names = vars) #> 51. vctrs:::stop_subscript_oob(...) #> 52. vctrs:::stop_subscript(...)
#' # throws when y is not the name of a column (good) tryCatch( select_nse(data.frame(x = 1, z = 3), -y), error = function(e) { e } )
#> <error/vctrs_error_subscript_oob> #> Can't subset columns that don't exist. #> x Column `y` doesn't exist. #> Backtrace: #> 1. pkgdown::build_site(new_process = FALSE) #> 2. pkgdown:::build_site_local(...) #> 3. pkgdown::build_reference(...) #> 4. purrr::map(...) #> 5. pkgdown:::.f(.x[[i]], ...) #> 7. pkgdown:::data_reference_topic(...) #> 8. pkgdown:::run_examples(...) #> 9. pkgdown:::highlight_examples(code, topic, env = env) #> 10. downlit::evaluate_and_highlight(...) #> 11. evaluate::evaluate(code, child_env(env), new_device = TRUE) /Users/johnmount/Documents/work/downlit/R/evaluate.R:25:2 #> 12. evaluate:::evaluate_call(...) #> 22. [ base::eval(...) ] with 1 more call #> 28. seplyr::select_nse(data.frame(x = 1, z = 3), -y) #> 29. seplyr::select_se(.data, colNames) #> 31. dplyr:::select.data.frame(.data, !!!colSyms) #> 32. tidyselect::eval_select(expr(c(...)), .data) #> 33. tidyselect:::eval_select_impl(...) #> 41. tidyselect:::vars_select_eval(...) #> 42. tidyselect:::walk_data_tree(expr, data_mask, context_mask) #> 43. tidyselect:::eval_c(expr, data_mask, context_mask) #> 44. tidyselect:::reduce_sels(node, data_mask, context_mask, init = init) #> 45. tidyselect:::walk_data_tree(new, data_mask, context_mask) #> 46. tidyselect:::as_indices_sel_impl(...) #> 47. tidyselect:::as_indices_impl(x, vars, strict = strict) #> 48. tidyselect:::chr_as_locations(x, vars) #> 49. vctrs::vec_as_location(x, n = length(vars), names = vars) #> 51. vctrs:::stop_subscript_oob(...) #> 52. vctrs:::stop_subscript(...)