Skip to content

Data frames code autocomplete, also search for Hmisc labels or other metadata instead of just the variable names? #14242

Closed

Description

@BdR76

We have a lot of medical data frames which have standardized variable names, for example variable NL_CM_7_2_9 is "Type Of Tobacco Used", NL_CM_7_2_10 is "Tobacco Use Status" and so on (also see here).

When working on the R scripts for these type of dataframes, it's hard to find the correct variable using the Rstudio auto-complete because of these coded names.

I know we could rename the variables to more meaningful names using dplyr::rename(), but that's not workable because of standardization and for compatibility with scripts/datasets with other sites we are cooperating with.

These variables do already have metadata in the form of labels, added using the Hmisc library. However, using CTRL+space for auto-codecompletion it does not take into account this label metadata.

  • So my question is, is it possible to have the autocomplete also search in the label metadata when typing?

See code example below

library(Hmisc)

# dummy example data
df_tobacco <- read.table(text = "
PAT_ID,NL_CM_7_2_3,NL_CM_7_2_9,NL_CM_7_2_10,NL_CM_7_2_4,NL_CM_7_2_5,NL_CM_7_2_6,NL_CM_7_2_6UNIT
1693,,160619003,8517006,1986-07-01,2018-08-01,0.75,pack-day
1689,alleenweekends,65568007,8517006,2005-01-01,2011-01-01,,
2840,,160619003,449868002,1988-01-01,,0.25,pack-day
1569,32jaargeleden,160619003,8517006,1960-01-01,1992-01-01,,
6259,,65568007,8517006,1964-05-06,2002-05-15,0.50,pack-day
8994,ong.4perdag,65568007,OTH,2021-07-14,,0.10,pack-day
2192,,OTH,449868002,1988-01-01,,0.25,pack-day
6274,\"nicotinepleisters,nugestopt\",160619003,8517006,1980-05-01,2023-01-01,0.25,pack-day
2462,,160619003,449868002,2021-07-12,,,
4862,,65568007,OTH,2021-07-14,,0.10,pack-day
", header = TRUE, sep = ",", na.strings = "")

# apply code labels
df_tobacco_NL_CM_7_2_9_mapping <- c(
  "65568007" = "Cigarette smoker (finding)",
  "59978006" = "Cigar smoker (finding)",
  "82302008" = "Pipe smoker (finding)",
  "160619003" = "Rolls own cigarettes (finding)",
  "OTH" = "Other"
)
df_tobacco$NL_CM_7_2_9.factor <- factor(df_tobacco$NL_CM_7_2_9, levels = names(df_tobacco_NL_CM_7_2_9_mapping), labels = df_tobacco_NL_CM_7_2_9_mapping)

# apply labels to variables
label(df_tobacco$PAT_ID) = "Participant ID"
label(df_tobacco$NL_CM_7_2_3) = "Comment"
label(df_tobacco$NL_CM_7_2_9) = "TypeOfTobaccoUsed"
label(df_tobacco$NL_CM_7_2_10) = "TobaccoUseStatus"
label(df_tobacco$NL_CM_7_2_4) = "StartDate"
label(df_tobacco$NL_CM_7_2_5) = "StopDate"
label(df_tobacco$NL_CM_7_2_6) = "Amount"
label(df_tobacco$NL_CM_7_2_6UNIT) = "Amount unit"

## now type df_tobacco$ and press ctrl+space

rstudio_label_hint

Activity

  1. added
    new
    on Feb 8, 2024
  2. kevinushey commented on Feb 8, 2024

    @kevinushey
    Collaborator
    > df_tobacco$NL_CM_7_2_9.factor <- factor(df_tobacco$NL_CM_7_2_9, levels = names(NL_CM_7_2_1_NL_CM_7_2_9_mapping), labels = NL_CM_7_2_1_NL_CM_7_2_9_mapping)
    Error: object 'NL_CM_7_2_1_NL_CM_7_2_9_mapping' not found
    

    Are you able to share that data, or a separate minimally-reproducible example?

  3. BdR76 commented on Feb 8, 2024

    @BdR76
    Author

    Error: object 'NL_CM_7_2_1_NL_CM_7_2_9_mapping' not found

    I had (incompletely) renamed the NL_CM_7_2_1_NL_CM_7_2_9_mapping variable to df_tobacco_NL_CM_7_2_9_mapping to make it a bit more readable here. The factors and code labels aren't that relevant to the code completion, I kept it in to make the code example close to our irl use-case.

    But anyway I've fixed the code in the original post.

  4. self-assigned this
    on Feb 8, 2024
  5. added this to the Chocolate Cosmos milestone on Feb 8, 2024
  6. BdR76 commented on Feb 8, 2024

    @BdR76
    Author

    Cool, impressive to see how quickly you were able to create a fix for this 👍

    But when typing, does it also select the corresponding variable based on either the variable name or when typing (part of) the label description? Or does it just display the label description?

  7. BdR76 commented on Feb 8, 2024

    @BdR76
    Author

    Btw I realize that I didn't include this in the example code, but some descriptions can be longer, like 50 to 150 characters is common, sometimes even longer.

    For example take a look at this questionnaire. We have dataset like this, where the variables are named qol_q01, qol_q02 etc and the labels contain "6. Were you limited in doing either your work or other daily activities?" etc.

  8. removed
    new
    on Feb 8, 2024
  9. kevinushey commented on Feb 8, 2024

    @kevinushey
    Collaborator

    But when typing, does it also select the corresponding variable based on either the variable name or when typing (part of) the label description? Or does it just display the label description?

    The input will match against the whole display string, so you can do something like this:

    Screenshot 2024-02-08 at 1 24 16 PM

    But accepting the completion will give the variable name as expected.

    For example take a look at this questionnaire. We have dataset like this, where the variables are named qol_q01, qol_q02 etc and the labels contain "6. Were you limited in doing either your work or other daily activities?" etc.

    We truncate labels larger than 32 characters, but allow you to view the full text on hover.

    Screenshot 2024-02-08 at 1 28 13 PM
  10. reopened this on Feb 8, 2024
  11. BdR76 commented on Feb 8, 2024

    @BdR76
    Author

    (Oops accidentally pressed Close issue)

    Sounds perfect, no notes 😀 That's exactly the use-case our statisticians need for the scripts that they're working with.

  12. DesiQuintans commented on Feb 15, 2024

    @DesiQuintans

    @BdR76 You might be interested in a package that I wrote for exactly this purpose: https://cran.r-project.org/web/packages/siftr/readme/README.html. It lets me use regex and/or orderless look-around to search for a variable in medical dataframes with potentially millions of rows and thousands of columns. It searches variable names, variable labels, factor levels, and unique values of the data.

    # An orderless search that looks for the word "color" and 
    # the regular expression "any 4 lowercase letters followed by a _".
    
    sift(starwars, color, "[a-z]{4}_")
    
    #> 4 hair_color
    #>         Type: character          Missing: 5 %            All same? No             
    #>         Peek: blond, unknown, none, auburn, grey, blonde, brown, auburn,…
    #> 
    #> 5 skin_color
    #>         Type: character          Missing: 0 %            All same? No             
    #>         Peek: white, brown mottle, white, blue, fair, green, yellow, blu…
    #> 
    #> ✔ There were 2 results for query `(?=.*color)(?=.*[a-z]4_)`.
  13. BdR76 commented on Feb 15, 2024

    @BdR76
    Author

    @DesiQuintans Thanks for the tip, but it sounds like that's a different use-case, for when looking for variables through lots of datasets.

    Our reseachers are analyzing datasets and write R scripts to group/sort the data and create charts etc. They usually work with one or two dataset at a time and just want to just quickly lookup the variable name, without having to lookup the labels in the environment pane icon on the right

  14. DesiQuintans commented on Feb 15, 2024

    @DesiQuintans

    @BdR76 No, it's the same use case. That's exactly how I work, one or two dataframes at a time for modelling and graphing. It's just that my dataframes are huge, they come from other institutions, and they don't always come with meaningful names or labels, hence searching through as much of that relevant data as possible and not just labels. It's like knowing that one of my 2,000 columns should have "Female" in it, and it turns out it's named something non-obvious like CLSAB with no label.

    @DesiQuintans Thanks for the tip, but it sounds like that's a different use-case, for when looking for variables through lots of datasets.

    Our reseachers are analyzing datasets and write R scripts to group/sort the data and create charts etc. They usually work with one or two dataset at a time and just want to just quickly lookup the variable name, without having to lookup the labels in the environment pane icon on the right

  15. olivroy commented on Feb 23, 2024

    @olivroy
    Contributor

    @kevinushey. Wonder if this is intended, but this affects quarto chunk options in yaml and in chunks.

    It is cool, but not convinced it is useful. (Please disregard if this was intended) 2024.04.0+470

    reproduce by creating a minimal quarto document.

    ---
    title: title
    <request autocompletion here>
    ---
    
    # section

    The information is already in the yellow tooltip, so this feels redundant.

    image

    fixed by cf7f1df

  16. kevinushey commented on Feb 23, 2024

    @kevinushey
    Collaborator

    Nice catch; we'll want to fix that.

  17. self-assigned this
    on Mar 13, 2024
  18. added
    autocompletion
    on Mar 13, 2024
  19. ronblum commented on Mar 13, 2024

    @ronblum
    Contributor

    Verified in RStudio Desktop 2024.04.0-daily_573 on MacOS 14.4.

    Checked the original post:

    Image

    Checked the additional issue:

    Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

autocompletionenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions