tidyeval

Passing a list of arguments to a function with quasiquotation

守給你的承諾、 提交于 2019-12-18 05:13:24
问题 I am trying to write a function in R that summarizes a data frame according to grouping variables. The grouping variables are given as a list and passed to group_by_at , and I would like to parametrize them. What I am doing now is this: library(tidyverse) d = tribble( ~foo, ~bar, ~baz, 1, 2, 3, 1, 3, 5 4, 5, 6, 4, 5, 1 ) sum_fun <- function(df, group_vars, sum_var) { sum_var = enquo(sum_var) return( df %>% group_by_at(.vars = group_vars) %>% summarize(sum(!! sum_var)) ) } d %>% sum_fun(group

How to pass strings denoting expressions to dplyr 0.7 verbs?

谁说胖子不能爱 提交于 2019-12-17 22:25:53
问题 I would like to understand how to pass strings representing expressions into dplyr, so that the variables mentioned in the string are evaluated as expressions on columns in the dataframe. The main vignette on this topic covers passing in quosures, and doesn't discuss strings at all. It's clear that quosures are safer and clearer than strings when representing expressions, so of course we should avoid strings when quosures can be used instead. However, when working with tools outside the R

R: Using a string as an argument to mutate verb in dplyr

偶尔善良 提交于 2019-12-17 16:30:49
问题 I am building a shiny app which needs to allow users to define new variables for plotting. Specifically I want to allow users to define an expression to be used in mutate verb. The server receives the expression as text and I am wondering how to make mutate execute it in dplyr 0.7. I can make it work (partially) using mutate_ but it is deprecated now. It also defines the new column name as the entire expression rather than the new variable Here is a reproducible example: input_from_shiny <-

Why is enquo + !! preferable to substitute + eval

前提是你 提交于 2019-12-17 09:33:15
问题 In the following example, why should we favour using f1 over f2 ? Is it more efficient in some sense? For someone used to base R, it seems more natural to use the "substitute + eval" option. library(dplyr) d = data.frame(x = 1:5, y = rnorm(5)) # using enquo + !! f1 = function(mydata, myvar) { m = enquo(myvar) mydata %>% mutate(two_y = 2 * !!m) } # using substitute + eval f2 = function(mydata, myvar) { m = substitute(myvar) mydata %>% mutate(two_y = 2 * eval(m)) } all.equal(d %>% f1(y), d %>%

dplyr mutate multiple columns based on names in vectors

痴心易碎 提交于 2019-12-13 00:23:36
问题 I want to multiply two columns with each other by using dplyr's mutate function. But instead of writing a new line for each mutate conditions I would like to use the names of the columns stored in the vectors var1 and var2 . For example in the end I want to have a additional column in my existing bankdata with the name result1 which contains the result by multiplying the columns cash and loans with each other. This shall be continued until 3 new columns have been created. Reproducible code:

Using the pipe in selfmade function with tidyeval (quo_name)

我是研究僧i 提交于 2019-12-11 15:17:32
问题 I have two functions: date_diff and group_stat. So I have read this article tidyverse and I try so create simple functions and use the pipe. The first function creates a difftime and names them timex_minus_timey but when I pipe this result into the next function I have to look at the name so I can fill in summary_var. Is there a better way to do this? library(tidyverse) # set.seed(42) data <- dplyr::bind_rows( tibble::tibble(Hosp = rep("A", 1000), drg = sample(letters[1:5], 1000, replace =

Constructing lists using tidyeval tools (like `!!` and `:=`)

谁说我不能喝 提交于 2019-12-11 04:38:55
问题 I am looking a way for easy list constructing based on R 's tidyeval framework as defined in the rlang package. Below is what I want to achieve: a <- "item_name" b <- "item_value" identical( list(!!a := !!b), # list(!!a := b) is of course also fine list(item_name = "item_value") ) What I can obtain at the moment is: list(!!a := !!b) # output [[1]] `:=`(!(!a), !(!b) Alternatively it can get perhaps a little bit better when adding quosure : quo(list(!!a := !!b)) # output <quosure: global> ~list

Error using tidyeval quo() with gather()

我与影子孤独终老i 提交于 2019-12-10 22:54:38
问题 I am trying to run gather() on data frames, and programmatically assign the .key column name using !!quo(). But I keep getting 'Error: Invalid column specification'. I even found a closed ticket where it shows that it should work (https://github.com/tidyverse/tidyr/issues/293). I'm going to go back to using rename_() as a workaround, but it would be nice to use the more elegant NSE. library('tidyverse') data(mtcars) my_var <- 'my_col_name' The following works, but is a one-trick pony > mtcars

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

廉价感情. 提交于 2019-12-10 15:16:49
问题 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 %>% group_by(.[[group]]) %>% mutate(mean_stat = mean(.[[metric]])) %>% pull(mean_stat) %>% unique() } get_means(cats, metric = "weight", group = "type") What I tried I expect to get two values back, instead I get one value. It appears that the groupby is failing. I tried everything including

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

安稳与你 提交于 2019-12-10 04:22:42
问题 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 purrr::pmap , it works only if the argument is quoted. So I have two questions: Why does the function behavior this way? How can I make a function using rlang such that I won't have to quote the arguments even if used in purrr::pmap ? Here is a