ajdamico / asdfree

analyze survey data for free
http://asdfree.com/
GNU General Public License v3.0
612 stars 449 forks source link

batch conversion tutorial with NPPES #140

Open ajdamico opened 8 years ago

ajdamico commented 8 years ago

write a markdown document/script as a blogpost. how to send more than 1,000 batch addresses through http://geocoding.geo.census.gov/geocoder/

ajdamico commented 6 years ago

the POST command looks like this

columns_to_import <-
    c( 
        "npi" , 
        "provider_first_line_business_practice_location_address" , 
        "provider_business_practice_location_address_city_name" ,
        "provider_business_practice_location_address_state_name" ,
        "provider_business_practice_location_address_postal_code"
    )

stopifnot( all( columns_to_import %in% column_names ) )

nppes_df <- 
    data.frame( 
        readr::read_csv( 
            file.path( tempdir() , 
                "nppes.csv" ) , 
            col_names = columns_to_import , 
            col_types = 
                paste0( 
                    ifelse( column_names %in% columns_to_import , column_types , '_' ) , 
                    collapse = "" 
                ) ,
            skip = 1
        ) 
    )

practice_locations <- paste0( tempfile() , ".csv" )

write.table( nppes_df[ 1:10000 , ] , practice_locations , col.names = FALSE , row.names = FALSE , sep = "," , quote = FALSE , na = '' )

system.time( result <- POST("https://geocoding.geo.census.gov/geocoder/locations/addressbatch?form", body=list(benchmark="9",addressFile=upload_file(practice_locations, "text/csv"))) )

tf <- tempfile()
writeBin( content( result ) , tf )
x <- read.csv( tf , header = FALSE )