Skip to content
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 · 4 comments
Labels
feature a feature request or enhancement wip work in progress

Comments

@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
Copy link
Member

hadley commented Jun 20, 2018

A PR would be great!

@hadley hadley added feature a feature request or enhancement wip work in progress labels Jun 20, 2018
@huftis
Copy link
Contributor Author

huftis commented Jun 20, 2018

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

@hadley
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
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
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
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 pushed a commit that referenced this issue Aug 28, 2018
…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()`.
@lock
Copy link

lock bot commented Feb 24, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Feb 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement wip work in progress
Projects
None yet
Development

No branches or pull requests

2 participants