-
Notifications
You must be signed in to change notification settings - Fork 2
/
write_spss.R
73 lines (68 loc) · 2.65 KB
/
write_spss.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#### Writing files
#############################################################################
#' Write a \code{GADSdat} object to a file
#'
#' Write a \code{GADSdat} object, which contains meta information as value and variable labels to an \code{SPSS} file (\code{sav})
#' or \code{Stata} file (\code{dta}).
#' See 'details' for some important limitations.
#'
#' The provided functionality relies on \code{havens} \code{\link[haven:read_spss]{write_sav}} and
#' \code{\link[haven:read_dta]{write_dta}} functions.
#'
#' Currently known limitations for \code{write_spss} are:
#' \itemize{
#' \item{a) } {value labels for long character variables (> \code{A10}) are dropped,}
#' \item{b) } {under specific conditions very long character variables (> \code{A254}) are incorrectly
#' displayed as multiple character variables in \code{SPSS},}
#' \item{c) } {exporting date or time variables is currently not supported,}
#' \item{d) } {missing tags are slightly incompatible between \code{SPSS} and \code{eatGADS}
#' as \code{eatGADS} supports unlimited discrete missing tags (but no range of missing tags) and
#' \code{SPSS} only supports up to three discrete missing tags or ranges of missing tags. For this purpose, if a variable
#' is assigned more than three discrete missing tags, \code{write_spss()} (more precisely \code{\link{export_tibble}})
#' performs a silent conversion of the discrete missing tags into a missing range.
#' If this conversion affects other value labels or values in the data not tagged as missing, an error is issued.}
#'}
#'
#' Currently known limitations for \code{write_stata} are:
#' \itemize{
#' \item{a) }{Variable format is dropped,}
#' \item{b) }{missing codes are dropped.}
#' }
#'
#'@param GADSdat A \code{GADSdat} object.
#'@param filePath Path of \code{sav} file to write.
#'
#'@return Writes file to disc, returns \code{NULL}.
#'
#'@examples
#'
#'# write to spss
#'tmp <- tempfile(fileext = ".sav")
#'write_spss(pisa, tmp)
#'
#'# write to stata
#'tmp <- tempfile(fileext = ".dta")
#'write_stata(pisa, tmp)
#'
#'@export
write_spss <- function(GADSdat, filePath) {
UseMethod("write_spss")
}
#'@export
write_spss.GADSdat <- function(GADSdat, filePath) {
df <- export_tibble(GADSdat = GADSdat)
haven::write_sav(df, path = filePath)
return()
}
#' @rdname write_spss
#'@export
write_stata <- function(GADSdat, filePath) {
UseMethod("write_stata")
}
#'@export
write_stata.GADSdat <- function(GADSdat, filePath) {
df <- export_tibble(GADSdat = GADSdat)
warning("Missing codes and variable formats are dropped when writing to '.dta'.")
haven::write_dta(df, path = filePath)
return()
}