tidyeval

How to use tidyeval on a column to mutate?

佐手、 提交于 2019-11-29 12:01:26
I'm sorry for the confusion but eventually, the first example I posted (at the bottom of the page), did not help me to figure out how tidyeval works with mutate, so I'm adding a new example. I would like to create a function that takes three args: a dataframe the column(s) to mutate a variable (from the dataframe) to replace the values that are being mutated For instance, to replace the values in mpg with the values from carb I tried this: I tried this colToX <- function(dt, ..., repl) { cols <- quos(...) repl <- quo(repl) mutate_at(dt, vars(!!!cols), funs(function(x) !!repl)) } colToX(mtcars,

Passing a list of arguments to a function with quasiquotation

本小妞迷上赌 提交于 2019-11-29 11:43:37
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_vars = c("foo", "bar"), baz) However, I would like to call the function like so: d %>% sum_fun(group

dplyr 0.7 equivalent for deprecated mutate_

拈花ヽ惹草 提交于 2019-11-29 10:57:57
I cannot find in dplyr 0.7 a way to replace the mutate_ function which is going to be deprecated. The mutate_ function is useful in my use case : I store in a database (string format) many instructions (that can be filtered if needed) and apply these instructions to one or several data frames. For example : dplyr::tibble(test = "test@test") %>% dplyr::mutate_(.dots = list("test2" = "substr(test, 1, 5)", "test3" = "substr(test, 5, 5)")) Is there a way to do this with dplyr 0.7 keeping variables and instructions as character? To expand a little bit on MrFlick 's example, let's assume you have a

How to use dplyr's enquo and quo_name in a function with tidyr and ggplot2

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 03:05:58
问题 library(dplyr) #Devel version, soon-to-be-released 0.6.0 library(tidyr) library(ggplot2) library(forcats) #for gss_cat data I'm attempting to write a function that combines quosures from the soon-to-be-released dplyr devel version together with tidyr::gather and ggplot2 . So far it seems to work with tidyr , but I'm having trouble with the plotting. The below function seems to work with tidyr's gather : GatherFun<-function(gath){ gath<-enquo(gath) gss_cat%>%select(relig,marital,race,partyid)%

How to pass strings denoting expressions to dplyr 0.7 verbs?

老子叫甜甜 提交于 2019-11-28 19:02:05
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 ecosystem, such as javascript or YAML config files, one will often have to work with strings instead of

Scale value inside of aes_string()

天大地大妈咪最大 提交于 2019-11-28 13:04:03
I want to scale my y-var by multiplying it by a number, say 10, in ggplot. The problem is this is in a Shiny app and the variable must be passed as a character string, i.e. input$variable . How can I multiply one of the variables in aes_string() the same way I would in aes() ? Here is an example of when it fails: library(ggplot2) ggplot(data = subset(mtcars, cyl == 4), aes_string(x = "wt", y = "mpg")) + geom_line(size = 1.5, color = "#00868B") + geom_line(data = subset(mtcars, cyl == 8), aes_string(x = "wt", y = "mpg" * 10)) Error in "mpg" * 10 : non-numeric argument to binary operator You can

Non-standard evaluation and quasiquotation in dplyr() not working as (naively) expected

末鹿安然 提交于 2019-11-28 12:41:09
I am trying to search a database and then label the ouput with a name derived from the original search, "derived_name" in the reproducible example below. I am using a dplyr pipe %>% , and I am having trouble with quasiquotation and/or non-standard evaluation. Specifically, using count_colname , a character object derived from "derived_name" , in the final top_n() function fails to subset the dataframe. search_name <- "derived_name" set.seed(1) letrs <- letters[rnorm(52, 13.5, 5)] letrs_count.df <- letrs %>% table() %>% as.data.frame() count_colname <- paste0(search_name, "_letr_count")

Using dplyr::quos() with a list argument rather than the ellipsis argument

ε祈祈猫儿з 提交于 2019-11-28 09:06:03
问题 I am using dplyr and trying to create a function to calculate p.values based on grouping arguments. I would like to be able to have an argument that would be list of any length of variables to group by. Here is the example dataset: dataset <- structure(list(Experiment = c(170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222, 170222,

dplyr 0.7 equivalent for deprecated mutate_

半世苍凉 提交于 2019-11-28 04:07:13
问题 I cannot find in dplyr 0.7 a way to replace the mutate_ function which is going to be deprecated. The mutate_ function is useful in my use case : I store in a database (string format) many instructions (that can be filtered if needed) and apply these instructions to one or several data frames. For example : dplyr::tibble(test = "test@test") %>% dplyr::mutate_(.dots = list("test2" = "substr(test, 1, 5)", "test3" = "substr(test, 5, 5)")) Is there a way to do this with dplyr 0.7 keeping

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

六眼飞鱼酱① 提交于 2019-11-27 23:11:59
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 <- "Petal.ratio = Petal.Length/Petal.Width" iris_mutated <- iris %>% mutate_(input_from_shiny) This gives