Questions tagged [tidyeval]

Please use this tag for questions about using tidy evaluation within the tidyverse framework. For more information please refer to this handbook: https://tidyeval.tidyverse.org/

473 questions
7
votes
1 answer

How to write functions with tidy evaluation inside ggplot geoms?

I would like to write a function that performs aesthetic mappings in ggplot. The function is supposed to have two arguments: var is supposed to be mapped to aesthetic. The first code block below actually works. However, I would like to do the…
Till
  • 707
  • 3
  • 14
7
votes
2 answers

how to unquote (!!) inside `map` inside `mutate`

I'm modifying nested data frames inside of foo with map2 and mutate, and I'd like to name a variable in each nested data frame according to foo$name. I'm not sure what the proper syntax for nse/tidyeval unquotation would be here. My…
lost
  • 1,483
  • 1
  • 11
  • 19
7
votes
6 answers

Use quoted variable in group_by() %>% mutate() function call

Reproducible example cats <- data.frame( name = c(letters[1:10]), weight = c(rnorm(5, 10, 1), rnorm(5, 20, 3)), type = c(rep("not_fat", 5), rep("fat", 5)) ) get_means <- function(df, metric, group) { df %>% …
Robert Tan
  • 634
  • 1
  • 8
  • 21
7
votes
2 answers

ggplot2 facet grid with conditional facets and tidy evaluation

I want to create a function that produces a ggplot plot and provide an optional argument for a facetting variable to facet_grid(). In particular, if possible, I want to incorporate the conditional logic inside facet_grid. I also want to use the…
user3646834
7
votes
5 answers

Create R function using dplyr::filter problem

I've looked at other answers but cannot find a solution for the code below to work. Basically, I'm creating a function that inner_join the two data frame and filter based on a column inputted in the function. The problem is that the filter part of…
x85ms16
  • 587
  • 7
  • 17
7
votes
1 answer

How to define key argument of gather function using string concatenation

I've got a tibble with interaction of few factors as a column names (see example with two factors below). ex <- structure(list(`Monday*FALSE` = 42.74, `Monday*TRUE` = 70.68, `Tuesday*TRUE` = 44.05, `Tuesday*FALSE` = 51.25, `Wednesday*TRUE` = 35.57,…
Kuba_
  • 886
  • 6
  • 22
7
votes
3 answers

Using as.formula with a comma

I'd like to get conditions dynamically from the user, so I built a shiny app that gets them from an input field. Problem is that as.formula doesn't work for a character vector with a comma (without it works fine). Code: all_conditions = …
7
votes
2 answers

String based filtering in dplyr - NSE

I'd like to use dplyr's new NSE notations (version >= 0.6) for a dynamic filter on my data. Let's say I have the following dummy dataset: df = data_frame(x = 1:10, y = 10:1, z = 10 * runif(10)) If now I want to filter column tofilter = "x" for…
Lorenzo Rossi
  • 1,481
  • 1
  • 9
  • 16
6
votes
1 answer

NSE in nested function calls

I'd like to use a utility function to check whether a given column exists within a given data.frame. I'm piping within the tidyverse. The best I've come up with so far is library(magrittr) columnExists <- function(data, col) { tryCatch({ …
Limey
  • 10,234
  • 2
  • 12
  • 32
6
votes
3 answers

How do I create a function to mutate new columns with a variable name and "_pct"?

Using mtcars as an example. I would like to write a function that creates a count and pct column such as below - library(tidyverse) mtcars %>% group_by(cyl) %>% summarise(count = n()) %>% ungroup() %>% mutate(cyl_pct =…
The Rookie
  • 877
  • 8
  • 15
6
votes
3 answers

Passing variables into the names_glue parameter of tidyr::pivot_wider

Here's some silly data that we pivot wider, using two names: library(tidyr) df <- data.frame( food = c('banana','banana','banana','banana','cheese','cheese','cheese','cheese'), binary = c(rep(c('yes','no'), 4)), car =…
heds1
  • 3,203
  • 2
  • 17
  • 32
6
votes
1 answer

using `rlang` NSE to group by multiple variables

I am trying to write a custom function that uses rlang's non-standard evaluation to group a dataframe by more than one variable. This is what I've- library(rlang) # function definition tryfn <- function(data, groups, ...) { # preparing data df…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
6
votes
4 answers

Generating dynamic variables referencing existing variables

I'm trying to generate a correlation matrix with significance stars. Take the following dataframe: df <- tibble(stub = c(1,2,3,4), stub_pvalue = c(.00, .04, .07,.2)) I'd like to write a function that pastes any column (e.g. stub in…
6
votes
2 answers

confusing behavior of purrr::pmap with rlang; "to quote" or not to quote argument that is the Q

I have a custom function where I am reading entered variables from a dataframe using rlang. This function works just fine irrespective of whether the arguments entered are quoted or unquoted. But, strangely enough, when this function is used with…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
6
votes
1 answer

Supplying multiple groups of variables to a function for dplyr arguments in the body

Here is the data: library(tidyverse) data <- tibble::tribble( ~var1, ~var2, ~var3, ~var4, ~var5, "a", "d", "g", "hello", 1L, "a", "d", "h", "hello", 2L, "b", "e", "h", "k", 4L, "b", "e", "h", …
Geet
  • 2,515
  • 2
  • 19
  • 42
1 2
3
31 32