Skip to content

Commit

Permalink
adds database append functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanrussellmd committed Jun 5, 2020
1 parent de66ae2 commit 05b5e58
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
linters: with_defaults(line_length_linter(120))
exclusions: list()
59 changes: 30 additions & 29 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
Package: nsqipr
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person(given = "Dylan",
family = "Russell",
role = c("aut", "cre"),
email = "dyl.russell@gmail.com",
comment = c(ORCID = "0000-0002-9543-9897"))
Description: What the package does (one paragraph).
License: CC0
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
Suggests:
knitr,
rmarkdown,
testthat,
spelling
VignetteBuilder: knitr
URL: https://github.com/doctortickle/nsqipr
BugReports: https://github.com/doctortickle/nsqipr/issues
Language: en-US
Imports:
stringr,
DBI,
odbc,
readr
Package: nsqipr
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person(given = "Dylan",
family = "Russell",
role = c("aut", "cre"),
email = "dyl.russell@gmail.com",
comment = c(ORCID = "0000-0002-9543-9897"))
Description: What the package does (one paragraph).
License: CC0
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
Suggests:
knitr,
rmarkdown,
testthat,
spelling
VignetteBuilder: knitr
URL: https://github.com/doctortickle/nsqipr
BugReports: https://github.com/doctortickle/nsqipr/issues
Language: en-US
Imports:
stringr,
DBI,
odbc,
readr,
magrittr
76 changes: 40 additions & 36 deletions R/dbcon.R
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
#conn <- DBI::dbconnnect(odbc::odbc(),
#conn <- DBI::dbConnect(odbc::odbc(),
# Driver = nsqip_db_driver,
# Server = nsqip_db_ip, # Public IP of the PostgreSQL database
# Server = nsqip_db_ip,
# Database = nsqip_db,
# UID = nsqip_db_user,
# PWD = nsqip_db_pw,
# Port = nsqip_db_port) # odbc() allows interface with RStudio connnections pane.
# Port = nsqip_db_port
#)

connDb <- function(driver, ip, db, port, user, pw) {
DBI::dbconnnect(odbc::odbc(),
Driver = driver,
Server = ip,
Databse = db,
Port = port,
UID = user,
PWD = pw
)
}

addToNsqip <- function(file) {
df <- readr::read_delim(file,
delim = "\t",
na = c("","NA","-99","NULL")
)
DBI::dbCreateTable(conn,
name = 'puf',
fields = df)
DBI::dbAppendTable(conn,
name = 'puf',
value = df)
}
#conn <- function(driver, ip, db, port, user, pw) {
# DBI::dbConnect(odbc::odbc(),
# Driver = driver,
# Server = ip,
# Databse = db,
# Port = port,
# UID = user,
# PWD = pw
# )
#}

importData <- function(driver, ip, db, port, user, pw, dir) {
conn <- connDb(driver, ip, db, port, user, pw)
files <- list.files(path = dir, pattern = "*.txt", full.names = TRUE, recursive = FALSE)
lapply(files, writeData, conn)
import_data_dir <- function(conn, dir) {
if (file_test("-d", dir)) {
files <- list.files(path = dir, pattern = "*.txt",
full.names = TRUE, recursive = FALSE)
lapply(files, write_data, conn)
} else if (file_test("-f", dir)) {
write_data(conn, dir)
} else {
stop(paste(dir,'is an invalid file or directory path.', sep = " "))
}
}

writeData <- function(file, conn) {
createDF(file)
write_data <- function(conn, file) {
df <- create_df(file)
tablename <- parse_filename(file)
if (!DBI::dbExistsTable(conn, tablename)) {
DBI::dbCreateTable(conn, tablename, df)
}
DBI::dbAppendTable(conn, tablename, df)
}

parseFilename <- function(file) {
pattern <- stringr::regex("acs_nsqip_puf|puf_tar_[a-z]{1,4}", ignore_case = TRUE)
parse_filename <- function(file) {
pattern <- stringr::regex("acs_nsqip_puf|puf_tar_[a-z]{1,4}",
ignore_case = TRUE)
stopifnot(stringr::str_detect(file, pattern))
tablename <- stringr::str_extract(file, pattern)
stringr::str_to_lower(tablename)
}

assertTableExists <- function(conn, tablename) {
stopifnot(DBI::dbExistsTable(conn, tablename))
create_df <- function(file) {
nas <- c("", "NA", "-99","NULL")
df <- readr::read_tsv(file, na = nas, guess_max = 30000,
trim_ws = TRUE) %>%
dplyr::rename_all(tolower) %>%
dplyr::mutate(dplyr::across(where(is.character), stringr::str_to_sentence))
}
11 changes: 11 additions & 0 deletions R/utils-pipe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#' Pipe operator
#'
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL
44 changes: 22 additions & 22 deletions nsqipr.Rproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
Version: 1.0
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
2 changes: 1 addition & 1 deletion tests/spelling.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
if(requireNamespace('spelling', quietly = TRUE))
if (requireNamespace("spelling", quietly = TRUE))
spelling::spell_check_test(vignettes = TRUE, error = FALSE,
skip_on_cran = TRUE)
11 changes: 5 additions & 6 deletions tests/testthat/test-dbcon.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
testthat::test_that("parseFilename correctly picks out the table name", {
testthat::expect_equal(parseFilename("puf_tar_cea_2015.txt"), "puf_tar_cea")
testthat::expect_equal(parseFilename('PUF_TAR_CEA_2015.txt'), "puf_tar_cea")
testthat::expect_equal(parseFilename('puf_tar_gyne_2015.txt'), "puf_tar_gyne")
testthat::expect_equal(parseFilename('PUF_TAR_GYNE_2015.txt'), "puf_tar_gyne")
testthat::expect_equal(parseFilename('puf_tar_hep_update_2015.txt'), "puf_tar_hep")
testthat::expect_equal(parseFilename('PUF_TAR_HEP_UPDATE_2015.TXT'), "puf_tar_hep")
testthat::expect_equal(parseFilename("PUF_TAR_CEA_2015.txt"), "puf_tar_cea")
testthat::expect_equal(parseFilename("puf_tar_gyne_2015.txt"), "puf_tar_gyne")
testthat::expect_equal(parseFilename("PUF_TAR_GYNE_2015.txt"), "puf_tar_gyne")
testthat::expect_equal(parseFilename("puf_tar_hep_update_2015.txt"), "puf_tar_hep")
testthat::expect_equal(parseFilename("PUF_TAR_HEP_UPDATE_2015.TXT"), "puf_tar_hep")
testthat::expect_equal(parseFilename("acs_nsqip_puf_05_06_vr1.txt"), "acs_nsqip_puf")
testthat::expect_equal(parseFilename("ACS_NSQIP_PUF_05_06_vr1.txt"), "acs_nsqip_puf")
testthat::expect_equal(parseFilename("acs_nsqip_puf18.txt"), "acs_nsqip_puf")
testthat::expect_equal(parseFilename("ACS_NSQIP_PUF18.txt"), "acs_nsqip_puf")
testthat::expect_equal(parseFilename("ACS_NSQIP_PUF_05_06_vr1.txt"), "acs_nsqip_puf")
})

0 comments on commit 05b5e58

Please sign in to comment.