Skip to content

Commit

Permalink
Replace Hmisc sasxport.get with foreign read.xport
Browse files Browse the repository at this point in the history
  • Loading branch information
cjendres1 committed May 2, 2022
1 parent 8e6feda commit 65a32ef
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: nhanesA
Version: 0.6.7
Date: 2022-04-27
Date: 2022-05-02
Title: NHANES Data Retrieval
Author: Christopher J. Endres
Maintainer: Christopher J. Endres <[email protected]>
Expand All @@ -9,7 +9,7 @@ Depends:
R (>= 3.0.0)
Imports:
stringr,
Hmisc (>= 3.17-1),
foreign,
rvest,
magrittr,
xml2,
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export(nhanesSearchVarName)
export(nhanesTableVars)
export(nhanesTables)
export(nhanesTranslate)
importFrom(Hmisc,sasxport.get)
importFrom(foreign,read.xport)
importFrom(magrittr,"%>%")
importFrom(plyr,mapvalues)
importFrom(rvest,html_elements)
Expand Down
18 changes: 8 additions & 10 deletions R/nhanes.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#nhanesA - retrieve data from the CDC NHANES repository
# Christopher J. Endres 04/19/2022
# Christopher J. Endres 05/02/2022
#
nhanesURL <- 'https://wwwn.cdc.gov/Nchs/Nhanes/'
dataURL <- 'https://wwwn.cdc.gov/Nchs/Nhanes/search/DataPage.aspx'
Expand Down Expand Up @@ -304,7 +304,7 @@ nhanesTableVars <- function(data_group, nh_table, details = FALSE, nchar=100, na
#'
#' Use to download NHANES data tables that are in SAS format.
#'
#' @importFrom Hmisc sasxport.get
#' @importFrom foreign read.xport
#' @importFrom stringr str_c
#' @param nh_table The name of the specific table to retrieve.
#' @return The table is returned as a data frame.
Expand All @@ -328,7 +328,7 @@ nhanes <- function(nh_table) {

tf <- tempfile()
download.file(url, tf, mode = "wb", quiet = TRUE)
return(sasxport.get(tf, lowernames=FALSE))
return(read.xport(tf))
},
error = function(cond) {
message(paste("Data set ", nh_table, " is not available"), collapse='')
Expand All @@ -348,7 +348,7 @@ nhanes <- function(nh_table) {
#' DXA data were acquired from 1999-2006.
#'
#' @importFrom stringr str_c
#' @importFrom Hmisc sasxport.get
#' @importFrom foreign read.xport
#' @importFrom utils download.file
#' @param year The year of the data to import, where 1999<=year<=2006.
#' @param suppl If TRUE then retrieve the supplemental data (default=FALSE).
Expand Down Expand Up @@ -393,7 +393,7 @@ nhanesDXA <- function(year, suppl=FALSE, destfile=NULL) {
ok <- suppressWarnings(tryCatch({download.file(url, tf, mode="wb", quiet=TRUE)},
error=function(cond){message(cond); return(NULL)}))
if(!is.null(ok)) {
return(sasxport.get(tf,lowernames=FALSE))
return(read.xport(tf))
} else { return(NULL) }
}
}
Expand All @@ -408,7 +408,7 @@ nhanesDXA <- function(year, suppl=FALSE, destfile=NULL) {
#' Returns attributes such as number of rows, columns, and memory size,
#' but does not return the table itself.
#'
#' @importFrom Hmisc sasxport.get
#' @importFrom foreign read.xport
#' @importFrom stringr str_c
#' @importFrom utils object.size
#' @param nh_table The name of the specific table to retrieve
Expand All @@ -432,7 +432,7 @@ nhanesAttr <- function(nh_table) {
nht <- tryCatch({
nh_year <- .get_year_from_nh_table(nh_table)
url <- str_c(nhanesURL, nh_year, '/', nh_table, '.XPT', collapse='')
tmp <- sasxport.get(url,lowernames=FALSE)
tmp <- read.xport(url)
nhtatt <- attributes(tmp)
nhtatt$row.names <- NULL
nhtatt$nrow <- nrow(tmp)
Expand Down Expand Up @@ -809,9 +809,7 @@ nhanesSearchVarName <- function(varname=NULL, ystart=NULL, ystop=NULL, includerd
#' @details Code translation tables are retrieved via webscraping using rvest.
#' Many of the NHANES data tables have encoded values. E.g. 1 = 'Male', 2 = 'Female'.
#' Thus it is often helpful to view the code translations and perhaps insert the translated values
#' in a data frame. Note that Hmisc supports "labelled" fields. When a translation is applied directly
#' to a column in a data frame, the column class is first converted to 'factor' and then the coded
#' values are replaced with the code translations.
#' in a data frame.
#' @examples
#' nhanesTranslate('DEMO_B', c('DMDBORN','DMDCITZN'))
#' nhanesTranslate('BPX_F', 'BPACSZ', details=TRUE)
Expand Down
4 changes: 1 addition & 3 deletions man/nhanesTranslate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 65a32ef

Please sign in to comment.