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.
689
questions
1
vote
2
answers
49
views
In R, how do I get an if statement to recognize if input in double curly brackets is a certain value?
I'm building a function where users can select a column from a list of options, and within the function, I want to do an if statement where, if the x variable is one of the options that the function ...
2
votes
1
answer
28
views
Modify symbols inside an expression using the tidy eval framework
I want to replace the function lazyeval::interp() using the tidy eval framework. Basically, I have this simple expression :
library(rlang)
expr((a + 5 + b)/3)
#> (a + 5 + b)/3
I want to replace ...
0
votes
2
answers
48
views
Using a named list within a purrr::map function call for additional function parameters
I am beginning to work with purrr and came across the following problem - I hope the following example illustrates my problem:
I have three functions with the following two properties:
The parameters ...
1
vote
0
answers
22
views
Passing index expression with filter() to database
I have PostgreSQL table table with lot of rows.
In order to make queries faster and to ensure UTC time zone, it indexed by timezone('utc'::text, t.closed_at)::date
for getting data I use following ...
0
votes
0
answers
11
views
Custom testthat expectation that performs calculations
I am trying to test if the off-diagonal values of a table in R are non-zero using the {testthat} package, and would like to apply this test to multiple different tables e.g. may load different data ...
1
vote
0
answers
30
views
How to make cli_warn() and cli_abort() not print in white color?
I'm migrating from rlang to cli for error and warning handling (and it's awesome).
However, I couldn't find out how to tell cli_warn() and cli_error() to have the same blueish color as in base R (and ...
1
vote
3
answers
76
views
Which rlang function should I use to evaluate a glue string as a variable name?
Suppose that I want to create a function to be used within dplyr::mutate(), and in which I feed a variable name, and within the function, it will extract a particular pattern in the variable name ...
0
votes
0
answers
36
views
Trouble installing rlang in R [duplicate]
I have just updated my RStudio to 4.1.2 and since then I lost all my packages. I successfully re-installed most of them except for rlang.
I keep receiving this message every time I try to install it. ...
0
votes
0
answers
30
views
Unable to load shared object rlang.so when conda environment is activated
I'm using R on Ubuntu 20.04.3 LTS (Focal Fossa).
I'm trying to use R's plotly's save_image function which enables static image exporting via the kaleido python package.
To this end I'm first ...
1
vote
0
answers
52
views
Understand data masking in rlang
When reading rland document, it is still a bit of difficult for me to understand:
forwarding patterns, name patterns, bridge patterns
For example, it is difficult to judge which of following is ...
1
vote
1
answer
66
views
Using rlang double curly braces {{ in data.table
Problem
The {{}} operator from the rlang package makes it incredibly easy to pass column names as function arguments (aka Quasiquotation). I understand rlang is intended to work with tidyverse, but is ...
0
votes
1
answer
27
views
Receive the name of a list element from function input
Via a .yml file I load in a number of file paths to different data frames.
They are structured in a named list. Here a very simplified version:
settings_list <- list(survey = list(df1 = "path&...
0
votes
0
answers
83
views
ggplot not installing - tried everything
I'm out of options to install ggplot. I tried installing from Tools, from the console itself, indicating the cran source, indicating the local dir that the ggplot.gz file appears in. None of them ...
0
votes
0
answers
54
views
How to evaluate variable with space in a tidyverse function?
I am having trouble using variable with a space in a function (terrible idea I know).
The idea here is that I am using a statistical function from rstatix, which is using tidyverse non standard ...
0
votes
1
answer
29
views
Using rlang's sym inside a function and getting error
I have the following function:
library(tidyverse)
library(dplyr)
library(rlang)
custom_filter <- function(
df,
pos_filter_1 = NULL,
ct_filter_1,
filter_1
){
pos_filter_1 <- paste0(&...
0
votes
0
answers
16
views
Error in cfBuild(X_reduced, y, ensNum = 2, bootNum = 100, seed = 1) : could not find function "cfBuild"
I have imported the library classyfireR which has the cfBuild function but when I try to use the function in code it is giving me an error.Also I tried importing classyfire in R but it shows this ...
1
vote
1
answer
41
views
Pass a variable to .x$ in a formula of a map function
The first example works, but I'd like to move more of the repetitive code inside a function along the lines of example 2. I haven't found the right rlang approach to deal with the .x$x.
library(...
0
votes
1
answer
28
views
replacing list element of character(0) with NA by using is_empty()
I have a dataframe (my_df) with a list called 'house' with character(0) in one row.
I would like to replace this value with NA_character by using rlang's is_empty(). I am aware that there are ...
0
votes
2
answers
32
views
What if I want to use embracing operator with `starts_with()`?
Using the embracing operator eliminates the need to enclose arguments passed to a function in double quotation marks.
But what if I want to use it with starts_with()?
# This works.
test <- function(...
2
votes
3
answers
71
views
Mutating columns with reduce2 and rlang
I am trying the following:
library(tidyverse)
library(rlang)
df <- data.frame(a = 1:2)
reduce2(list(df, df, df), letters[2:3], ~ mutate(.x, !!(.y) := 2:3))
#> Error in local_error_context(dots ...
1
vote
1
answer
61
views
R: Use real lon/lat values at x and y axis
I've generated the following chart with R:
library(rnaturalearth)
library(rnaturalearthdata)
library(ggplot2)
library(rworldmap)
library(scales)
library(sf)
library(mapdata)
library(maptools)
library(...
1
vote
1
answer
17
views
using quasiquotation in a function with summarize in dplyr
I'm trying to write a function that can take column names as strings assigned to variables and produce a summarized output for them like so
my_function <- function(my_df, columnA,columnB){
...
1
vote
2
answers
39
views
Dynamically named dots passed to lapply
This isn't really a httr2 specific problem though it is easy to illustrate this way. If I have a param that is being to a function that I want to lapply on and that function and the componets of ... ...
1
vote
0
answers
465
views
R: filter issue - Error in `filter()`: ! Problem while computing `..1 = between(data$column, 400, 1800)`
I have a data frame and am looking to get a subset based on conditions related to another column. This seems to work for some variable, but somehow for one particular one, I am getting the following ...
1
vote
1
answer
65
views
Dynamic variables names in dplyr function across multiple columns
I am trying to write a function that uses dplyr::summarise to obtain means of multiple columns of a data frame and assign dynamic names to the summarised columns using the new rlang glue syntax and := ...
0
votes
0
answers
18
views
VAR wrapping not compatible with IRF? | R
I am developing my own package for time series, it is basically a wrap up of all the things I learn, so I learned about vars using the vars package.
So check this function:
var_est <- function(...
0
votes
1
answer
80
views
Functions dplyr with rlang::last_error() in purrr::map loop in r
I'm using a function to calculate the length of linestring per cell by ID and store in a list, convert each element of the list into a RasterLayer and turn that list into a RasterStack, average all ...
0
votes
0
answers
93
views
could not find function "new_overscope" when running collect() from dplyr package
I am using R studio V3.5.1 on a server.
Below codes were to retrieve some data from a certain table in a database. The codes worked until today when I was rerunning the chunk. I got such an error ...
0
votes
0
answers
30
views
What is the functionality of %||% and what does it do? [duplicate]
This has been hard to search for as everything that comes up relates to %in%. What exactly does %||% do in R? Are there any related documentation?
Thanks!
3
votes
1
answer
75
views
R metaprogramming: pass expression/quosure to function that partially accesses local frame
I will use the following example to explain my question. But the question is not only about this specific example, but more general about meta-programming in R.
I have two specific functions to make ...
1
vote
2
answers
35
views
How do you filter by passing a string as a column name in a user-defined function?
I'm writing a function where the user specifies the column they want to filter and what cutoff value they want. In this example, I want to filter out any pretest scores under 2. Here's a sample ...
2
votes
1
answer
207
views
Error when R package building: `:=` can only be used within dynamic dots
So I've written this function here which renames the first column in a data frame:
{
df %>%
rename({{newname}} := colnames(df[1]))
}
It ran completely fine. Now when I moved this function ...
1
vote
0
answers
59
views
Error when referring to column names with rlang and sym when column name is identical to passed value
What am I missing here.
I'm trying to do some data filtering based on columns that I pass into the filter function dynamically. This works as long as the column name itself is not identical to the ...
0
votes
1
answer
16
views
rlang::parse_expr with string with escape characters
how I could use eval(rlang::parse_expr(string))’ or alternative with such expresssion string <-"print('A\s*B')"`? I am getting unrecognized escape character. The expression is evaluated ...
1
vote
1
answer
30
views
Using an rlang alternative to eval in R
In my below function, I was wondering if there is an rlang alternative to eval()?
library(rlang)
foo <- function(dat, ..., i){
dot_cols <- rlang::ensyms(...)[[1]]
subset(dat, eval(...
2
votes
1
answer
41
views
How to undo ensym and specify the environment for a function within a function?
So, I'm asking this as a follow-up to another question, to the solution to which I thought would fix all of my problems. It seems like that's not the case. Take the following setup
library(tidyverse)
...
3
votes
2
answers
88
views
How to convert value to numeric and filter out NAs within function using curly curly?
I'd like to take a tibble (or dataframe), convert one of the columns to numeric, only select the same column plus a third column, and filter out NAs.
Given the following data:
library(tidyverse)
set....
2
votes
2
answers
106
views
using `!!` without attaching `rlang`
I'm writing a package and one of my functions generates a ggplot. I would like to only import ggplot2 or rlang (without depending on them). After some trial and error I managed to get it to work, but ...
2
votes
1
answer
33
views
Use vector of columns in custom dplyr function
I'm trying to use the tidyverse to make a function that allows me to take a vector of column names (of class factor), count how many observations meet a certain condition (value == "yes"), ...
4
votes
2
answers
149
views
Inject Expression from One Environment and Evaluate in Another
Update
It turns out that the function rlang::expr_interp() essentially meets my goal.
unquo_2 <- function(expr, inj_env = rlang::caller_env(), eval_env = NULL) {
# Capture verbatim the argument ...
2
votes
1
answer
61
views
Using pivot_wider in a function with named variable from function argument
I am trying to use pivot_wider inside a function. The values are not uniquely defined, so I need to specify the values_fn argument of pivot_wider, which should return a list. The column for values_fn ...
1
vote
1
answer
56
views
Using ggplot in a function
I am trying to create a function in r using ggplot. This is my code so far:
breakchart<-function(df,x,y) {
ggplot(df,aes( {{x}} ,{{y}})) + geom_point() +
ylim(-20,20) + xlim(-20,20) +
...
0
votes
2
answers
58
views
Declaring a sym without backticks in rlang
I am defining a function like this;
afun <- function(formula,data,level){
dataname <- rlang::sym(deparse(substitute(data)))
call('somefunction',formula = formula,data = dataname,vec ...
3
votes
1
answer
88
views
Extract a function-call expression's arguments with names
Let's say I have an expression representing a function call, where some arguments are named and some are positional.
expr <- quote(mean(1:5, na.rm = TRUE))
Would it be possible to extract the ...
0
votes
0
answers
40
views
Segfault when running R callback function from C++ after completing a task on a background thread
The following code (seems to) work as expected, but I receive a segfault if I remove the PROTECT call or if I remove the second member expr of struct func_data.
This seems like it could be related to ...
1
vote
1
answer
51
views
How do we refer to a dataframe column with an interaction term using a string variable?
I have a data frame whose columns contain the coefficients of a regression model trained on different data sets. Each row of the data frame corresponds to the model trained on a (possibly) different ...
2
votes
0
answers
109
views
How is the rlang `:=` operator defined?
I'm trying to understand how does := work in rlang. I found it here but I don't think it's very useful for me to understand how it works. I would like to know if it's defined in C language and where I ...
1
vote
1
answer
38
views
simplifying tidyeval with multiple symbols
The following function behaves as desired: several variables can be passed to group_by without the need to put them into alist() or dplyr::vars:
mean_by_grp <- function(df, meanvar, grp) {
...
0
votes
1
answer
93
views
evaluate string expression in ggplot
I would like to pass the label_format expression to ggplot but I'm getting an error
#toy df
df <- data.frame(
x = 1:10,
y = seq(0.1,1,by=0.1),
label_format = "scales::percent_format(...
3
votes
1
answer
83
views
env_parent(): different results when using default argument or when passing explicitly default argument
I have noticed different behavior when using env_parent() from rlang package and when using env_parent(caller_env()), although caller_env() is a default argument for env_parent() first parameter:
...