rlang

using `rlang` quasiquotation with `dplyr::_join` functions

梦想与她 提交于 2021-02-19 05:45:26
问题 I am trying to write a custom function where I use rlang 's quasiquotation. This function also internally uses dplyr 's join functions. I have provided below a minimal working example that illustrated my problem. # needed libraries library(tidyverse) # function definition df_combiner <- function(data, x, group.by) { # check how many variables were entered for this grouping variable group.by <- as.list(rlang::quo_squash(rlang::enquo(group.by))) # based on number of arguments, select `group.by`

How to supply variable names as strings in fct_reorder within ggplot code?

江枫思渺然 提交于 2021-02-08 10:24:09
问题 I want to create the geom_col chart by supplying the variable names as strings. I know the aes_string does that in the ggplot code. However, I am not sure how to handle the variables supplied in the fct_reorder to order the bars. Here is my wip code. I want to convert this: mpg %>% ggplot() + geom_col(aes(x = cty, y = fct_reorder(drv, cty, .fun = mean, na.rm = T), fill = drv), width = 0.8) + theme_classic() into a function, chart_fun <- function(x, y) { mpg %>% ggplot() + geom_col(aes_string

Programming with tidyeval: The mutate function after tidyr::unite(col = !!col)

倾然丶 夕夏残阳落幕 提交于 2021-02-08 04:54:20
问题 So I want to make a function with unite() from tidyr, but it does not seem to work.. library(dplyr, warn.conflicts = FALSE) library(tidyr, warn.conflicts = FALSE) library(stringr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg , cyl) %>% mutate_all(as.character) %>% unite(col = hello, sep = "/") %>% mutate(hello = str_replace(hello, "/", "")) #> # A tibble: 32 x 1 #> hello #> <chr> #> 1 216 #> 2 216 #> 3 22.84 #> 4 21.46 #> 5 18.78 #> 6 18.16 #> 7 14.38 #> 8 24.44 #> 9 22.84 #>

Programming with tidyeval: The mutate function after tidyr::unite(col = !!col)

白昼怎懂夜的黑 提交于 2021-02-08 04:54:09
问题 So I want to make a function with unite() from tidyr, but it does not seem to work.. library(dplyr, warn.conflicts = FALSE) library(tidyr, warn.conflicts = FALSE) library(stringr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg , cyl) %>% mutate_all(as.character) %>% unite(col = hello, sep = "/") %>% mutate(hello = str_replace(hello, "/", "")) #> # A tibble: 32 x 1 #> hello #> <chr> #> 1 216 #> 2 216 #> 3 22.84 #> 4 21.46 #> 5 18.78 #> 6 18.16 #> 7 14.38 #> 8 24.44 #> 9 22.84 #>

Programming with tidyeval: The mutate function after tidyr::unite(col = !!col)

拈花ヽ惹草 提交于 2021-02-08 04:54:06
问题 So I want to make a function with unite() from tidyr, but it does not seem to work.. library(dplyr, warn.conflicts = FALSE) library(tidyr, warn.conflicts = FALSE) library(stringr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg , cyl) %>% mutate_all(as.character) %>% unite(col = hello, sep = "/") %>% mutate(hello = str_replace(hello, "/", "")) #> # A tibble: 32 x 1 #> hello #> <chr> #> 1 216 #> 2 216 #> 3 22.84 #> 4 21.46 #> 5 18.78 #> 6 18.16 #> 7 14.38 #> 8 24.44 #> 9 22.84 #>

How to pass a filter statement as a function parameter in dplyr using quosure [duplicate]

白昼怎懂夜的黑 提交于 2021-02-05 07:55:09
问题 This question already has answers here : dplyr/rlang: parse_expr with multiple expressions (3 answers) Closed 9 months ago . Using the dplyr package in R , I want to pass a filter statement as a parameter in a function. I don't know how to evaluate the statement as code instead of a string. When I try the code below, I get an error message. I'm assuming I need a quosure or something, but I don't fully grasp that concept. data("PlantGrowth") myfunc <- function(df, filter_statement) { df %>%

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

人走茶凉 提交于 2021-02-05 07:26:05
问题 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 again, but when I reach the mutate step it either repeats the text string as the column values or will not evaluate. I've recreated the code below: #Testing rename, select, and mutate use cases for enquo() #Load packages library(dplyr) library

Using formulas with aliases to perform multi-column operations

人盡茶涼 提交于 2021-01-29 14:36:57
问题 This question is related to a previous one I asked, but trying to be more generic. I want to use formulas to perform operations on multiple "groups" of data (i.e. a_data1 , a_data2 , b_data1 , b_data2 , and then make operations using the *_data1 columns). Based on @akrun's answer to that question, I created the following function. It takes a one-sided formula and applies it to all the "groups of data": suppressPackageStartupMessages({ library(dplyr) library(tidyr) }) polymutate <- function(df

How do I pipe into or include an additional argument within a list of quosures?

ぃ、小莉子 提交于 2021-01-27 11:58:00
问题 I have a list of quosures in my_q_list below: library(rlang) suppressPackageStartupMessages(library(dplyr)) q_list <- function(...) { enquos(...) } my_q_list <- q_list( select(mpg, hp), filter(hp > 20), mutate(mpg2 = mpg*2) ) my_q_list #> <list_of<quosure>> #> #> [[1]] #> <quosure> #> expr: ^select(mpg, hp) #> env: global #> #> [[2]] #> <quosure> #> expr: ^filter(hp > 20) #> env: global #> #> [[3]] #> <quosure> #> expr: ^mutate(mpg2 = mpg * 2) #> env: global Created on 2020-07-01 by the

How do I pipe into or include an additional argument within a list of quosures?

喜你入骨 提交于 2021-01-27 11:57:35
问题 I have a list of quosures in my_q_list below: library(rlang) suppressPackageStartupMessages(library(dplyr)) q_list <- function(...) { enquos(...) } my_q_list <- q_list( select(mpg, hp), filter(hp > 20), mutate(mpg2 = mpg*2) ) my_q_list #> <list_of<quosure>> #> #> [[1]] #> <quosure> #> expr: ^select(mpg, hp) #> env: global #> #> [[2]] #> <quosure> #> expr: ^filter(hp > 20) #> env: global #> #> [[3]] #> <quosure> #> expr: ^mutate(mpg2 = mpg * 2) #> env: global Created on 2020-07-01 by the