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

how to use rlang::as_string() inside of a function?

I'm writing a function where I supply a variable name as a symbol. At a different step in the function, I want to use the variable name as a string. Per the documentation, rlang::as_string "converts symbols to character strings." Here is a basic…
John J.
  • 1,450
  • 1
  • 13
  • 28
3
votes
4 answers

refer to column name from variable in across in dplyr

Given a reference column z, I want to use dplyr to transform each column as: x = log(x) - log(z) I want z to be a string, or even better, a quoted expression (e.g. input by a user - all of this is within a function). Here is what I've…
cmo
  • 3,762
  • 4
  • 36
  • 64
3
votes
1 answer

Using quoted variables in a custom dplyr wrapper function

My problem is the following. I have a function foo which works inside dplyr::mutate. This function accepts tidyselect syntax. I want to build a wrapper function bar which should also support tidyselect syntax. I am looking for a clean way to pass…
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
3
votes
1 answer

Explanation of rlang operators used to write functions

I recently posted two questions (1, 2) related to functions I was trying to write. I received useful answers to each, which resulted in the following two functions: second_table <- function(dat, variable1, variable2){ dat %>% …
Emily Halford
  • 169
  • 1
  • 7
3
votes
1 answer

How to pass dataframe columns as argument when curlycurly does not work? (e.g.map function within nest)

I'm very happy that it is relatively easy to pass dataframe columns as function argument with "curly curly" {{}} to e.g. filter() or select(). But this does not work with nest and map (see code below, {{x}} in lm function) and I don't know why. How…
Manuel
  • 47
  • 4
3
votes
1 answer

rlang: Curly curly operator and tunneling data-variables inside strings on RHS

I think curly curly is a good successor to bang bang. Nevertheless, I am still struggling to understand the tidyverse NSE. Let's say I want to make a simple function that runs on a dataframe, takes multiple columns, reshapes them in long format and…
Claudiu Papasteri
  • 2,469
  • 1
  • 17
  • 30
3
votes
1 answer

dplyr'ish approach to simple subset/summarise function

Background The provided function achieves the following: Subsets the provided data frame using user-provided expression Selects the desired column Applies custom summary function on resulting vector and returns scalar base…
Konrad
  • 17,740
  • 16
  • 106
  • 167
3
votes
1 answer

How to reconcile .data pronouns with rlang::enquo

I am writing a package using a lot of dplyr functions - to pass on all tests in devtools::check(), I have to use .data frequently. Some of the functions are nested into other functions. In the example below, I need to use variable both in a…
3
votes
2 answers

R use bang-bang within a glue statement

I'd like to make a simple function that takes a data frame and user supplied names for two columns in that data frame. The purpose would be to allow this to work easily with dplyr pipes. It will produce a character vector of glued strings: func <-…
Dylan Russell
  • 936
  • 1
  • 10
  • 29
3
votes
2 answers

Named vector "by" arguments for `dplyr::_join` functions

I am writing a function to dplyr::_join two dataframes by different columns, with the column name of the first dataframe dynamically specified as a function argument. I believe I need to use rlang quasiquotation/metaprogramming but haven't been able…
maia-sh
  • 537
  • 4
  • 14
3
votes
1 answer

Only strings can be converted to symbols within a function in R

I have a function that is intended to operate on data obtained from a variety of sources with many manual entry fields. Since I don't know what to expect for the layout or naming convention used in these files, I want it to 'scan' a data frame for…
SGE
  • 311
  • 3
  • 10
3
votes
2 answers

R How to check that a custom function is called within a specific function from a certain package

I want to create a function myfun that can only be used inside another function, in my case dplyrs mutate or summarise. I further do not want to rely on dplyrs internals (for example mask$...). I came up with a quick and dirty workaround: A function…
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
3
votes
1 answer

How do I pipe into or include an additional argument within a list of quosures?

I have a list of quosures in my_q_list below: library(rlang) suppressPackageStartupMessages(library(dplyr)) q_list <- function(...) { enquos(...) } my_q_list <- q_list( select(mpg, hp), filter(hp > 20), mutate(mpg2 = mpg*2) ) my_q_list #>…
David Ranzolin
  • 913
  • 1
  • 6
  • 18
3
votes
1 answer

Create dplyr statements to later be evaluated in R

I want to create a single function called eval_data where the user can input a list of data frames a list of dplyr functions to apply to the data frames a list of columns to select from each dataframe: This will look something like: eval_data <-…
MayaGans
  • 1,815
  • 9
  • 30
3
votes
1 answer

dplyr group_by dynamic cols

What is the consensus on the best way to group_by when the group_by is being fed a variable? Consider the following simple function: library(dplyr) myFunction <- function(df, col_name) { out <- …
user1658170
  • 814
  • 2
  • 14
  • 24