Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add show_call option to get_*() to print API calls #203

Merged
merged 4 commits into from Nov 16, 2019

Conversation

@mfherman
Copy link
Contributor

mfherman commented Nov 15, 2019

In reference to #194, this adds an option to the three get_*() functions to print the API call that is sent to the Census API. This enables a user to copy and paste the API call into a browser and help to determine if the issue is with tidycensus or the Census API if they are receiving an error.

When the request requires multiple calls to the API because of a large number of variables, all API calls are printed. But, because messages are suppressed internally when making iterating over get_*() calls (required for block groups or multi-state tract level requests), the API calls are not printed.

Below are examples of the API call printing.

Also another commit to change the default year for get_estimates() to 2018 in re #200.

library(tidycensus)

# ACS single API call printed
get_acs(
  geography = "state",
  state = "VT",
  survey = "acs1",
  variables = "B01001_001",
  show_call = TRUE
  )
#> Getting data from the 2017 1-year ACS
#> The one-year ACS provides data for geographies with populations of 65,000 and greater.
#> Census API call: https://api.census.gov/data/2017/acs/acs1?get=B01001_001E%2CB01001_001M%2CNAME&for=state%3A50
#> # A tibble: 1 x 5
#>   GEOID NAME    variable   estimate   moe
#>   <chr> <chr>   <chr>         <dbl> <dbl>
#> 1 50    Vermont B01001_001   623657    NA

# Decennial
get_decennial(
  geography = "state",
  state = "VT",
  variables = "P005003",
  show_call = TRUE)
#> Getting data from the 2010 decennial Census
#> Census API call: https://api.census.gov/data/2010/dec/sf1?get=P005003%2CNAME&for=state%3A50
#> # A tibble: 1 x 4
#>   GEOID NAME    variable  value
#>   <chr> <chr>   <chr>     <dbl>
#> 1 50    Vermont P005003  590223

# Population estimates
get_estimates(
  geography = "county",
  state = "VT",
  county = "Washington",
  time_series = TRUE,
  year = 2018,
  product = "components",
  show_call = TRUE
)
#> Census API call: https://api.census.gov/data/2018/pep/components?get=GEONAME%2CBIRTHS%2CDEATHS%2CDOMESTICMIG%2CINTERNATIONALMIG%2CNATURALINC%2CNETMIG%2CRBIRTH%2CRDEATH%2CRDOMESTICMIG%2CRINTERNATIONALMIG%2CRNATURALINC%2CRNETMIG%2CPERIOD_CODE&for=county%3A023&in=state%3A50
#> # A tibble: 108 x 5
#>    NAME                       PERIOD GEOID variable value
#>    <chr>                       <dbl> <chr> <chr>    <dbl>
#>  1 Washington County, Vermont      1 50023 BIRTHS     174
#>  2 Washington County, Vermont      2 50023 BIRTHS     611
#>  3 Washington County, Vermont      3 50023 BIRTHS     615
#>  4 Washington County, Vermont      4 50023 BIRTHS     541
#>  5 Washington County, Vermont      5 50023 BIRTHS     567
#>  6 Washington County, Vermont      6 50023 BIRTHS     576
#>  7 Washington County, Vermont      7 50023 BIRTHS     535
#>  8 Washington County, Vermont      8 50023 BIRTHS     528
#>  9 Washington County, Vermont      9 50023 BIRTHS     518
#> 10 Washington County, Vermont      1 50023 DEATHS     143
#> # … with 98 more rows

# ACS multiple API calls printed when many variables requested
get_acs(
  geography = "state",
  state = "VT",
  table = "B01001",
  show_call = TRUE
  )
#> Getting data from the 2013-2017 5-year ACS
#> Census API call: https://api.census.gov/data/2017/acs/acs5?get=B01001_001E%2CB01001_001M%2CB01001_002E%2CB01001_002M%2CB01001_003E%2CB01001_003M%2CB01001_004E%2CB01001_004M%2CB01001_005E%2CB01001_005M%2CB01001_006E%2CB01001_006M%2CB01001_007E%2CB01001_007M%2CB01001_008E%2CB01001_008M%2CB01001_009E%2CB01001_009M%2CB01001_010E%2CB01001_010M%2CB01001_011E%2CB01001_011M%2CB01001_012E%2CB01001_012M%2CB01001_013E%2CB01001_013M%2CB01001_014E%2CB01001_014M%2CB01001_015E%2CB01001_015M%2CB01001_016E%2CB01001_016M%2CB01001_017E%2CB01001_017M%2CB01001_018E%2CB01001_018M%2CB01001_019E%2CB01001_019M%2CB01001_020E%2CB01001_020M%2CB01001_021E%2CB01001_021M%2CB01001_022E%2CB01001_022M%2CB01001_023E%2CB01001_023M%2CB01001_024E%2CB01001_024M%2CNAME&for=state%3A50
#> Census API call: https://api.census.gov/data/2017/acs/acs5?get=B01001_025E%2CB01001_025M%2CB01001_026E%2CB01001_026M%2CB01001_027E%2CB01001_027M%2CB01001_028E%2CB01001_028M%2CB01001_029E%2CB01001_029M%2CB01001_030E%2CB01001_030M%2CB01001_031E%2CB01001_031M%2CB01001_032E%2CB01001_032M%2CB01001_033E%2CB01001_033M%2CB01001_034E%2CB01001_034M%2CB01001_035E%2CB01001_035M%2CB01001_036E%2CB01001_036M%2CB01001_037E%2CB01001_037M%2CB01001_038E%2CB01001_038M%2CB01001_039E%2CB01001_039M%2CB01001_040E%2CB01001_040M%2CB01001_041E%2CB01001_041M%2CB01001_042E%2CB01001_042M%2CB01001_043E%2CB01001_043M%2CB01001_044E%2CB01001_044M%2CB01001_045E%2CB01001_045M%2CB01001_046E%2CB01001_046M%2CB01001_047E%2CB01001_047M%2CB01001_048E%2CB01001_048M%2CNAME&for=state%3A50
#> Census API call: https://api.census.gov/data/2017/acs/acs5?get=B01001_049E%2CB01001_049M%2CNAME&for=state%3A50
#> # A tibble: 49 x 5
#>    GEOID NAME    variable   estimate   moe
#>    <chr> <chr>   <chr>         <dbl> <dbl>
#>  1 50    Vermont B01001_001   624636    NA
#>  2 50    Vermont B01001_002   308026   318
#>  3 50    Vermont B01001_003    15500   155
#>  4 50    Vermont B01001_004    17015   469
#>  5 50    Vermont B01001_005    17733   463
#>  6 50    Vermont B01001_006    11473   131
#>  7 50    Vermont B01001_007    10734   245
#>  8 50    Vermont B01001_008     5240   456
#>  9 50    Vermont B01001_009     5597   594
#> 10 50    Vermont B01001_010    12933   580
#> # … with 39 more rows

# ACS no API call printed for multi-state tract calls
get_acs(
  geography = "tract",
  state = c("VT", "NH"),
  variables = "B01001_001",
  show_call = TRUE
  )
#> Getting data from the 2013-2017 5-year ACS
#> Fetching tract data by state and combining the result.
#> # A tibble: 479 x 5
#>    GEOID      NAME                                 variable  estimate   moe
#>    <chr>      <chr>                                <chr>        <dbl> <dbl>
#>  1 500019601… Census Tract 9601, Addison County, … B01001_0…     3781   182
#>  2 500019602… Census Tract 9602, Addison County, … B01001_0…     2750    20
#>  3 500019603… Census Tract 9603, Addison County, … B01001_0…     2606    18
#>  4 500019604… Census Tract 9604, Addison County, … B01001_0…     5081   186
#>  5 500019605… Census Tract 9605, Addison County, … B01001_0…     3901    18
#>  6 500019606… Census Tract 9606, Addison County, … B01001_0…     2712   138
#>  7 500019607… Census Tract 9607, Addison County, … B01001_0…     3862   230
#>  8 500019608… Census Tract 9608, Addison County, … B01001_0…     4680   229
#>  9 500019609… Census Tract 9609, Addison County, … B01001_0…     5203   174
#> 10 500019610… Census Tract 9610, Addison County, … B01001_0…     2249   153
#> # … with 469 more rows

# ACS no API call printed for block group calls
get_acs(
  geography = "block group",
  state = "VT",
  variables = "B01001_001",
  show_call = TRUE
  )
#> Getting data from the 2013-2017 5-year ACS
#> 
#> # A tibble: 522 x 5
#>    GEOID      NAME                                 variable  estimate   moe
#>    <chr>      <chr>                                <chr>        <dbl> <dbl>
#>  1 500110101… Block Group 1, Census Tract 101, Fr… B01001_0…     1554   497
#>  2 500110101… Block Group 2, Census Tract 101, Fr… B01001_0…      925   280
#>  3 500110101… Block Group 3, Census Tract 101, Fr… B01001_0…     1148   346
#>  4 500110101… Block Group 4, Census Tract 101, Fr… B01001_0…     2317   236
#>  5 500110101… Block Group 5, Census Tract 101, Fr… B01001_0…     1414   205
#>  6 500110102… Block Group 1, Census Tract 102, Fr… B01001_0…     1711   197
#>  7 500110102… Block Group 2, Census Tract 102, Fr… B01001_0…     1245   180
#>  8 500110102… Block Group 3, Census Tract 102, Fr… B01001_0…     1513   180
#>  9 500110103… Block Group 1, Census Tract 103, Fr… B01001_0…     1094   205
#> 10 500110103… Block Group 2, Census Tract 103, Fr… B01001_0…     1364   233
#> # … with 512 more rows

Created on 2019-11-15 by the reprex package (v0.3.0)

@mfherman

This comment has been minimized.

Copy link
Contributor Author

mfherman commented Nov 16, 2019

In these other commits, I added in a couple helpful error messages that would address issues raised in #197 and #198

library(tidycensus)

load_variables(2010, "sf3")
#> Error: Summary File 3 was not released in 2010. Use tables from the American Community Survey via get_acs() instead.

get_acs(
  geography = "state",
  state = "VT",
  table = c("B01001", "B01003"),
  show_call = TRUE
)
#> Error: Only one table may be requested per call.

Created on 2019-11-15 by the reprex package (v0.3.0)

@walkerke

This comment has been minimized.

Copy link
Owner

walkerke commented Nov 16, 2019

awesome work, thanks @mfherman!!!

@walkerke walkerke merged commit 739f28b into walkerke:master Nov 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.