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
9
votes
6 answers

Using quotations inside mutate: an alternative to mutate_(.dots = ...)

I want to apply different functions to the same column in a tibble. These functions are stored in a character string. I used to do this with mutate_ and the .dots argument like this: library(dplyr) myfuns <- c(f1 = "a^2", f2 = "exp(a)", f3 =…
Cettt
  • 11,460
  • 7
  • 35
  • 58
9
votes
1 answer

Get expression that evaluated to dot in function called by `magrittr` pipe

I have a function x_expression() which prints the expression passed to argument x. pacman::p_load(magrittr, rlang) x_expression <- function(x) { print(enquo(x)) } y <- 1 x_expression(y) #> #> expr: ^y #> env: global y %>%…
Rory Nolan
  • 972
  • 10
  • 15
9
votes
1 answer

When to use rlang::ensym() over rlang::sym()?

I can see from the documentation that rlang::enquo() and rlang::quo() are used in different contexts. Hence, I used rlang::enysm() recently within a function declaration (see below). However, wrapped inside another SE function call, I got an…
Lorenz Walthert
  • 4,414
  • 1
  • 18
  • 24
9
votes
2 answers

R quo_name equivalent of quos

Hi following Programming with dplyr I noticed that one can add a name using quo_name. I was wondering how to do this for multiple columns, eg. like a quos_name of sorts. E.g.: my_mutate <- function(df, expr) { expr <- enquo(expr) mean_name <-…
Geyer Bisschoff
  • 221
  • 2
  • 9
9
votes
2 answers

dplyr 0.7 equivalent for deprecated mutate_

I cannot find in dplyr 0.7 a way to replace the mutate_ function which is going to be deprecated. The mutate_ function is useful in my use case : I store in a database (string format) many instructions (that can be filtered if needed) and apply…
stephLH
  • 131
  • 7
9
votes
1 answer

Correct usage of dplyr::select in dplyr 0.7.0+, selecting columns using character vector

Suppose we have a character vector cols_to_select containing some columns we want to select from a dataframe df, e.g. df <- tibble::data_frame(a=1:3, b=1:3, c=1:3, d=1:3, e=1:3) cols_to_select <- c("b", "d") Suppose also we want to use…
RobinL
  • 11,009
  • 8
  • 48
  • 68
8
votes
5 answers

rowwise() sum with vector of column names in dplyr

I am once again confused about how to achieve this: Given this data frame: df <- tibble( foo = c(1,0,1), bar = c(1,1,1), foobar = c(0,1,1) ) And this vector: to_sum <- c("foo", "bar") I would like to get the row-wise sum of the values in the…
MKR
  • 1,620
  • 7
  • 20
8
votes
5 answers

ggplot2 fails to load, with 'rlang' package error

This is the error message: Error: package or namespace load failed for ‘ggplot2’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]): namespace ‘rlang’ 0.3.4 is already loaded, but >= 0.4.0 is required In addition to a Warning…
Tony
  • 91
  • 1
  • 1
  • 3
8
votes
1 answer

curly curly Tidy evaluation and modifying inputs or their names

The new curly curly method of tidy evaluation is explained in this article. Several examples are given demonstrating the use of this style of non-standard evaluation (NSE). library(tidyverse) # Example 1 -------------------------- max_by <-…
Display name
  • 4,153
  • 5
  • 27
  • 75
8
votes
3 answers

In a named argument to dplyr::funs, can I reference the names of other arguments?

Consider the following: library(tidyverse) df <- tibble(x = rnorm(100), y = rnorm(100, 10, 2), z = x * y) df %>% mutate_all(funs(avg = mean(.), dev = sd(.), scaled = (. - mean(.)) / sd(.))) Is there a way to avoid calling mean and sd twice by…
Jonathan Gilligan
  • 701
  • 1
  • 5
  • 21
8
votes
4 answers

R package updates

I have been having some issues accessing an updated version of a package in R. On running a workflow for data analysis I got this error message: library(dplyr) Error: package or namespace load failed for ‘dplyr’ in loadNamespace(i, c(lib.loc,…
tom
  • 83
  • 1
  • 1
  • 3
8
votes
2 answers

Functional programming with dplyr

Looking for a more efficient / elegant way to pass multiple arguments to a group-by using non-standard evaluation in a function using dplyr. I don't want to use the ... operator, but to specify the functions individually. My specific use case is a…
Aaron Cooley
  • 438
  • 3
  • 8
8
votes
1 answer

Naming a new variable based on a quosure

I'm trying to write a custom function that will compute a new variable based on values from a predefined vector of variables (e.g., vector_heavy) and then name the new variable based on an argument provided to the function (e.g., custom_name). This…
Joe
  • 3,217
  • 3
  • 21
  • 37
8
votes
2 answers

How to detect an empty quosure in rlang?

f <- function(x) enquo(x) e <- f() # #~ None of these work: > is_empty(e) [1] FALSE > is_missing(e) [1] FALSE > is_false(e) [1] FALSE > is_quosure(e) [1] TRUE
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
7
votes
1 answer

What is the "embracing operator" `{{ }}`?

I just came across the "embracing operator" {{ }} in section 2.2.3 of the tidyverse style guide. What does the embracing operator {{ }} do in R?
sieste
  • 8,296
  • 3
  • 33
  • 48
1 2
3
52 53