0

I'm trying to create a function where I can pass in column names as an argument. I've seen some other examples like this post on passing column names to a function but I'm getting an error:

Error: Column column is unknown

Here is my data in my csv:

Role
1 Primary
2 Secondary
3 Primary
4 Primary

Here is my code:

mydata = read.csv("EA5.csv")

my_bar_chart <- function(data, column, title){
  column<-eval(substitute(column),data, parent.frame())
  toReturn <- data %>% 
    group_by(column) %>% 
    summarize(count = n()) %>% 
    mutate(percent = count/sum(count), column = reorder(column, -count, FUN=identity)) %>%
    ggplot(aes(x=column, y=count)) +
    xlab(title)+
    geom_col() +
    geom_text(aes(label = paste0(round(100 * percent, 1), "%")))
  return(toReturn)
}

p1 <- my_bar_chart(mydata, Role, "EA5 Controller Role")

grid.arrange(p1)
b-ryce
  • 5,752
  • 7
  • 49
  • 79
  • 1
    You're looking for non-standard evaluation (NSE). Perhaps https://dplyr.tidyverse.org/articles/programming.html can help. – r2evans Dec 26 '19 at 16:37
  • Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Dec 26 '19 at 16:43
  • My hazy memory is that you might want the dplyr `!!` and `!!!` functions. Furthermore Invoking your function with an undefined `Role` object should have thrown an error, so you have not told us enough to offer advice. – IRTFM Dec 26 '19 at 16:50
  • These might help https://stackoverflow.com/q/45439813/786542 & https://stackoverflow.com/q/58786357/786542 – Tung Dec 26 '19 at 16:51
  • I've updated the question to include the data. (The column header is called "Role") – b-ryce Dec 26 '19 at 17:16

1 Answers1

3

In the latest version of rlang we can use 'curly-curly' for this ({{column}}):

mydata = iris

my_bar_chart <- function(data, column, title){
  toReturn <- data %>% 
    group_by({{column}}) %>% 
    summarize(count = n()) %>% 
    mutate(percent = count/sum(count), column = reorder({{column}}, -count, FUN=identity)) %>%
    ggplot(aes(x=column, y=count)) +
    xlab(title)+
    geom_col() +
    geom_text(aes(label = paste0(round(100 * percent, 1), "%")))
  return(toReturn)
}

p1 <- my_bar_chart(mydata, Species, "EA5 Controller Role")

Chris
  • 3,836
  • 1
  • 16
  • 34
  • 1
    No problem! For reference it's just a shorthand way of using the quote-unquote pattern, so `!!enquo(column)` should work too. [Another good reference here](https://dplyr.tidyverse.org/articles/programming.html). – Chris Dec 26 '19 at 18:05