How to use $ in user defined function?

Hello--I'm having trouble selecting on a variable within a user defined function. I have read elsewhere that the $ is not appropriate in this instance, but I wasn't able to find out what is appropriate. Thanks for taking a look.

---
title: "using $ in user defined function"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, include=FALSE}
library(tidyverse)
library(labelled)
library(janitor)
library(kableExtra)
```

# set variable label
```{r}
var_label(gss_cat$partyid) <- "What is your political party affiliation?"
```

# use var_label() to set caption in kable()
```{r}
gss_cat %>% 
  tabyl(partyid) %>% 
  kable(digits = 1,
        caption = var_label(gss_cat$partyid)) %>% 
  kable_styling(full_width=FALSE)
```

# how to use $ selector in user defined function?
```{r}
qtab <- function(df, x) {
  df %>% 
    tabyl({{x}}) %>% 
    kable(digits = 1,
          caption = var_label(df$x)) %>% 
    kable_styling(full_width=FALSE)
}

gss_cat %>% qtab(partyid)
```

You don't tell us what is the expected result.
It looks like var_label is a function (maybe from one of the packages labelleded or janitor that I do not know) and gss_cat is a data.frame .
If that is the case you first need to create it before you use the $ operator.

It doesn't matter if you use the $ operator inside or outside a user defined function.
As an aside, I don't see any user defined function.

Thanks. When using kable, I am able to print the variable label in the caption of a table with caption = var_label(gss_cat$partyid). When calling the qtab function defined above (the user defined function?) with caption = var_label(df$x), the label does not print. The warning, which prints when knitting the above reprex in RMarkdown, is: Warning: Unknown or uninitialised column: x. I've tried various alternatives to df$x without success.

gss_cat is a built into forcats and is loaded with the tidyverse.

library(tidyverse)
  
head(gss_cat)
#> # A tibble: 6 x 9
#>    year marital      age race  rincome    partyid     relig     denom    tvhours
#>   <int> <fct>      <int> <fct> <fct>      <fct>       <fct>     <fct>      <int>
#> 1  2000 Never mar…    26 White $8000 to … Ind,near r… Protesta… Souther…      12
#> 2  2000 Divorced      48 White $8000 to … Not str re… Protesta… Baptist…      NA
#> 3  2000 Widowed       67 White Not appli… Independent Protesta… No deno…       2
#> 4  2000 Never mar…    39 White Not appli… Ind,near r… Orthodox… Not app…       4
#> 5  2000 Divorced      25 White Not appli… Not str de… None      Not app…       1
#> 6  2000 Married       25 White $20000 - … Strong dem… Protesta… Souther…      NA

Created on 2021-07-26 by the reprex package (v0.3.0)

Thanks again

qtab <- function(df, x) {
  df %>% 
    tabyl({{x}}) %>% 
    kable(digits = 1,
          caption = var_label(pull(df,{{x}}))) %>% 
    kable_styling(full_width=FALSE)
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.