Skip to content

Commit

Permalink
[2023.8]
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce committed Aug 8, 2023
1 parent 9f61326 commit 29fdc28
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
25 changes: 14 additions & 11 deletions R/bruceR_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -926,10 +926,10 @@ file_ext = function(filename) {
#' \itemize{
#' \item Plain text (.txt, .csv, .csv2, .tsv, .psv), using \code{\link[data.table:fread]{data.table::fread()}}
#' \item Excel (.xls, .xlsx), using \code{\link[readxl:read_excel]{readxl::read_excel()}}
#' \item SPSS (.sav), using \code{\link[foreign:read.spss]{foreign::read.spss()}};
#' if failed, using \code{\link[haven:read_spss]{haven::read_sav()}} instead
#' \item Stata (.dta), using \code{\link[foreign:read.dta]{foreign::read.dta()}};
#' if failed, using \code{\link[haven:read_dta]{haven::read_dta()}} instead
#' \item SPSS (.sav), using \code{\link[haven:read_spss]{haven::read_sav()}}
#' or \code{\link[foreign:read.spss]{foreign::read.spss()}}
#' \item Stata (.dta), using \code{\link[haven:read_dta]{haven::read_dta()}}
#' or \code{\link[foreign:read.dta]{foreign::read.dta()}}
#' \item R objects (.rda, .rdata, .RData), using \code{\link[base:load]{base::load()}}
#' \item R serialized objects (.rds), using \code{\link[base:readRDS]{base::readRDS()}}
#' \item Clipboard (on Windows and Mac OS), using \code{\link[clipr:read_clip_tbl]{clipr::read_clip_tbl()}}
Expand All @@ -953,10 +953,13 @@ file_ext = function(filename) {
#' You may specify it as \code{range="A1:E100"} or \code{range="Sheet1!A1:E100"}.
#' @param pkg [Only for SPSS & Stata] Use which R package to read
#' SPSS (.sav) or Stata (.dta) data file?
#' Defaults to using \code{foreign}; if failed, then using \code{haven}.
#' Users may also specify one of them to use.
#' Defaults to \code{"haven"}. You may also use \code{"foreign"}.
#'
#' Notably, \code{"haven"} may be preferred because it is more robust to
#' non-English characters and can also keep variable labels (descriptions)
#' from SPSS.
#' @param value.labels [Only for SPSS & Stata] Convert variables with value labels
#' into R factors with those levels? Defaults to \code{TRUE}.
#' into R factors with those levels? Defaults to \code{FALSE}.
#' @param as Class of the imported data.
#' Defaults to \code{"data.frame"}.
#' Ignored if the file is an R data object (.rds, .rda, .rdata, .RData).
Expand Down Expand Up @@ -994,8 +997,8 @@ import = function(
header="auto",
sheet=NULL,
range=NULL,
pkg=c("foreign", "haven"),
value.labels=TRUE,
pkg=c("haven", "foreign"),
value.labels=FALSE,
as="data.frame",
verbose=FALSE
) {
Expand Down Expand Up @@ -1050,7 +1053,7 @@ import = function(
} else if(fmt %in% c("sav")) {
error = TRUE
if(pkg[1]=="foreign") {
# default & pkg="foreign"
# pkg="foreign"
installed("foreign")
try({
data = foreign::read.spss(
Expand All @@ -1072,7 +1075,7 @@ import = function(
} else if(fmt %in% c("dta")) {
error = TRUE
if(pkg[1]=="foreign") {
# default & pkg="foreign"
# pkg="foreign"
installed("foreign")
try({
data = foreign::read.dta(file=file, convert.factors=value.labels)
Expand Down
21 changes: 12 additions & 9 deletions man/import.Rd

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

0 comments on commit 29fdc28

Please sign in to comment.