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

Unquoting fails to find variable in mutate and map2 when renaming column of data in nested tibble R

Ok, I'm just trying to rename a column inside a nested tibble based on an identifier/character column: MWE: library(magrittr) iris %>% tibble::as_tibble() %>% tidyr::nest(-Species) %>% dplyr::mutate( Species = as.character(Species), …
Baraliuh
  • 2,009
  • 5
  • 11
3
votes
1 answer

Quasiquotation and ifelse : Unquoting not resolving as expected

I was expecting that I could use quasiquotation mechanisms from rlang package, such as !! and quo_name() to program name unquoting within mutate() and within ifelse() function. However, it did not work as I expected which I show below. Instead of…
mpettis
  • 3,222
  • 4
  • 28
  • 35
3
votes
1 answer

How do I pass a variable name to conditionally sum in dplyr pipe?

The crux of the problem is how to pass in a column variable into a grouped df to conditionally sum data. Data for the example follows: library(dplyr) library(rlang) set.seed(1) # dummy dates date_vars <- purrr::map(c('2018-01-31', '2018-02-28',…
cavamic
  • 69
  • 7
3
votes
3 answers

Passing string as an argument in R

On a fairly regular basis I want to pass in strings that function as arguments in code. For context, I often want a section where I can pass in filtering criteria or assumptions that then flow through my analysis, plots, etc. to make it more…
3
votes
2 answers

Storing and calling variables in a column in dplyr within a function

I want to store some variables within a column cell within a tibble. I then want to call that column and either paste the names of those variables or call that column and paste the columns which those variables correspond to together. In addition,…
Margot J
  • 33
  • 1
  • 5
3
votes
1 answer

Passing weights to glm() using rlang

I want to pass weights to glm() via a function without having to use the eval(substitute()) or do.call() methods, but using rlang. This describes a more complicated underlying function. # Toy data mydata = dplyr::tibble(outcome =…
Ewen
  • 1,276
  • 8
  • 13
3
votes
2 answers

What does `sym()` do regarding tidyeval?

library(tidyverse) input_name <- "birth_year" input_value <- 19 quo(filter(starwars, !!input_name == !!input_value)) # line 5 quo(filter(starwars, !!sym(input_name) == !!input_value)) # line 6 What's the difference between line #5 and line…
stackinator
  • 5,429
  • 8
  • 43
  • 84
3
votes
1 answer

Using rlang Package to Parse Quoted Argument

I'm hoping to string-split a single argument into two arguments and use each in different sections of a function. Is it possible to do this using quasiquotation (!!) or other rlang functions? Thanks! Data: person <- tibble(id = 1, age = 20) friends…
CFB
  • 109
  • 6
3
votes
2 answers

Using rlang::sym inside list definition

I'm writing an R script and I need to allow user to specify the name of variable that will be proccessed by hand, i.e. var <- 'user_name' Generally then I refer to that using rlang::sym and quoting/unquoting mechanism. However, I need to use…
jakes
  • 1,964
  • 3
  • 18
  • 50
3
votes
3 answers

Unquoting the loop variable in `rlang::expr`

I came across unexpected behavior when using operator !! to iteratively construct expressions that involve a loop variable. Creating an expression from the loop variable itself, e.g., X <- list() for( i in 1:3 ) X[[i]] <- rlang::expr( !!i…
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
3
votes
2 answers

Lintr - is there a way to suppress capitalised data frame column name warnings?

I've been writing an R package and using lintr to tidy it up stylistically. One problem I am seeing a lot is that my data.frame columns are named from the CSV and are capitalised, e.g. MyVariableName. This is outside my control and outputted data…
Andrew Hill
  • 307
  • 2
  • 12
3
votes
1 answer

Evaluating expressions inside lists passed as arguments

Suppose I want to create a new table from expressions involving columns from another table. That's pretty easy: library(rlang) library(purrr) from_exprs = function(tb, ...) { quos(...) %>% map_dfc(~ eval_tidy(., tb)) } Example: > tb =…
Luiz Rodrigo
  • 936
  • 1
  • 7
  • 19
3
votes
1 answer

enquo() inside a magrittr pipeline

I just would like to understand what's going wrong here. In the first case (working), I assign the enquo()-ted argument to a variable, in the second case, I use the enquoted argument directly in my call to mutate. library("dplyr") df <- tibble(x =…
eladin
  • 121
  • 6
3
votes
2 answers

Dplyr standard evaluation using a vector of multiple strings with mutate function

I am trying to supply a vector that contains multiple column names to a mutate() call using the dplyr package. Reproducible example below: stackdf <- data.frame(jack = c(1,NA,2,NA,3,NA,4,NA,5,NA), jill =…
Brandon
  • 1,722
  • 1
  • 19
  • 32
3
votes
2 answers

Iterative summary by column pairs using purrr map

I have a large dataset from which I wish to obtain summary estimates (mean, medians, counts, etc) of one column when grouped by two other columns. Trying really hard to work out how to do this using purrr - hopefully to get this workflow to click…
Peter MacPherson
  • 683
  • 1
  • 7
  • 17