Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created November 2, 2022 15:42
Show Gist options
  • Save walkerke/d8cc1e921b8947eda8f5a58e13e700b4 to your computer and use it in GitHub Desktop.
Save walkerke/d8cc1e921b8947eda8f5a58e13e700b4 to your computer and use it in GitHub Desktop.
library(tigris)
library(mapboxapi)
library(gganimate)
library(tidyverse)
library(sf)
library(showtext)
options(tigris_use_cache = TRUE)
animation::ani.options(ani.width = 700, ani.height = 550)
font_add_google("Roboto", "Roboto")
showtext_auto()
cook_roads <- roads("IL", "Cook")
chicago_hall <- mb_geocode("121 N La Salle St, Chicago, IL", output = "sf") %>%
st_transform(st_crs(cook_roads))
cook_roads$dist <- as.numeric(st_distance(cook_roads, chicago_hall))
cook_roads$dist_km <- cook_roads$dist / 1000
cook_roads$dist_round <- round(cook_roads$dist, -3)
ggplot(cook_roads, aes(color = dist_km)) +
geom_sf(linewidth = 0.1) +
theme_void(base_family = "Roboto", base_size = 10) +
scale_color_viridis_c(option = "plasma") +
labs(color = "Distance (km) ",
title = "Cook County roads by distance to Chicago City Hall",
subtitle = "@kyle_e_walker | tigris / gganimate R packages",
caption = "") +
theme(legend.position = "bottom", legend.direction = "horizontal",
legend.key.width = unit(1.5, "cm")) +
transition_states(dist_round) +
shadow_mark()
@JamshidSod
Copy link

After this line of code cook_roads <- roads("IL", "Cook") it says:
Using FIPS code '17' for state 'IL'
Using FIPS code '031' for 'Cook County'
Previous download failed. Re-download attempt 1 of 3...
|=================================================================================| 100%
Previous download failed. Re-download attempt 2 of 3...
|=================================================================================| 100%
Previous download failed. Re-download attempt 3 of 3...
|=================================================================================| 100%
Error: Download failed; check your internet connection or the status of the Census Bureau website
at http://www2.census.gov/geo/tiger/.

When visiting directly it states:
You don't have permission to access "http://www2.census.gov/" on this server.
Reference #18.b5fcc317.1667409239.ff607e8

Any solutions?

@walkerke
Copy link
Author

walkerke commented Nov 2, 2022

I'm guessing you have a very old version of the tigris package installed given that it is trying to download over http rather than https. Try re-installing the package and running the code again.

@JamshidSod
Copy link

Latest version of tigris is installed but the same error appears. I removed and reinstalled tigris package. Any ideas?

sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.0

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] showtext_0.9-5 showtextdb_3.0 sysfonts_0.8.8 sf_1.0-8 forcats_0.5.2
[6] stringr_1.4.1 dplyr_1.0.10 purrr_0.3.5 readr_2.1.3 tidyr_1.2.1
[11] tibble_3.1.8 tidyverse_1.3.2 gganimate_1.0.8 ggplot2_3.3.6 mapboxapi_0.5
[16] tigris_1.6.1

loaded via a namespace (and not attached):
[1] googledrive_2.0.0 colorspace_2.0-3 ellipsis_0.3.2 class_7.3-20
[5] leaflet_2.1.1 rgdal_1.5-32 base64enc_0.1-3 fs_1.5.2
[9] rstudioapi_0.14 proxy_0.4-27 farver_2.1.1 remotes_2.4.2
[13] fansi_1.0.3 lubridate_1.8.0 xml2_1.3.3 codetools_0.2-18
[17] cachem_1.0.6 pkgload_1.3.1 jsonlite_1.8.3 broom_1.0.1
[21] dbplyr_2.2.1 png_0.1-7 shiny_1.7.3 compiler_4.2.2
[25] httr_1.4.4 backports_1.4.1 assertthat_0.2.1 fastmap_1.1.0
[29] gargle_1.2.1 cli_3.4.1 later_1.3.0 tweenr_2.0.2
[33] htmltools_0.5.3 prettyunits_1.1.1 tools_4.2.2 gtable_0.3.1
[37] glue_1.6.2 rappdirs_0.3.3 Rcpp_1.0.9 cellranger_1.1.0
[41] raster_3.6-3 vctrs_0.5.0 crosstalk_1.2.0 ps_1.7.2
[45] rvest_1.0.3 mime_0.12 miniUI_0.1.1.1 lifecycle_1.0.3
[49] devtools_2.4.5 googlesheets4_1.0.1 terra_1.6-17 scales_1.2.1
[53] protolite_2.1.3 hms_1.1.2 promises_1.2.0.1 slippymath_0.3.1
[57] animation_2.7 curl_4.3.3 memoise_2.0.1 aws.signature_0.6.0
[61] stringi_1.7.8 maptools_1.1-5 e1071_1.7-12 pkgbuild_1.3.1
[65] rlang_1.0.6 pkgconfig_2.0.3 lattice_0.20-45 htmlwidgets_1.5.4
[69] processx_3.8.0 tidyselect_1.2.0 magrittr_2.0.3 geojsonsf_2.0.3
[73] R6_2.5.1 magick_2.7.3 generics_0.1.3 profvis_0.3.7
[77] DBI_1.1.3 pillar_1.8.1 haven_2.5.1 foreign_0.8-83
[81] withr_2.5.0 units_0.8-0 sp_1.5-0 modelr_0.1.9
[85] crayon_1.5.2 uuid_1.1-0 KernSmooth_2.23-20 utf8_1.2.2
[89] tzdb_0.3.0 urlchecker_1.0.1 aws.s3_0.3.21 jpeg_0.1-9
[93] progress_1.2.2 usethis_2.1.6 grid_4.2.2 readxl_1.4.1
[97] callr_3.7.3 reprex_2.0.2 digest_0.6.30 classInt_0.4-8
[101] xtable_1.8-4 httpuv_1.6.6 munsell_0.5.0 sessioninfo_1.2.2

@walkerke
Copy link
Author

walkerke commented Nov 2, 2022

It's possible that census.gov is blocked in your location or by your employer, unfortunately. If you can't navigate to census.gov in a web browser, you probably won't be able to use tigris.

@JamshidSod
Copy link

I think so too I'm accessing outside US. Thank you for feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment