Error in `env_get()`

Hello Friends,

Any and all insights are welcome, and my sincere thanks in advance. In brief: RStudio\R is throwing an intermittent Error in get_env() error. reprex is below, along with my sessioninfo(). I updated all packages, restarted the R session, restarted RStudio, and yet the error persists.

If I step through the code, note that the column does exist , but is not found in the pivot_wider() step.

library(tidyverse)
library(tidycensus)

load_variables(
  2020, 
  "acs5"
  ) -> myvars

get_acs(
  geography = "county",
  state = "OH",
  table = "B18101",
  year = 2020,
  geometry = TRUE
  ) %>%
  left_join(
    myvars[, c(1:3)],
    by = c("variable" = "name")
  ) -> b18101_county
#> Getting data from the 2016-2020 5-year ACS
#> Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
                                                             |======================================================================| 100%

b18101_county %>%
  mutate(
    sex = case_when(
      grepl("Male", label) ~ "Male",
      grepl("Female", label) ~ "Female"
    ),
    disability = case_when(
      grepl("With a disability", label) ~ "With a Disability",
      grepl("No disability", label) ~ "No Disability"
    ),
    age = stringr::str_remove_all(
      label,
      "Estimate!!Total:|!!Male:|!!Male:!!|!!Female:|!!Female:!!|:!!No disability|:!!With a disability|!!|:"
    )
  ) %>%
  filter(
    !is.na(sex),
    !is.na(disability)
  ) -> tab02_df

tab02_df %>%
  group_by(NAME) %>%
  mutate(
    total_popn = sum(estimate)
  ) %>%
  group_by(NAME, disability) %>%
  mutate(
    total = sum(estimate)
  ) %>%
  ungroup() -> tab02_df_b

tab02_df_b %>%
  select(NAME, disability, total_popn, total) %>%
  distinct() %>%
  mutate(
    prop = (total / total_popn)
  ) %>%
  group_by(
    NAME, total_popn
  ) -> tab02_df_c 

tab02_df_c %>%
  select(NAME, total_popn, disability, prop) %>%
  group_by(NAME, total_popn) %>%
  pivot_wider(
    names_from = disability,
    values_from = prop
  ) -> tab04
#> Error in `env_get()`:
#> ! object 'disability' not found

#> Backtrace:
#>      ▆
#>   1. ├─... %>% ...
#>   2. ├─tidyr::pivot_wider(., names_from = disability, values_from = prop)
#>   3. ├─sf::pivot_wider.sf(., names_from = disability, values_from = prop)
#>   4. │ └─sf:::.re_sf(NextMethod(), sf_column_name = sf_column_name, agr)
#>   5. │   └─base::stopifnot(...)
#>   6. ├─base::NextMethod()
#>   7. ├─tidyr:::pivot_wider.data.frame(., names_from = disability, values_from = prop)
#>   8. │ └─tidyr::build_wider_spec(...)
#>   9. │   └─tidyselect::eval_select(enquo(names_from), data)
#>  10. │     └─tidyselect:::eval_select_impl(...)
#>  11. │       ├─tidyselect:::with_subscript_errors(...)
#>  12. │       │ ├─base::tryCatch(...)
#>  13. │       │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  14. │       │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  15. │       │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  16. │       │ └─tidyselect:::with_entraced_errors(expr)
#>  17. │       │   └─rlang::try_fetch(...)
#>  18. │       │     └─base::withCallingHandlers(...)
#>  19. │       └─tidyselect:::vars_select_eval(...)
#>  20. │         └─tidyselect:::walk_data_tree(expr, data_mask, context_mask, error_call)
#>  21. │           └─tidyselect:::eval_sym(expr, data_mask, context_mask)
#>  22. │             └─rlang::env_get(env, name, default = missing_arg(), inherit = TRUE)
#>  23. └─base::.handleSimpleError(...)
#>  24.   └─rlang (local) h(simpleError(msg, call))
#>  25.     └─handlers[[1L]](cnd)
#>  26.       └─rlang::abort(conditionMessage(cnd), call = conditionCall(cnd))

tab02_df_c %>%
  select(NAME, total_popn, disability, prop) 
#> Simple feature collection with 176 features and 4 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.82016 ymin: 38.4032 xmax: -80.51869 ymax: 41.97716
#> Geodetic CRS:  NAD83
#> # A tibble: 176 × 5
#> # Groups:   NAME, total_popn [88]
#>    NAME                    total_popn disability  prop                  geometry
#>    <chr>                        <dbl> <chr>      <dbl>        <MULTIPOLYGON [°]>
#>  1 Washington County, Ohio      59550 With a Di… 0.193 (((-81.85293 39.32537, -…
#>  2 Washington County, Ohio      59550 No Disabi… 0.807 (((-81.85293 39.32537, -…
#>  3 Clark County, Ohio          132695 With a Di… 0.167 (((-84.05228 39.86508, -…
#>  4 Clark County, Ohio          132695 No Disabi… 0.833 (((-84.05228 39.86508, -…
#>  5 Hamilton County, Ohio       807777 With a Di… 0.119 (((-84.82016 39.22722, -…
#>  6 Hamilton County, Ohio       807777 No Disabi… 0.881 (((-84.82016 39.22722, -…
#>  7 Cuyahoga County, Ohio      1226557 With a Di… 0.147 (((-81.97097 41.36911, -…
#>  8 Cuyahoga County, Ohio      1226557 No Disabi… 0.853 (((-81.97097 41.36911, -…
#>  9 Pike County, Ohio            27637 With a Di… 0.226 (((-83.38442 39.06027, -…
#> 10 Pike County, Ohio            27637 No Disabi… 0.774 (((-83.38442 39.06027, -…
#> # … with 166 more rows
sessionInfo()
#> R version 4.2.1 (2022-06-23)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur ... 10.16
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.2/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     
#> 
#> loaded via a namespace (and not attached):
#>  [1] rstudioapi_0.14   knitr_1.40        magrittr_2.0.3    R.cache_0.16.0   
#>  [5] rlang_1.0.6       fastmap_1.1.0     fansi_1.0.3       stringr_1.4.1    
#>  [9] styler_1.7.0      highr_0.9         tools_4.2.1       xfun_0.33        
#> [13] R.oo_1.25.0       utf8_1.2.2        cli_3.4.1         withr_2.5.0      
#> [17] htmltools_0.5.3   yaml_2.3.5        digest_0.6.29     tibble_3.1.8     
#> [21] lifecycle_1.0.2   purrr_0.3.4       R.utils_2.12.0    vctrs_0.4.2      
#> [25] fs_1.5.2          glue_1.6.2        evaluate_0.16     rmarkdown_2.16   
#> [29] reprex_2.0.2      stringi_1.7.8     compiler_4.2.1    pillar_1.8.1     
#> [33] R.methodsS3_1.8.2 pkgconfig_2.0.3

Created on 2022-10-05 with reprex v2.0.2

Hi @Ani ,

I was able to get it to work if I pass both disability and prop as strings, so wrap them in quotes.

tab02_df_c %>%
  select(NAME, total_popn, disability, prop) %>%
  group_by(NAME, total_popn) %>% 
  pivot_wider(
    names_from = "disability",
    values_from = "prop"
  ) -> tab04

# Groups:   NAME, total_popn [6]
  NAME                    total_popn                      geometry With …¹ No Di…²
  <chr>                        <dbl>            <MULTIPOLYGON [°]>   <dbl>   <dbl>
1 Washington County, Ohio      59550 (((-81.85293 39.32537, -81.8…   0.193   0.807
2 Clark County, Ohio          132695 (((-84.05228 39.86508, -84.0…   0.167   0.833
3 Hamilton County, Ohio       807777 (((-84.82016 39.22722, -84.8…   0.119   0.881
4 Cuyahoga County, Ohio      1226557 (((-81.97097 41.36911, -81.9…   0.147   0.853
5 Pike County, Ohio            27637 (((-83.38442 39.06027, -83.3…   0.226   0.774
6 Ottawa County, Ohio          40101 (((-82.74167 41.49666, -82.7…   0.156   0.844

Hope that helps! Cool project you're working on.

Thanks @jonesey441 ... that works . I appreciate you finding the solution.

I will have to look into this since not using quotes works in the vignettes and every other time in the same project.

Hi @Ani ,

Glad it worked for you. It would appear it's an issue with the data type. If you run class(tab02_df_c) you'll see it specifies "sf" first. If you convert this to say, a tibble:

tab02_df_c_tib <- as_tibble(tab02_df_c)

tab02_df_c_tib %>%
  select(NAME, total_popn, disability, prop) %>%
  group_by(NAME, total_popn) %>% 
  pivot_wider(
    names_from = disability,
    values_from = prop
  ) -> tab04_tib

You can remove the quotes from the variable names and it works too. If you want to keep your data in the sf format you might just need to use quotes when you get a similar error to the original.

Cheers!

Thanks again @jonesey441 !!

Another alternative is to drop the geometry; then you don't need quotes. Today has been an all-round learning day!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.