Questions tagged [quosure]

79 questions
18
votes
4 answers

Pass column names as strings to group_by and summarize

With dplyr starting version 0.7 the methods ending with underscore such as summarize_ group_by_ are deprecated since we are supposed to use quosures. See: https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html I am trying to…
witek
  • 984
  • 1
  • 8
  • 25
10
votes
4 answers

Function which runs lm over different variables

I would like to create a function which can run a regression model (e.g. using lm) over different variables in a given dataset. In this function, I would specify as arguments the dataset I'm using, the dependent variable y and the independent…
Joost
  • 103
  • 5
8
votes
2 answers

dplyr mutate using variable columns

I am trying to use mutate to create a new column with values based on a specific column. Example final data frame (I am trying to create new_col): x = tibble(colA = c(11, 12, 13), colB = c(91, 92, 93), col_to_use = c("colA",…
burger
  • 5,683
  • 9
  • 40
  • 63
6
votes
5 answers

dplyr case_when with dynamic number of cases

Wanting to use dplyr and case_when to collapse a series of indicator columns into a single column. The challenge is I want to be able to collapse over an unspecified/dynamic number of columns. Consider the following dataset, gear has been split into…
Simon.S.A.
  • 6,240
  • 7
  • 22
  • 41
6
votes
2 answers

Creating dplyr function that can tell if variable input is a string or a symbol

I've been studying the "Programming with dplyr" vignette because I want to create functions that use dplyr functions. I would like to use the functions I make in both shiny applications and interactive R work. For use in shiny, I would like these…
Dave Rosenman
  • 1,252
  • 9
  • 13
5
votes
2 answers

In writing an R package, using the flowCore::transform function, can I both use a variable name as text and get the actual value?

I am trying to pass on an argument to a function, which is a string but must be evaluated both for it's name (symbol?) and it's value (see example below). So far I am able to use base::get to get the actual value, but the assignment in…
FM Kerckhof
  • 1,270
  • 1
  • 14
  • 31
5
votes
1 answer

using `rlang::exec` with functions that use `rlang::ensym`

I am trying to write a custom function which is a bit more complicated so for the sake of simplicity I have created toy examples. Let's say I want to write a function that- automatically decides the appropriate function to run: for example, a…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
5
votes
2 answers

How do I pass a dynamic variable name created using enquo() to dplyr's mutate for evaluation?

I'm creating a workflow that contains the same piping steps of renaming, selecting by, then mutating all using a name I provide prior to the pipe. I have had success using enquo() and !!(bang bang) to rename to my desired string and then select it…
Adam Kemberling
  • 301
  • 1
  • 11
5
votes
1 answer

Passing quasiquoted arguments within nested functions

Below I've written a simple function snafu() that calculates a new variable snafu_var. library(dplyr) df <- mtcars %>% select(am, cyl) %>% slice(1:5) snafu <- function(data, var1, var2){ require(dplyr) var1 <- enquo(var1) var2 <-…
Joe
  • 3,217
  • 3
  • 21
  • 37
5
votes
1 answer

dplyr .data pronoun vs "quosure" approach

In dplyr v0.7.0, the .data pronoun was introduced that allowed us to refer to variables with strings. I was just curious as to whether this approach was preferred over the "quosure" approach. For example, here is an approach that uses the .data…
TinyHeero
  • 580
  • 1
  • 4
  • 18
4
votes
3 answers

Convert a quosure with dashes to a string?

When I do: > quo(DLX6-AS1) The output is: expr: ^DLX6 - AS1 env: global Which inserts spaces around the dash. When I try to convert that to a string, I get either: quo(DLX6-AS1) %>% quo_name "DLX6 - AS1" or quo(DLX6-AS1) %>%…
Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46
4
votes
2 answers

ggplot with aesthetics generated from input data

Since I will need to make a lot of different plots in R I'm trying to put some more logic in preparing the data (add column names corresponding to the aesthetics) and less logic in the plot itself. Consider the following default iris…
Martin
  • 1,084
  • 9
  • 15
4
votes
2 answers

R: why group_by still requires "do" even when using quosures

How to make a user-defined function work nicely with pipes and group_by? Here is a simple function: library(tidyverse) fun_head <- function(df, column) { column <- enquo(column) df %>% select(!!column) %>% head(1) } The function works nicely…
Irakli
  • 959
  • 1
  • 11
  • 18
4
votes
0 answers

mutate_at vs. quosure with string arguments

When a parameter of a function is a string rather than a quosure (e.g. from a Shiny UI) I can think of 2 options to use it in a dplyr::mutate call: string -> quosure library(dplyr) var <- "am" mtcars %>% mutate(!!rlang::sym(var) :=…
thothal
  • 16,690
  • 3
  • 36
  • 71
4
votes
1 answer

using quosures within formula inside an anonymous function

I am trying to use quosures to pass along variable names within a custom function for data processing and use in a formula, but my use of quosures in the formula is not correct. Is there a better way to unquote arguments within a…
Joe
  • 3,217
  • 3
  • 21
  • 37
1
2 3 4 5 6