Skip to content

Instantly share code, notes, and snippets.

@tylerlittlefield
Last active January 31, 2021 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerlittlefield/55cc7e330c440d2546aa770c9f35fdb9 to your computer and use it in GitHub Desktop.
Save tylerlittlefield/55cc7e330c440d2546aa770c9f35fdb9 to your computer and use it in GitHub Desktop.
Take 538 data and push to database
# 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