Questions tagged [rlang]

rlang is an R package for creating tidy evaluation interfaces and manipulating language and environment objects. It is a utility package underlying many of the tidyverse family of packages.

rlang is an R package for creating tidy evaluation interfaces and manipulating language and environment objects. It is a utility package underlying many of the tidyverse family of packages.

rlang offers tools for building an alternate non-standard evaluation (NSE) interface, redubbed "tidy eval", based on quasiquotation. Its basic operators are quo() for quoting and !! (said "bang-bang") for unquoting. The tidy eval framework is now integrated in many tidyverse packages, including dplyr and tidyr.

786 questions
3
votes
1 answer

How can I use NSE and filter an expression using ... (ellipsis)?

How can I utilize the magic dots (...) / ellipsis in order to filter based off an arbitrary column? df = tibble::tibble(col1 = c('a', 'b', 'c'), col2 = c(1,3,4)) my_func = function(x, ...){ df %>% dplyr::filter(... == x) } my_func('a',…
Scott Stoltzman
  • 363
  • 1
  • 15
3
votes
1 answer

Use Shiny to choose column, equality, and value to filter by conditions

I'm creating a Shiny app where I'd like the user to be able to select a column and condition, resulting in the input$COLUMN input$CONDITION input$VALUE which can be used to filter a dataframe. Desired Output iris %>% filter(input$COLUMN…
MayaGans
  • 1,815
  • 9
  • 30
3
votes
3 answers

Why is plotly() and enquo + !! in conflict?

I am writing a function that uses plot_ly for a pieplot. The tilde (~) within argument labels = ~ is in conflict with the unquote operator !!. Is there a solution for this problem? pieplotr <- function (df, Property){ Property_Name <-…
Soph Carr
  • 31
  • 5
3
votes
3 answers

rlang: Error: Can't convert a function to a string

I created a function to convert a function name to string. Version 1 func_to_string1 works well, but version 2 func_to_string2 doesn't work. func_to_string1 <- function(fun){ print(rlang::as_string(rlang::enexpr(fun))) } func_to_string2 <-…
yusuzech
  • 5,896
  • 1
  • 18
  • 33
3
votes
2 answers

Using dplyr verbs in a function with column labels as character vectors

I would like to create a function that takes a data frame and a character vector containing column names as input, and uses tidy verse quoting functions inside in a safe manner. I believe I have a working example of what I want to do. I would like…
leej3
  • 83
  • 5
3
votes
3 answers

Accept both bare (from rlang) or string as a function input

I editing an existing function in a package. Currently, the function accepts a column name in a data frame as a string. I am updating the function to accept either a string name or a bare name. But I am running into some issues. The general…
Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
3
votes
3 answers

Parse and Evaluate Column of String Expressions in R?

How can I parse and evaluate a column of string expressions in R as part of a pipeline? In the example below, I produce my desired column, evaluated. But I know this isn't the right approach. I tried taking a tidyverse approach. But I'm just very…
lowndrul
  • 3,715
  • 7
  • 36
  • 54
3
votes
1 answer

how to use bracket notation (or an alternative) while programming with dplyr

I'm trying to write a function to calculate toplines (as commonly used in polling data). It needs to include both a "percent" and "valid percent" column. Here's an example library(tidyverse) # prepare some data d <- gss_cat %>% mutate(tvhours2 =…
John J.
  • 1,450
  • 1
  • 13
  • 28
3
votes
2 answers

Pass function as parameter using tidy_eval/rlang context

I have a function that I want to pass a function to. However, I can't seem to get it to work using the tidy_eval context. Here is some data: df <- tribble( ~A, ~B, "hi", "hello", "bye", "later" ) I want to be able to call the function like…
Jesse Kerr
  • 341
  • 1
  • 8
3
votes
2 answers

Creating a new formula of type `~ x + y` using `rlang`

I am trying to write a custom function where I want to use the cor.test function but I am having trouble unquoting the needed arguments to create a working formula. Here is what I currently have that doesn't work- library(rlang) # custom…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
3
votes
3 answers

reference another column with tidyeval in filter

I have a tibble with column foo that contains the name of another column in the tibble. I'd like to filter based on the column that is named in foo: mtcars %>% mutate(foo = c(rep("carb", 16), rep("gear", 16))) %>% filter(!!sym(foo) == 4) #>…
lost
  • 1,483
  • 1
  • 11
  • 19
3
votes
1 answer

Choosing the function in a selfmade function with tidyeval

So this example is basically from https://tidyeval.tidyverse.org/dplyr.html#patterns-for-single-arguments and it works just fine: library(tidyverse) group_mean <- function(df, group_var, summary_var){ group_var <- rlang::enquo(group_var) …
xhr489
  • 1,957
  • 13
  • 39
3
votes
1 answer

Tidyeval in own functions inside own functions with the pipe

So I am trying to make a package (I have not included my roxygen2 headers below): I have this function: date_from_text <- function(df, x){ x <- rlang::enquo(x) name <- rlang::quo_name(x) df %>% dplyr::mutate(!!name :=…
xhr489
  • 1,957
  • 13
  • 39
3
votes
1 answer

combine formula and tidy eval (plotly)

I'm struggling to understand this. The below lets me filter my data.frame in a "tidy" way, and draw a plot using plotly. In this case, I'm using plotly's formula-based API to say which columns of a data frame to use: library(plotly) tidy_filter =…
jakub
  • 4,774
  • 4
  • 29
  • 46
3
votes
1 answer

Getting 'argument "e2" is missing with no default' error when using tidyeval within dplyr::summarize_at

I'm trying to capture a summarize_at operation across a bunch of variables. Here's a silly example: library(dplyr) library(stringr) starwars %>% summarise_at(c("hair_color", "skin_color"), ~ sum(if_else(str_detect(., "brown"), 1,…
Phil
  • 7,287
  • 3
  • 36
  • 66