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
1 answer

With rlang, convert contents of `...` to a character vector

I'd like to be able to create a character vector based on the names supplied to the ... part of a function. For instance, if I have the function foo(...) and I type foo(x, y), how do I create a character vector that looks like c("x", "y")? I'm most…
bschneidr
  • 6,014
  • 1
  • 37
  • 52
4
votes
1 answer

A simple example of tidy evaluation for formulas

I am trying to grasp tidy evaluation from rlang. As a short example I would like to add a column of predictions to a data frame. This is implemented in modelr, but I wanted to pass the formula directly so I could practice some tidy evaluation. I…
Alex
  • 2,603
  • 4
  • 40
  • 73
4
votes
1 answer

Using select with quosures

Question How would I do the following using quosures? library(tidyverse) lkp <- c("am", "vs", "Sepal.Width", "Sepal.Length") stringSelect <- function(mdat) { lkpOK <- intersect(lkp, names(mdat)) mdat %>%…
thothal
  • 16,690
  • 3
  • 36
  • 71
4
votes
1 answer

class of expr and exprs are different in rlang in R ! Why?

I am not sure if this has been asked here, But I am very confused here. I am reading this awesome book called Advanced R by Hadley Wickham from here. There is function called cement that has been described here, I have modified it little bit and…
PKumar
  • 10,971
  • 6
  • 37
  • 52
4
votes
2 answers

What is the difference between sym() and parse_expr() in the rlang package?

Using the rlang package, I wonder what is the difference between sym() and parse_expr(). Consider for example the following expressions: ex1 = sym('a') ex2 = parse_expr('a') They both return a identical(ex1, ex2) [1] TRUE Suppose now I need a…
Rtist
  • 3,825
  • 2
  • 31
  • 40
4
votes
2 answers

Select named [list] element using tidy evaluation

I'm trying to wrap my head around non-standard evaluation as it's interpreted in the rlang package. With that goal in mind, my question is: How do I write a dplyr::select.list() function that is consistent with tidy evaluation principles? Here's…
mkearney
  • 1,266
  • 10
  • 18
4
votes
1 answer

Using purrr and dplyr: is rlang::sym the best way

I'd like to write functions that use dplyr verbs, which means I have to wade into the murky waters of rlang. To provide a concrete example, say I want to use purrr::map_df() to iterate over variables in a dplyr::group_by(). The programming with…
karldw
  • 361
  • 3
  • 12
4
votes
1 answer

How do I combine varying input variables and varying functions in dplyr summarise

I need to group and summarise a dataframe, using different summarise functions depending on the variable I'm summarising. Those functions can have different main and optional arguments and I'd like to code a function which can do all of that. Here…
Romain
  • 1,931
  • 1
  • 13
  • 24
4
votes
2 answers

Parse and evaluate quosures from string

Is there a way to parse and evaluate a quosure from a string. I would like to achieve the same output as in the example below: library(rlang) a <- 10 quo(UQ(a) + 2 * b) ## ## ~10 + 2 * b but starting from t <- "UQ(a) + 2 *…
johannes
  • 14,043
  • 5
  • 40
  • 51
4
votes
2 answers

Programmatic regression modelling with tidyeval

I am trying to get my head around programming using tidyeval. I want to write a function to run logistic regression models for selected outcome variables: library(tidyverse) set.seed(1234) df <- tibble(id = 1:1000, group =…
Peter MacPherson
  • 683
  • 1
  • 7
  • 17
4
votes
3 answers

Spread multiple columns in a function

Often I need to spread multiple value columns, as in this question. But I do it often enough that I'd like to be able to write a function that does this. For example, given the data: set.seed(42) dat <- data_frame(id = rep(1:2,each = 2), …
joran
  • 169,992
  • 32
  • 429
  • 468
4
votes
3 answers

Pass vector of column names to paste() within mutate (dplyr)

I'm trying to write a function that takes as one of its arguments a vector of column names from user. The column names will be used to specify what columns of the dataframe will be pasted together to form a new column within dplyr::mutate. I tried…
bikeactuary
  • 447
  • 4
  • 18
4
votes
1 answer

Dplyr Tidyyeval for built variable names

I am trying to build a function that will allow me to perform functions on columns that start with a specific prefix. I am struggling to figure out how to get the names to evaluate to the correct thing. I've looked at the dplyr website where it…
Dan
  • 2,625
  • 5
  • 27
  • 42
4
votes
1 answer

Finding the origin environment of the ... (dots) arguments of a call

I want to be able to find the environment from which the ... (dots) arguments of a call originate. Scenario For example, consider a function foo <- function(x, ...) { # do something } We want a function env_dots(), which we invoke from within…
egnha
  • 1,157
  • 14
  • 22
3
votes
3 answers

How to use ensym as a variable to store values within function

How can I use ensym or another appropriate function that will store this as a variable and assign a value to it. For example: quest <- 'a' list(rlang::ensym(quest) = 1) >Error: unexpected '=' in "list(rlang::ensym(quest)=" Expected output: $a [1]…
Working dollar
  • 306
  • 2
  • 10