tidyeval

tidy eval with e.g. mtcars %>% mutate(target := log(target))

本小妞迷上赌 提交于 2021-01-28 22:09:58
问题 I figured this out while typing my question, but would like to see if there's a cleaner, less code way of doing what I want. e.g. code block: target <- "mpg" # want mtcars %>% mutate(target := log(target)) I'd like to update mpg to be the log of mpg based on the variable target. Looks like I got this working with: mtcars %>% mutate(!! rlang::sym(target) := log(!! rlang::sym(target))) That just reads as pretty repetitive. Is there a 'cleaner', less code way of achieving the same result? I'm

tidy eval with e.g. mtcars %>% mutate(target := log(target))

房东的猫 提交于 2021-01-28 22:06:17
问题 I figured this out while typing my question, but would like to see if there's a cleaner, less code way of doing what I want. e.g. code block: target <- "mpg" # want mtcars %>% mutate(target := log(target)) I'd like to update mpg to be the log of mpg based on the variable target. Looks like I got this working with: mtcars %>% mutate(!! rlang::sym(target) := log(!! rlang::sym(target))) That just reads as pretty repetitive. Is there a 'cleaner', less code way of achieving the same result? I'm

tidy eval with e.g. mtcars %>% mutate(target := log(target))

流过昼夜 提交于 2021-01-28 21:49:54
问题 I figured this out while typing my question, but would like to see if there's a cleaner, less code way of doing what I want. e.g. code block: target <- "mpg" # want mtcars %>% mutate(target := log(target)) I'd like to update mpg to be the log of mpg based on the variable target. Looks like I got this working with: mtcars %>% mutate(!! rlang::sym(target) := log(!! rlang::sym(target))) That just reads as pretty repetitive. Is there a 'cleaner', less code way of achieving the same result? I'm

Quosures in R, how to use the !! operator (tidy-evaluation)

故事扮演 提交于 2021-01-28 19:10:56
问题 I'm trying to understand tidy evaluation in R. grouped_mean <- function(data, group_var, summary_var) { group_var <- enquo(group_var) summary_var <- enquo(summary_var) data %>% group_by(!!group_var) %>% summarise(mean = mean(!!summary_var)) } I understand why and how to use it but not what actually happens I guess. var <- "test" var <- enquo(var) !!var Error in is_quosure(e2) : argument "e2" is missing, with no default This gives me an error while I expected it to work outside dplyr too. Why

R How to Pass a function as a String Inside another Function

落花浮王杯 提交于 2021-01-28 02:57:46
问题 Any assistance on this little conundrum would be mightily appreciated thanks. I am trying to pass an argument to the tq_transmute function from the tidyquant package; the value for the argument is a function, however I would like to pass it as a string (out with the scope of the example below I’ll be passing it via a Shiny selectInput ). I have tried every way I can think of to turn the string 'apply.quarterly' into the object apply.quarterly accepted by the mutate_fun argument. The commented

rlang: Error: Can't convert a function to a string

霸气de小男生 提交于 2021-01-27 10:47:30
问题 I created a function to convert a function name to string. Version 1 func_to_string1 works well, but version 2 func_to_string2 doesn't work. func_to_string1 <- function(fun){ print(rlang::as_string(rlang::enexpr(fun))) } func_to_string2 <- function(fun){ is.function(fun) print(rlang::as_string(rlang::enexpr(fun))) } func_to_string1 works: > func_to_string1(sum) [1] "sum" func_to_string2 doesn't work. > func_to_string2(sum) Error: Can't convert a primitive function to a string Call `rlang:

Using unquoted strings in with `dplyr` verbs: `select` and `arrange` working differently

醉酒当歌 提交于 2021-01-04 05:04:19
问题 I am trying to unquote a string for use in dplyr::arrange . It does not appear to work. However, it appears to work correctly in dplyr::select . Am I missing something or doing something wrong here? library(tidyverse) df <- tibble(x = c(1, 2, 3), y = c(8, 6, 3)) v <- 'y' # `select` works with `!!v` df %>% select(y) #> # A tibble: 3 x 1 #> y #> <dbl> #> 1 8 #> 2 6 #> 3 3 df %>% select(!!v) #> # A tibble: 3 x 1 #> y #> <dbl> #> 1 8 #> 2 6 #> 3 3 # `arrange` not work with `!!v` df %>% arrange(y)

Using unquoted strings in with `dplyr` verbs: `select` and `arrange` working differently

人走茶凉 提交于 2021-01-04 05:00:34
问题 I am trying to unquote a string for use in dplyr::arrange . It does not appear to work. However, it appears to work correctly in dplyr::select . Am I missing something or doing something wrong here? library(tidyverse) df <- tibble(x = c(1, 2, 3), y = c(8, 6, 3)) v <- 'y' # `select` works with `!!v` df %>% select(y) #> # A tibble: 3 x 1 #> y #> <dbl> #> 1 8 #> 2 6 #> 3 3 df %>% select(!!v) #> # A tibble: 3 x 1 #> y #> <dbl> #> 1 8 #> 2 6 #> 3 3 # `arrange` not work with `!!v` df %>% arrange(y)

Using unquoted strings in with `dplyr` verbs: `select` and `arrange` working differently

随声附和 提交于 2021-01-04 04:59:31
问题 I am trying to unquote a string for use in dplyr::arrange . It does not appear to work. However, it appears to work correctly in dplyr::select . Am I missing something or doing something wrong here? library(tidyverse) df <- tibble(x = c(1, 2, 3), y = c(8, 6, 3)) v <- 'y' # `select` works with `!!v` df %>% select(y) #> # A tibble: 3 x 1 #> y #> <dbl> #> 1 8 #> 2 6 #> 3 3 df %>% select(!!v) #> # A tibble: 3 x 1 #> y #> <dbl> #> 1 8 #> 2 6 #> 3 3 # `arrange` not work with `!!v` df %>% arrange(y)

Using unquoted strings in with `dplyr` verbs: `select` and `arrange` working differently

萝らか妹 提交于 2021-01-04 04:59:30
问题 I am trying to unquote a string for use in dplyr::arrange . It does not appear to work. However, it appears to work correctly in dplyr::select . Am I missing something or doing something wrong here? library(tidyverse) df <- tibble(x = c(1, 2, 3), y = c(8, 6, 3)) v <- 'y' # `select` works with `!!v` df %>% select(y) #> # A tibble: 3 x 1 #> y #> <dbl> #> 1 8 #> 2 6 #> 3 3 df %>% select(!!v) #> # A tibble: 3 x 1 #> y #> <dbl> #> 1 8 #> 2 6 #> 3 3 # `arrange` not work with `!!v` df %>% arrange(y)