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
4
votes
2 answers

Dplyr, Non-standard evaluation and Walrus operator and curly-curly

A real question. Whenever I need to write dplyr functions, I play by the ear. I am aware of the curly-curly operator which simplifies a lot the task. https://www.tidyverse.org/blog/2019/06/rlang-0-4-0/…
larry77
  • 1,309
  • 14
  • 29
4
votes
2 answers

How to get case_when in dplyr accept conditions from character

I am trying to use case_when in shiny application in order to build an app, showing the preview of some selection policy expressed as a set of rules. In the app, user can input expressions in case_when syntax, e.g.: cond = "Age > 40 ~ 1, TRUE ~…
dotsbyname
  • 250
  • 5
  • 14
4
votes
1 answer

Using tidy eval in R packages

I have a function stacked_plot() that uses tidy evaluation to make a stacked plot. I wanted to include it in my package and make the other function from that package to call it. Here's the minimal example: stacked_plot <- function(data, what, by =…
Kuba_
  • 886
  • 6
  • 22
4
votes
4 answers

using `rlang` quasiquotation with `dplyr::_join` functions

I am trying to write a custom function where I use rlang's quasiquotation. This function also internally uses dplyr's join functions. I have provided below a minimal working example that illustrated my problem. # needed libraries…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
4
votes
2 answers

Quasiquotation with data.table

I'm trying to wrap my head around quasiquotation so that I could use it together with a data.table call. Here is an example: library(data.table) library(rlang) dt <- data.table(col1 = 1:10, col2 = 11:20) dt[, col1] If I wanted to wrap this into…
JBGruber
  • 11,727
  • 1
  • 23
  • 45
4
votes
0 answers

Best practice for unquoting variable names in the right-hand side of dplyr verbs

What is best practice for using variable names as function arguments in the right-hand side of dplyr verbs such as mutate? To apply a function to a variable in a tibble with mutate is trivial if we use the column name directly: mtcars %>% …
heds1
  • 3,203
  • 2
  • 17
  • 32
4
votes
3 answers

Convert a quosure with dashes to a string?

When I do: > quo(DLX6-AS1) The output is: expr: ^DLX6 - AS1 env: global Which inserts spaces around the dash. When I try to convert that to a string, I get either: quo(DLX6-AS1) %>% quo_name "DLX6 - AS1" or quo(DLX6-AS1) %>%…
Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46
4
votes
3 answers

How to delay the evaluation of function arguments in R?

I would like to delay the evaluation of a function argument in R. Example: my_func <- function(FUN){print(FUN); print(FUN)} my_func(runif(1)) #> [1] 0.2833882 #> [1] 0.2833882 Created on 2019-07-21 by the reprex package (v0.2.1) This works as…
David Kane
  • 363
  • 1
  • 2
  • 8
4
votes
2 answers

rlang: pass multiple groups with ... to gather()

Lets say I'd like to calculate the mean, min and max for an arbitraty amount of groups within a custom function. The toy data looks like this: library(tidyverse) df <- tibble( Gender = c("m", "f", "f", "m", "m", "f", "f", "f", "m",…
j3ypi
  • 1,497
  • 16
  • 21
4
votes
1 answer

How do I use .data in filter_at?

The following code will filter a table of isotope combinations to identify combinations where only one element is isotopically enriched. df <- tibble::tibble( C12 = rep(c(2:0), 2), C13 = rep(c(0:2), 2), H1 = rep(c(0, 1), each = 3), H2 =…
Will Oldham
  • 704
  • 3
  • 13
4
votes
3 answers

R rlang: use .x in map() with quosure?

I am trying to pass a set of variables/values in a data.frame to a map function, but am not sure how to deal with the fact that .x refers to a quosure that needs to be evaluated: mutate(df2 = map2(variable, value, ~filter(df1, .x==.y))) A naive !!.x…
Matifou
  • 7,968
  • 3
  • 47
  • 52
4
votes
2 answers

How to fix 'Quosures can only be unquoted within a quasiquotation context' error in R function

I am trying to write my first function using rlang and I am having some trouble fixing the following error. I've read the vignette, but didn't see a good example of what I'm trying to do. library(babynames) library(tidyverse) name_graph <-…
H5470
  • 91
  • 1
  • 8
4
votes
1 answer

difference between !! and eval_tidy in mutate_at

I've been studying tidyeval semantics from a number of sources, but I'm getting a result I can't explain. I'm using mutate_at and case_when to transform some variables by (1) retrieving their names using quotation, (2) modifying their names using…
lost
  • 1,483
  • 1
  • 11
  • 19
4
votes
1 answer

Why does purrr::map not support quasiquotation?

I'm curious why the purrr::map_* function family, despite being a part of tidyverse, does not support quasiquotation by splice-unquoting its dots prior to evaluation of the mapped function? library(tidyverse) library(rlang) set.seed(1) dots <-…
mjktfw
  • 840
  • 6
  • 14
4
votes
2 answers

ggplot with aesthetics generated from input data

Since I will need to make a lot of different plots in R I'm trying to put some more logic in preparing the data (add column names corresponding to the aesthetics) and less logic in the plot itself. Consider the following default iris…
Martin
  • 1,084
  • 9
  • 15