# This is a cleaned up version of your original function
area_square_km <- function(data, ...) {
data |>
dplyr::mutate(
dplyr::across(
dplyr::all_of(...),
~ .x / 1000000
)
)
}
md_counties <- tigris::counties(state = "MD")
#> Retrieving data for the year 2021
#> | | | 0% | | | 1% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |== | 4% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 9% | |======= | 10% | |======= | 11% | |======== | 11% | |======== | 12% | |========= | 12% | |========= | 13% | |========= | 14% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 16% | |============ | 17% | |============ | 18% | |============= | 18% | |============= | 19% | |============== | 19% | |============== | 20% | |============== | 21% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================ | 24% | |================= | 24% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 29% | |===================== | 30% | |===================== | 31% | |====================== | 31% | |====================== | 32% | |======================= | 32% | |======================= | 33% | |======================= | 34% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 39% | |============================ | 40% | |============================ | 41% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |============================== | 44% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 49% | |=================================== | 50% | |=================================== | 51% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |===================================== | 54% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 57% | |======================================== | 58% | |========================================= | 58% | |========================================= | 59% | |========================================== | 59% | |========================================== | 60% | |========================================== | 61% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 62% | |============================================ | 63% | |============================================ | 64% | |============================================= | 64% | |============================================= | 65% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 66% | |=============================================== | 67% | |=============================================== | 68% | |================================================ | 68% | |================================================ | 69% | |================================================= | 69% | |================================================= | 70% | |================================================= | 71% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |=================================================== | 74% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |========================================================== | 84% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 89% | |=============================================================== | 90% | |=============================================================== | 91% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================= | 94% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
area_square_km(
md_counties$AWATER
)
#> Error in UseMethod("mutate"): no applicable method for 'mutate' applied to an object of class "c('double', 'numeric')"
# Why doesn't it work?
# md_counties$AWATER is a numeric vector
# mutate requires a data frame input
area_square_km(
md_counties
)
#> Error in `stopifnot()`:
#> ℹ In argument: `dplyr::across(dplyr::all_of(...), ~.x/1e+06)`.
#> Caused by error in `across()`:
#> ! Problem while evaluating `dplyr::all_of(...)`.
#> Caused by error in `dplyr::all_of()`:
#> ! argument "x" is missing, with no default
#> Backtrace:
#> ▆
#> 1. ├─global area_square_km(md_counties)
#> 2. │ ├─dplyr::mutate(data, dplyr::across(dplyr::all_of(...), ~.x/1e+06))
#> 3. │ ├─sf:::mutate.sf(data, dplyr::across(dplyr::all_of(...), ~.x/1e+06))
#> 4. │ │ └─sf:::.re_sf(NextMethod(), sf_column_name = sf_column_name, agr)
#> 5. │ │ └─base::stopifnot(...)
#> 6. │ ├─base::NextMethod()
#> 7. │ └─dplyr:::mutate.data.frame(...)
#> 8. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#> 9. │ ├─base::withCallingHandlers(...)
#> 10. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#> 11. │ └─dplyr:::expand_across(dot)
#> 12. │ └─dplyr:::across_setup(...)
#> 13. │ └─tidyselect::eval_select(cols, data = data, error_call = error_call)
#> 14. │ └─tidyselect:::eval_select_impl(...)
#> 15. │ ├─tidyselect:::with_subscript_errors(...)
#> 16. │ │ └─rlang::try_fetch(...)
#> 17. │ │ └─base::withCallingHandlers(...)
#> 18. │ └─tidyselect:::vars_select_eval(...)
#> 19. │ └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#> 20. │ └─tidyselect:::eval_context(expr, context_mask, call = error_call)
#> 21. │ ├─tidyselect:::with_chained_errors(...)
#> 22. │ │ └─rlang::try_fetch(...)
#> 23. │ │ ├─base::tryCatch(...)
#> 24. │ │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 25. │ │ │ └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 26. │ │ │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 27. │ │ └─base::withCallingHandlers(...)
#> 28. │ └─rlang::eval_tidy(as_quosure(expr, env), context_mask)
#> 29. ├─dplyr::all_of(...)
#> 30. │ └─tidyselect:::as_indices_impl(x, vars = vars, strict = TRUE)
#> 31. └─base::.handleSimpleError(...)
#> 32. └─rlang (local) h(simpleError(msg, call))
#> 33. └─handlers[[1L]](cnd)
#> 34. └─rlang::abort(msg, call = call, parent = cnd)
# Why doesn't it work?
# ... is empty
# all_of requires a string or character vector
# to specify the column names
# This works!
area_square_km(
md_counties,
"AWATER"
)
#> Simple feature collection with 24 features and 17 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -79.48765 ymin: 37.88661 xmax: -74.98628 ymax: 39.72304
#> Geodetic CRS: NAD83
#> First 10 features:
#> STATEFP COUNTYFP COUNTYNS GEOID NAME NAMELSAD LSAD
#> 131 24 047 01668802 24047 Worcester Worcester County 06
#> 170 24 001 01713506 24001 Allegany Allegany County 06
#> 184 24 510 01702381 24510 Baltimore Baltimore city 25
#> 337 24 015 00596115 24015 Cecil Cecil County 06
#> 656 24 005 01695314 24005 Baltimore Baltimore County 06
#> 817 24 013 01696228 24013 Carroll Carroll County 06
#> 875 24 009 01676636 24009 Calvert Calvert County 06
#> 909 24 019 00596495 24019 Dorchester Dorchester County 06
#> 1026 24 003 01710958 24003 Anne Arundel Anne Arundel County 06
#> 1051 24 021 01711211 24021 Frederick Frederick County 06
#> CLASSFP MTFCC CSAFP CBSAFP METDIVFP FUNCSTAT ALAND AWATER
#> 131 H1 G4020 480 41540 <NA> A 1213156434 586.53111
#> 170 H1 G4020 <NA> 19060 <NA> A 1093489884 14.71093
#> 184 C7 G4020 548 12580 <NA> F 209649327 28.75874
#> 337 H1 G4020 428 37980 48864 A 896912533 185.28126
#> 656 H1 G4020 548 12580 <NA> A 1549740652 215.95783
#> 817 H1 G4020 548 12580 <NA> A 1159355859 13.11246
#> 875 H1 G4020 548 47900 47894 A 552158542 341.58067
#> 909 H1 G4020 480 15700 <NA> A 1400573746 1145.35307
#> 1026 H1 G4020 548 12580 <NA> A 1074353889 448.03284
#> 1051 H1 G4020 548 47900 23224 A 1710922224 17.67412
#> INTPTLAT INTPTLON geometry
#> 131 +38.2221332 -075.3099315 MULTIPOLYGON (((-75.28336 3...
#> 170 +39.6123134 -078.7031037 MULTIPOLYGON (((-78.80894 3...
#> 184 +39.3000324 -076.6104761 MULTIPOLYGON (((-76.71151 3...
#> 337 +39.5623537 -075.9415852 MULTIPOLYGON (((-75.77209 3...
#> 656 +39.4431666 -076.6165693 MULTIPOLYGON (((-76.69766 3...
#> 817 +39.5633280 -077.0153297 MULTIPOLYGON (((-76.95782 3...
#> 875 +38.5227191 -076.5297621 MULTIPOLYGON (((-76.41579 3...
#> 909 +38.4291957 -076.0474333 MULTIPOLYGON (((-75.913 38....
#> 1026 +38.9916174 -076.5608941 MULTIPOLYGON (((-76.58029 3...
#> 1051 +39.4701773 -077.3976358 MULTIPOLYGON (((-77.45475 3...
# This is the original code for your function
# Why doesn't it work?
area_square_km_original <- function(x, ...) {
# The input is named `x` but within the function
# you use the variable name `data`
data |>
dplyr::mutate(
dplyr::across(
dplyr::all_of(...),
# When you use the `~` tilde character notation for formulas the input
# is `.x` not `.`
~ . / 1000000
)
)
}Created on 2023-10-18 with reprex v2.0.2