####################
# Inserting Data #
####################
<- "Folder_Name_Here" # <---------- Change this value FIRST!
folder
<- c(
ACSList # "Data Name","DataCode",
# ...
) ACSListToCSV(ACSList,folder)
##########################################
# Global Variables That can be Changed #
##########################################
#To change the get_acs() geography variable
<- "county"
geographyType
#To change the get_acs() servay variable
<- "ACS5"
servayType
#Change to NULL if no state
<- "IA"
stateType
#This will make a geometry file as well if TRUE
<- FALSE
withGeometry
#checking the year
= NULL
year
###################################
# Functions that make life easy #
###################################
#Imports
library(tidycensus) #For ACS extractions
library(stringi) #For folderNameFixer()
library(tigris)
options(tigris_use_cache = TRUE)
#File name changers. This will set the name of the file. Feel free to edit this
<- function(tableTitle, tableCode, isGeometric){
renameFile <- paste(tableTitle, " (", tableCode, sep='')
fileName if(isGeometric){
= paste(fileName, ", ",capFirst("tract"), sep='')
fileName else {
} = paste(fileName, ", ",capFirst(geographyType), sep='')
fileName
}if(is.null(stateType) == FALSE){
= paste(fileName, ", ", stateType, sep='')
fileName
}if(is.null(year) == FALSE){
= paste(fileName, ", ", year, sep='')
fileName
}= paste(fileName, ", ", servayType, sep='')
fileName if(isGeometric){
= paste(fileName, ", Geometry).csv", sep='')
fileName else {
} = paste(fileName, ").csv", sep='')
fileName
}return(fileName)
}
#ACS extractions.
#Feel free to add to this list.
= function(tableCode){
geoACSDataFrame get_acs(
#Add Changes here
geography = "tract",
table = tableCode,
servay = servayType,
state = stateType,
geometry = TRUE
)
}
= function(tableCode){
defaultACSDataFrame get_acs(
#Add Changes here
geography = geographyType,
table = tableCode,
servay = servayType,
state = stateType
)
}
#Conditions for files fell free to edit this
<- function(tableTitle, tableCode){
fileImplications #Add a condition and apply both the ACS extraction and the File name changer
#Example
if(withGeometry){
<- renameFile(tableTitle, tableCode, TRUE)
fileName #output with Geometry
dataToCSV(geoACSDataFrame(tableCode), fileName)
}
<- renameFile(tableTitle, tableCode, FALSE)
fileName #Default
#This Makes the CSV File
#format dataToCSV(your ACS DataFrame, File name changer() )
dataToCSV(defaultACSDataFrame(tableCode), fileName)
}
#For clarity capitalizes the first letter in a string and lowercases the rest (Feel free to Use)
= function(xStr){
capFirst paste(toupper(substring(xStr, 1, 1)), tolower(substring(xStr, 2, nchar(xStr))), sep = "")
}#Validates and fixes folder name string (Feel free to edit)
<- function(xStr, fixType){
nameFixer #Replaces bad characters with ''
<- stri_replace_all_regex(xStr,
xStr pattern=c('/', ':', '\\*', '"', '<', '>', '\\|'),
replacement=c('-', '', '', '', '', '', ''),
vectorize=FALSE)
if(xStr == ""){
if(capFirst(fixType) == "Folder"){
print("Ops the folder name has all bad characters lets fix that")
<- "New_Data_Folder"
xStr
}else{
print("Ops the file name has all bad characters lets fix that")
<- "New_Data_file"
xStr
}
}return(xStr)
}#For bulk downloading tables
<- function(bulkArray, folder){
ACSListToCSV if(length(bulkArray) %% 2){
#Scream if array is odd length. We don't need any mistakes!
print("Somethings Missing. List format should be like c( Data Name, DataCode, ... )")
return("ERROR")
}#Validates folder name
<- nameFixer(folder, "Folder")
folder #Makes a folder if needed
if(!file.exists(folder)){
dir.create(folder)
print("New Folder Made Adding Files")
}<- 1
index #Adds files
while(index < length(bulkArray)){
fileImplications(nameFixer(bulkArray[index], "File"), bulkArray[index+1]) # (title , code)
<- index + 2
index
}print("All Files have been downloaded")
}#ACS Data frame to CSV file
<- function(data, fileName){
dataToCSV #This adds the file to the folder
<- paste(".\\",folder,"\\", fileName, sep='')
path #Makes the CSV
write.csv(data, path)
print(paste("Added File:",fileName))
}
Since I needed to extract a large amount of data from the American Community Survey (ASC) and convert it into a CSV file. In light of this I wrote this code to extract ASC data from the table codes. The Code takes in a list of names that you want the file name to be. Along with the corresponding table code. It also takes in a folder name. It then makes (if needed) and adds the CVS files to the specified folder (For a clean directory). Its important to note that where you run the R file is where a folder is made. I made this with the intentions of it being editable (and hopefully user friendly).