Last active
January 31, 2021 20:03
-
-
Save tylerlittlefield/55cc7e330c440d2546aa770c9f35fdb9 to your computer and use it in GitHub Desktop.
Take 538 data and push to database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install.packages('fivethirtyeightdata', repos = 'https://fivethirtyeightdata.github.io/drat/', type = 'source') | |
# install.packages("fivethirtyeight") | |
# connect | |
con <- DBI::dbConnect(odbc::odbc(), "fivethirtyeight") | |
# get dataset names from each package | |
x <- tibble::as_tibble(data(package = "fivethirtyeight")$results) | |
y <- tibble::as_tibble(data(package = "fivethirtyeightdata")$results) | |
# upload first set | |
lapply(x$Item, function(i) { | |
tryCatch(expr = { | |
message("* Writing <", i, ">") | |
dat <- eval(parse(text = paste0("fivethirtyeight::", i))) | |
dat <- as.data.frame(dplyr::mutate_if(dat, is.factor, as.character)) | |
DBI::dbWriteTable(con, i, dat, overwrite = TRUE) | |
}, error = function(e) { | |
message("! Failed to write, removing <", i, ">") | |
tryCatch(expr = { | |
DBI::dbRemoveTable(con, i) | |
}, error = function(e) { | |
e | |
}) | |
}) | |
}) | |
# upload second set | |
lapply(y$Item, function(i) { | |
tryCatch(expr = { | |
message("* Writing <", i, ">") | |
dat <- eval(parse(text = paste0("fivethirtyeightdata::", i))) | |
dat <- as.data.frame(dplyr::mutate_if(dat, is.factor, as.character)) | |
DBI::dbWriteTable(con, i, dat, overwrite = TRUE) | |
}, error = function(e) { | |
message("! Failed to write, removing <", i, ">") | |
tryCatch(expr = { | |
DBI::dbRemoveTable(con, i) | |
}, error = function(e) { | |
e | |
}) | |
}) | |
}) | |
# disconnect | |
DBI::dbDisconnect(con) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment