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

Add label_abs_api_data function #258

Closed
wants to merge 2 commits into from

Conversation

justinduytran
Copy link

Function that labels read_api() data with the metadata from read_api_datastructure().

I found myself writing this function because I was constantly going back to read the datastructure labels while working with the api. The code should be fairly self-explanatory. I've avoided the use of non-base R as much as possible however had to use dplyr::relocate() unfortunately.

I'm open to improvements and suggestions, especially since it runs a little slow. It could be turned into a wrapper for read_api() and read_api_datastructure() but I've kept the scope small for now.

Hopefully it's a useful function, it's my first PR :D

Function that labels read_api() data with the metadata from read_api_datastructure.
Rework to remove for loops
Rename .R file to label_abs_api_data.R
Minor formatting
@MattCowgill
Copy link
Owner

Thanks so much @justinduytran! I really appreciate the contribution.

@MattCowgill
Copy link
Owner

Sorry for the delay in reviewing this @justinduytran. I haven't forgotten. Will get to it ASAP. Thanks again

@justinduytran
Copy link
Author

No problem. I'm not sure why the checks are throwing this error:

checking R code for possible problems ... NOTE
  label_abs_api_data : label_variables: no visible binding for global
    variable ‘var’
  Undefined global functions or variables:
    var
  Consider adding
    importFrom("stats", "var")
  to your NAMESPACE file.

Something to do with var being a restricted variable name? I've had to use it as var is the name of the column generated from read_api_datastructure() that is the basis for the label matching.

@justinduytran
Copy link
Author

I have just discovered labelled::unlabelled() functionally does the same thing. The only difference is it converts existing columns to their 'label' rather than creating a new one. It is possible to recreate my function in a simpler manner using labelled::to_factor() but given unlabelled() exists I think my function is redundant.

@MattCowgill
Copy link
Owner

Hey @justinduytran sorry for the delay in getting to this.
I'd be really keen for a PR in future if you come across any other functionality you think {readabs} is lacking!

FYI the error above is because using non-standard evaluation within functions throws an error when running R CMD check, even if the functions work for the user. To get around this, you can do df$var or use the {rlang} .data prefix as in .data$var (this works well in pipes and tidyverse functions).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants