sdcTools / sdcMicro

sdcMicro
http://sdctools.github.io/sdcMicro/
79 stars 23 forks source link

haven version 2.3.0 causes error on reading SPSS and STATA files in sdcApp #298

Closed thijsbenschop closed 4 years ago

thijsbenschop commented 4 years ago

The new haven release (2.3.0) adds the classes "vctrs_vctr" and "double" besides the class "haven_labelled" to factor variables. See example below (I can't add a STATA file for testing, but this happens to any STATA or SPSS file with labelled variables upon loading in R using read_sav or read_dta from the haven 2.3.0 package).

> class(test$gender)
[1] "haven_labelled" "vctrs_vctr"     "double"

The additional classes lead to a failure to detect the variables of class haven_labelled in line 458 of GUIfunctions.R.

#cl_lab <- which(sapply(res, class) %in% c("labelled", "haven_labelled"))

Therefore, the variables of class haven_labelled are not converted to class factor, which in turn causes an error upon loading and processing the files in sdcApp.

Screenshot 2020-06-15 at 12 07 49 Screenshot 2020-06-15 at 12 07 13

A solution is to ignore other classes of a variable besides the first class, e.g., by using

cl_lab <- which(sapply(test, function(var){class(var)[1]}) %in% c("labelled", "haven_labelled"))

Then variables of class haven_labelled will be properly detected and converted to class factor.

thijsbenschop commented 4 years ago

See pull request #299

bernhard-da commented 4 years ago

@thijsbenschop thx for reporting, fixed.