Questions tagged [quosure]

79 questions
4
votes
1 answer

Issue with Example in : Programming with dplyr

Refer: http://dplyr.tidyverse.org/articles/programming.html This Code works Fine: df <- tibble( g1 = c(1, 1, 2, 2, 2), g2 = c(1, 2, 1, 2, 1), a = sample(5), b = sample(5) ) my_summarise <- function(df, group_by) { group_by <-…
guna
  • 1,148
  • 1
  • 10
  • 18
4
votes
2 answers

Parse and evaluate quosures from string

Is there a way to parse and evaluate a quosure from a string. I would like to achieve the same output as in the example below: library(rlang) a <- 10 quo(UQ(a) + 2 * b) ## ## ~10 + 2 * b but starting from t <- "UQ(a) + 2 *…
johannes
  • 14,043
  • 5
  • 40
  • 51
3
votes
3 answers

Accept both bare (from rlang) or string as a function input

I editing an existing function in a package. Currently, the function accepts a column name in a data frame as a string. I am updating the function to accept either a string name or a bare name. But I am running into some issues. The general…
Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
3
votes
2 answers

Creating a new formula of type `~ x + y` using `rlang`

I am trying to write a custom function where I want to use the cor.test function but I am having trouble unquoting the needed arguments to create a working formula. Here is what I currently have that doesn't work- library(rlang) # custom…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
3
votes
3 answers

R & quosures - How to get names of symbols contained in a vector passed as function argument?

I want to write an R function arg2str that returns the names (that is a vector of strings) of the symbols that are fed as arguments. For the most simple case, I only have one input symbol: library ("rlang") arg2str.v0 <- function (arg)…
Robin LP
  • 41
  • 4
3
votes
2 answers

Passing column name as parameter to a function using dplyr

I have a dataframe like below : transid<-c(1,2,3,4,5,6,7,8) accountid<-c(a,a,b,a,b,b,a,b) month<-c(1,1,1,2,2,3,3,3) amount<-c(10,20,30,40,50,60,70,80) transactions<-data.frame(transid,accountid,month,amount) I am trying to write function for total…
vsb
  • 428
  • 6
  • 15
3
votes
1 answer

Passing an expression into `MoreArgs` of `mapply`

I'm doing some programming using dplyr, and am curious about how to pass an expression as (specifically a MoreArgs) argument to mapply? Consider a simple function F that subsets a data.frame based on some ids and a time_range, then outputs a summary…
Andreas
  • 1,923
  • 19
  • 24
2
votes
3 answers

tidyverse mutate with an external vector whose elements are column values in a data frame

Let's assume the following vector: prices <- c(10, 15, 20, 30) and the following data frame: df <- data.frame(count = c(1, 3, 2, 4)) What I want to do is, depending on the count value in df, taking the 1:count values from the prices vector and sum…
deschen
  • 10,012
  • 3
  • 27
  • 50
2
votes
1 answer

How do I quote a newly created variable in a function to a helper function?

Question What is the proper way to quote a parameter in a function that will be used to create a new variable that will be passed to another function? Background Ultimate goal is to create labels in a dataframe for a treemap with 2 levels of…
ScottyJ
  • 945
  • 11
  • 16
2
votes
1 answer

quosure in case_when and/or ifelse

I am trying to sort out how to use a quosure (if that's the right tool) to pass variable names to either an if_else(...) or a case_when(...) inside a mutate command using a string argument passed from a function. A quick reproducible example that…
Andrew Leach
  • 137
  • 1
  • 10
2
votes
2 answers

Tidy evaluation syntax in R user-defined function

I want to define a generic function func_boxplot2 <- function(tmp, xvar, yvar, groupvar) { xvar <- enquo(xvar) yvar <- enquo(yvar) groupvar <- enquo(groupvar) # If variable yield exists, put concentrations to NA for all yields <…
Alessandro
  • 129
  • 1
  • 8
2
votes
1 answer

function args for mutate_if in dplyr work for soft-depreciated funs() but not for list()

I am trying to update my following code because funs( MY_FUN ) is soft depreciated. I know that the replacement for this should be list( ~MY_FUN ), but this doesnt seem to be working for my code. Here are my data frames: fake_data <- data.frame(var1…
Voy
  • 99
  • 4
2
votes
0 answers

How to dynamically update the variable name passed to the ARIMA function in the fable package in an R loop,?

I am trying to estimate a series of ARIMA models in a loop, passing in a different dependent variable each iteration from from a list of dependent variables using the. I am trying to use the fable package to do this in R. But I can't seem to pass…
2
votes
2 answers

Convert list of symbols to character string in own function

I have the following data frame: dat <- data.frame(time = runif(20), group1 = rep(1:2, times = 10), group2 = rep(1:2, each = 10), group3 = rep(3:4, each = 10)) I'm now writing a function…
deschen
  • 10,012
  • 3
  • 27
  • 50
2
votes
1 answer

Using dplyr quosure custom function with mutate_at

I am trying to build a helper function that extract the digits in the column given in argument. I'm able to use my function inside mutate (and repeat it for all columns of interest), but it doesn't seems to work inside mutate_at. Here is an example…