491 questions
2
votes
1
answer
116
views
Programmatically filter using expressions stored in another data frame
I have a data frame containing the specification for a set of regression models (regress_grid) with a column for different aspects of the model. I then use dplyr::rowwise() to estimate a model for ...
0
votes
1
answer
71
views
How to implement a forwarding pattern to pass along a single argument into `dplyr::arrange()`?
I'm trying to forward arguments to dplyr functions in a function of my own cross_section(), namely to summarise(), group_by() and arrange() but I'm struggling to find the right pattern, particularly ...
4
votes
1
answer
106
views
force order of evaluation inside a tidypolars call
Is it possible in some way to force the evaluation order in a {tidypolars} pipe?
For example:
install.packages("tidypolars", repos = c("https://community.r-multiverse.org", '...
4
votes
2
answers
145
views
Rlang nested quotation fails for tibble but works for data.frame
I am trying to evaluate some user-provided arguments in a specific data environment using the rlang quasi-quotation approach. In addition, I want to wrap the output in a data.frame / tibble. However, ...
4
votes
3
answers
187
views
extracting grouping variables for fgroup_by using collapse package
I want to create a custom function for the collapse package where I supply unquoted grouping variables like so:
library(collapse)
library(tidyverse)
fgroup_by_no_entries <- function(df, ...) {
...
1
vote
1
answer
130
views
Using mutate inside a function to assign a new variable
I'm trying to better understand tidy evaluation and the use of rlang, but there's a specific use case I can't figure out. Let's say I want to run a linear mixed model and save the output of one ...
1
vote
1
answer
62
views
Wrapper around deparse() and substitute()
I'm trying to write a function that would simply wrap around deparse() and substitute() like so:
get_name <- function(x) {
deparse(substitute(x))
}
The point of get_name() is to return the ...
3
votes
1
answer
103
views
Tidyevaluation pattern to map over selected columns
outer_func uses tidy evaluation for col_rfs and dflt_flag, i.e. it is possible to do col_rfs = dplyr::starts_with("col").
library(magrittr)
outer_func <- function(dat, col_rfs, dflt_flag)...
0
votes
1
answer
61
views
Passing a function to `across()` inside `mutate()`
Here is what I want to achieve
library(dplyr)
mtcars %>% dplyr::summarise(
dplyr::across(mpg, ~ sum(.x) / sum(vs))
)
#> mpg
#> 1 45.92143
if I have this function stored in an object, I ...
1
vote
1
answer
73
views
What is the difference between expr and enexpr using tidy evalution in R?
In learning metaprogramming with R, I'm trying to understand the different concepts and mechanisms that form the tidy evaluation framework. I don't really understand the difference between expr() and ...
0
votes
1
answer
65
views
Running R Script at command line with commandArgs value in tidy evaluation in R programming
I have a r script, named myrsciprt.R as follows:
library(ggplot2)
library(ggpubr)
library(palmerpenguins)
create_plots_pdf <- function(output_filename, group_name){
# Histogram example: ...
1
vote
2
answers
69
views
Tidyeval and rlang within a nested tibble
I've attached a simple reproducible example below.
I'm currently working within a nested tibble with dynamic column names. I'm using the purrr::map() function to pass the column data into an anonymous ...
1
vote
1
answer
48
views
Is it possible to create a ggplot with interactions with tidy evaluation
I am trying to create a plot function that can plot different colors for a column with factors if .f2=NULL, and an interaction of the .f1 and .f2 columns if the .f2 parameter is not NULL.
The if ...
2
votes
3
answers
125
views
using tidy evaluation (curly-curly) with purrr
How can I iterate over functions that use tidy evaluation (via rlang's curly-curly)?
Here's a basic example, where I attempt to iterate over column names using purrr::map():
library("dplyr")
...
2
votes
1
answer
155
views
How to write a function with tidy eval when using the "arrow" R package (arrow::open_dataset) and dplyr verbs?
What I'm trying to do
I'm attempting to write a function that uses dplyr verbs and that takes an "arrow open dataset" as the first argument, and a column in that dataset as the second ...
0
votes
1
answer
55
views
Create function that uses tidy eval and the rescale function in R
I want to write a simple function that takes a dataframe and a numeric column (a parameter e.g. called col as input. The name of the numeric column should be quoted (like "mpg"). I am having ...
2
votes
1
answer
97
views
How to include a lookup table in a function for R ggplot graph and/or pass certain strings into graph?
I am trying to write a function in R for re-creating a ggplot graph. The graph is using climate data, and I want to be easily able to remake it for 3 different climate scenarios (rcp column) and ...
1
vote
2
answers
75
views
When is it necessary to use quotation marks to refer to a column in my dataframe and when can I do it without?
I'm fairly new in working with R, so I might not be naming so things right ;-)
I would like to plot the results of three different linear models. To do so, I loop over my column names, which I saved ...
0
votes
1
answer
46
views
How to flexibly supply a varying length argument to .l of pmap()
Below is a pmap() operation that requires my data to be in wide format. I perform a few simulations each day and capture the max value per simulation as post_max.
library(tidyverse)
POST_SIMS <- 2
...
1
vote
1
answer
33
views
How to programmatically implement flexibility to either apply a column or use a single color to fill argument in ggplot
I am creating a custom function that generates bar plots using ggplot2 and geom_col().
My objective is to enable the function to dynamically assign either a single color or a color based on a grouping ...
0
votes
1
answer
74
views
Tidy eval failing within a function
I'm trying to automate the production of .tex tables and I want to add in the add.lines section of stargazer a value called mean_outcome but I'm having trouble producing this value, I think I'm ...
0
votes
1
answer
76
views
how to evaluate textInput inside a Shiny Module as arguments
I am working on a Shiny app that serves as an interface for the gtsummary package.
One of the features I've added is the ability to apply functions like add_p() to the generated tables, and it's ...
2
votes
2
answers
2k
views
Tidyeval problem in `case_when` when using function in right side formula
Consider the following structure:
tmp_data <- structure(
list(
currency = c("EUR", "USD"),
funding = c(50, 700)),
class = c("tbl_df", "tbl", "...
1
vote
1
answer
114
views
Issue with ggplot2 facet_wrap labels using tidy evaluation in R
I am trying to label my facet_wrap titles based on an aggregation of data in my data frame.
I am writing a function to plot different data sets, so I am using a function argument to select the column ...
2
votes
1
answer
151
views
Build a dynamic-dots list to read dataframe one row at a time
I am trying to build a GET request one row at a time from a dataframe where the possible parameters are a varied and large list, and the dataframe I'm passing to the function may not have the ...
0
votes
1
answer
84
views
Is there a better way to use NSE in a function to concatenate dataframe columns?
Background
I'm trying to get my grip on meta-programming methods in Advanced R, and not being a programmer by background, it is taking some effort. I am trying to write functions to manipulate ...
1
vote
1
answer
93
views
Why are these error messages inconsistent when using NSE in R?
Question
Why do I get inconsistent error messages in R from a function that uses non-standard evaluation, and what is the best way to control this / write the function?
Initial setup that works as ...
0
votes
1
answer
52
views
function arguments not found in call environment
I'm creating a function which takes column names as arguments and to plot survival plots. however my arguments can't be found in the calling function.
thanks in advance for your help
plt_survive <- ...
0
votes
2
answers
479
views
How to use external character values to select x and y variables in ggplot?
I have data with multiple observable variables (say, x,y,z), indexed by another set of variables (sect, and item). Each time I run an experiment I get such a set of observations. So for experiment &...
0
votes
1
answer
119
views
Wrapper function to purrr::map() over column names using tidyeval function
I've got a function that uses tidyeval to refer to a column name, but also needs to access that column name as a string. The code for that works fine, see below (though improvements welcome, as_name(...
0
votes
0
answers
39
views
Unsure what is wrong with summarize() in my custom function
I have a list of tables that I need to make using different variables, and I want to make a function that will let me make the tables differently each time by passing through different variables as ...
4
votes
1
answer
122
views
why does across work with coalesce only when inside a function?
Little tidyeval question, I've got this function:
coacross <- function(...) {
coalesce(!!!across(...))
}
And then if I do something like this:
aa = tibble(a1 = c(NA, 1), a2 = c(2, NA), b = 3:4) %...
2
votes
1
answer
94
views
Write function that access data from dplyr context
Disclaimer: this is a very elemental question. I'll use an example to make it easier, but the question has nothing to do with the example itself.
Supose you have a dataframe df:
# A tibble: 5 × 4
...
3
votes
3
answers
703
views
R ggplot2: Replacing aes_string() with aes() and .data[[varname]] when varname may be NULL
I am working on updating some older R code to make it use ggplot2's aes() now that aes_string() is deprecated. I admit I don't understand all of the details of tidy evaluation, but from what I do ...
0
votes
2
answers
323
views
how to avoid tidy evaluation (especially double curly brace) in user define function in r?
id <- 1:30
x<-rnorm(30,1,10)
y<-rnorm(30,1,10)
data<-data.frame(id,x,y)
add<-function(data,
y_1,
y_2){
data<-dplyr::mutate(data,
...
2
votes
1
answer
128
views
In R how do I pass multiple variable names to a function using tidyeval
I would like to pass the names of several variables to a function that is using tidyeval.
I know I can pass the vars like this:
a_summary_fn <- function(the_data, the_group, vars) {
the_data |...
2
votes
2
answers
150
views
Use custom function (with tidy eval) within mutate & across
I have the following data:
my_data <- structure(list(case_one = c("A", "B", "A", "B", "A", "B", "A",
"B", "A&...
1
vote
1
answer
72
views
pass input parameter of function 1 to function 2 without explicitly passing it as an input to function 2
I'm chaining two functions together where both use the same column to perform some operation. I'd like to be able to specify this column as the input parameter only on the call to the first function ...
1
vote
1
answer
103
views
using dot-dot-dot (`...`) with `str_replace_all()` in R
I have a function which is a wrapper around stringr::str_c(), and therefore receives any number of character vectors as input using the ... argument.
The issue is that I would like to perform some ...
1
vote
1
answer
97
views
Tidy Eval - pass mutated column to a group by - R
I'm running into an issue passing a column being created in mutate to the the group_by function.
here is some example data:
set.seed(42) ## for sake of reproducibility
w2_test <- data.frame(id=1:...
1
vote
1
answer
122
views
pass values through to piped dplyr verb inside if{} statement in function
Say I have this data:
d <- msleep %>%
mutate(comfort_color_desk = sample(c(0,1), replace=TRUE, size=nrow(msleep)),
comfort_color_chair = sample(c(0,1), replace=TRUE, size=...
2
votes
3
answers
354
views
Evaluate function argument as string within dplyr custom function
I am writing a custom function that outputs the mean and sd of a field. I would like the .field argument to be evaluated in a tidyeval manner at some states and as a string at another stage. How can I ...
1
vote
1
answer
69
views
Can you use a parameter in a function name with tidy eval/NSE/metaprogramming?
I am writing a function that makes ggplots with given data, and I am interested in setting a parameter that can call the different scales::label_* functions to change the outputs. For example, I want ...
1
vote
1
answer
49
views
NSE error handling: check for `NA` with NSE/tidy eval
I am writing a function using tidyeval, and I ran into a problem while trying to check for NAs.
I am writing a function to make plots using ggplot2 that looks something like:
my_func <- function(...
0
votes
1
answer
92
views
Tidy eval for `by` in `dplyr::_join
I am writing a function to join two datasets using dplyr::_join where the by terms are parameters passed in without quotes. I have seen quite a few solutions to this issue, but all seem to be dated ...
1
vote
1
answer
50
views
Current way to use rlang and tidyeval inside functions
Just wondering what the current start of the art is for using tidyeval principles inside a function.
Imagine we have two tibbles in our environment, and we want to pass their names to a purrr::map ...
1
vote
3
answers
253
views
R - dplyr how do I reference a column created in a function with the walrus operator :=
I am trying to create a function and I want to reference a column I previously created inside the function with the {{}} and :=. How can I reference the "{{col}}_d" column?
library(...
0
votes
1
answer
69
views
map does not work on vector which i am trying to use
I have the data as below
adsl <- structure(list(SUBGRPVAR1 = structure(c("F", "M", "M", "M", "F",
"F"), label = "Sex"), ...
1
vote
1
answer
432
views
Use mutate + across with case_when with a list of conditions
I want to modify a list of columns. Each column has a list of conditions that need to be applied with a case_when.
I tried the following:
df <- tibble(value=c("a","b","c&...
2
votes
1
answer
1k
views
ggplot2 mappings changing when using tidy evaluation and aes() instead of aes_string()
I am trying to update some older plot-making code that gives the message:
Warning message: `aes_string()` was deprecated in ggplot2 3.0.0. Please use tidy evaluation ideoms with `aes()`
When I ...