New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generators `labelled()` and `labelled_spss()` don’t support adding `label` attribute #362

Closed
huftis opened this Issue Mar 9, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@huftis
Copy link
Contributor

huftis commented Mar 9, 2018

The labelled() and labelled_spss() generator functions supports adding a labels attribute, but they don’t have a built-in mechanism for adding a label attribute. (The labelattribute hold a short descripton of the variable.) This despite the fact that haven actually has very good support for the label attribute, e.g. when both writing and reading SPSS files.

Suggestion: Add a label argument to the labelled() and labelled_spss() functions. This would add the argument content as a label attribute. Although the argument logically belongs next to the labels argument, it’s probably best to add it as the last argument of the functions, in case people have used argument position when calling the functions.

Here’s a reprex illustrating that 1) the generator functions have no arguments for specifying the label attribute, and 2) exporting and importing to the SPSS format does support the label attribute (and correctly loses ‘unknown’ attributes):

library(haven)
library(tibble)

# No label argument
args(labelled)
#> function (x, labels) 
#> NULL
args(labelled_spss)
#> function (x, labels, na_values = NULL, na_range = NULL) 
#> NULL

# Example data
gen = labelled(c(0, 0, 1, 99), 
               labels = c(Female = 0, Male = 1))
attr(gen, "label") = "Patient gender"
attr(gen, "foo") = "bar"
d = tibble(gen)

# Write to SPSS and read resulting file
fname = tempfile()
write_sav(d, fname)
d2 = read_sav(fname)

# The `label` and `labels` attributes survived the conversion,
# but the `foo` attribute (correctly) did not
str(d2)
#> Classes 'tbl_df', 'tbl' and 'data.frame':    4 obs. of  1 variable:
#>  $ gen:Class 'labelled'  atomic [1:4] 0 0 1 99
#>   .. ..- attr(*, "label")= chr "Patient gender"
#>   .. ..- attr(*, "format.spss")= chr "F8.2"
#>   .. ..- attr(*, "labels")= Named num [1:2] 0 1
#>   .. .. ..- attr(*, "names")= chr [1:2] "Female" "Male"

I could probably create a PR if this is of interest.

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Jun 20, 2018

A PR would be great!

@huftis

This comment has been minimized.

Copy link
Contributor

huftis commented Jun 20, 2018

Will do, @hadley. But PR #353 needs to be merged first, as the needed code changes partially overlap.

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Jun 20, 2018

Ok, just fixed that myself to avoid blocking you.

huftis added a commit to huftis/haven that referenced this issue Jun 20, 2018

`labelled()` and `labelled_spss()` now support the `label` attribute
The constructor functions `labelled()` and `labelled_spss()` now support
adding the `label` attribute to the resulting object. The `label` is a
short, human-readable description of the object, and is now also used
when printing, and can be easily removed using the new `zap_label()`
function. Fixes tidyverse#362.

Previously, the `label` attribute was supported both when reading
and writing SPSS files, but it was not possible to actually create
objects in R having the `label` attribute using the constructors
`labelled()` or `labelled_spss()`.

huftis added a commit to huftis/haven that referenced this issue Jun 27, 2018

`labelled()` and `labelled_spss()` now support the `label` attribute
The constructor functions `labelled()` and `labelled_spss()` now support
adding the `label` attribute to the resulting object. The `label` is a
short, human-readable description of the object, and is now also used
when printing, and can be easily removed using the new `zap_label()`
function. Fixes tidyverse#362.

Previously, the `label` attribute was supported both when reading
and writing SPSS files, but it was not possible to actually create
objects in R having the `label` attribute using the constructors
`labelled()` or `labelled_spss()`.

huftis added a commit to huftis/haven that referenced this issue Jun 27, 2018

`labelled()` and `labelled_spss()` now support the `label` attribute
The constructor functions `labelled()` and `labelled_spss()` now support
adding the `label` attribute to the resulting object. The `label` is a
short, human-readable description of the object, and is now also used
when printing, and can be easily removed using the new `zap_label()`
function. Fixes tidyverse#362.

Previously, the `label` attribute was supported both when reading
and writing SPSS files, but it was not possible to actually create
objects in R having the `label` attribute using the constructors
`labelled()` or `labelled_spss()`.

@hadley hadley closed this in #384 Aug 28, 2018

hadley added a commit that referenced this issue Aug 28, 2018

`labelled()` and `labelled_spss()` now support the `label` attribute (#…
…384)

The constructor functions `labelled()` and `labelled_spss()` now support
adding the `label` attribute to the resulting object. The `label` is a
short, human-readable description of the object, and is now also used
when printing, and can be easily removed using the new `zap_label()`
function. Fixes #362.

Previously, the `label` attribute was supported both when reading
and writing SPSS files, but it was not possible to actually create
objects in R having the `label` attribute using the constructors
`labelled()` or `labelled_spss()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment