Sometimes when working with dplyr
one has a character vector of column names that's to be used to operate on data, for instance:
cols_of_interest <- c("Petal.Width", "Petal.Length")
In dplyr
0.5.0 and earlier, the recommended approach to this problem was to use the verb_
underscore construct as follows:
library("tidyverse")
my_cols <- c("Petal.Width", "Petal.Length")
iris %>%
select_(.dots = my_cols)
The verb_
functions are now deprecated in favour of the new tidy evaluation framework (dplyr.tidyverse.org/articles/programming.html) introduced by the rlang
library.
As of dplyr
0.7.0 the following works without any special accommodations:
library("tidyverse")
# library("rlang")
my_cols <- c("Petal.Width", "Petal.Length")
iris %>%
select(my_cols)
Note that in development builds of dplyr
, this was not the case
Motivation
Selecting columns in Shiny apps is a good example use case, this is how one could do it using verb_
notation
library("shiny")
library("tidyverse")
library("DT")
shinyApp(
ui = fluidPage(
selectInput("cols_to_show",
"Columns to show",
choices = colnames(iris),
multiple = TRUE),
dataTableOutput("verb_table")
),
server = function(input, output){
output$verb_table <- renderDataTable({
iris %>%
select_(.dots = input$cols_to_show)
})
}
)