rlang

Accept both bare (from rlang) or string as a function input

你。 提交于 2020-06-12 08:40:26
问题 I editing an existing function in a package. Currently, the function accepts a column name in a data frame as a string. I am updating the function to accept either a string name or a bare name. But I am running into some issues. The general approach I'd like to take is to convert a bare to a string, so the rest of the function does not need to be updated. If the user passes a string column name, then I don't need to modify the input. The code below converts a bare input to a string, but I can

How to programmatically filter columns in dplyr?

天涯浪子 提交于 2020-05-12 04:54:31
问题 How would I create a function that drops NA values in a column if I don't want to specify the column until the function is called? minimal_case <- function(column_name = "a") { enquo_name <- enquo(column_name) example <- tibble(a = c(NA, 1)) print(filter(example, !is.na(a))) print(filter(example, !is.na(rlang::UQ(enquo_name)))) } The output of the first print statement is: # A tibble: 1 x 1 a <dbl> 1 1 The output of the second print statement is: # A tibble: 2 x 1 a <dbl> 1 NA 2 1 How do I

How to pass an expression in a string to a verb in dplyr 0.7.2

牧云@^-^@ 提交于 2020-04-13 07:01:25
问题 I am trying to implement advice I am finding in the web but I am halfway where I want to go. Here is a reproducible example: library(tidyverse) library(dplyr) library(rlang) data(mtcars) filter_expr = "am == 1" mutate_expr = "gear_carb = gear*carb" select_expr = "mpg , cyl" mtcars %>% filter_(filter_expr) %>% mutate_(mutate_expr) %>% select_(select_expr) The filter expression works fine. The mutate expression works as well but the new variable has the name gear_carb = gear*carb instead of the

Creating an R function that can accept both datasets and object names as well as those names as a string object

主宰稳场 提交于 2020-03-23 23:25:42
问题 I can make a function that takes object names as arguments (this is just a normal function). I can also now make a function that get's its data and column arguments via a named vector (utilising dataset <- eval(sym(dataset)) and date_col <- sym(date_col) ). However I would like a function that can handle both types of inputs. Step one is to detect the class of the input. For the dataset argument, this (below) works fine for both the named vector and the actual object name. if (is.character

Creating an R function that can accept both datasets and object names as well as those names as a string object

∥☆過路亽.° 提交于 2020-03-23 23:25:33
问题 I can make a function that takes object names as arguments (this is just a normal function). I can also now make a function that get's its data and column arguments via a named vector (utilising dataset <- eval(sym(dataset)) and date_col <- sym(date_col) ). However I would like a function that can handle both types of inputs. Step one is to detect the class of the input. For the dataset argument, this (below) works fine for both the named vector and the actual object name. if (is.character

Creating an R function that can accept both datasets and object names as well as those names as a string object

青春壹個敷衍的年華 提交于 2020-03-23 23:24:44
问题 I can make a function that takes object names as arguments (this is just a normal function). I can also now make a function that get's its data and column arguments via a named vector (utilising dataset <- eval(sym(dataset)) and date_col <- sym(date_col) ). However I would like a function that can handle both types of inputs. Step one is to detect the class of the input. For the dataset argument, this (below) works fine for both the named vector and the actual object name. if (is.character

Get expression that evaluated to dot in function called by `magrittr`

删除回忆录丶 提交于 2020-03-18 12:43:25
问题 I have a function x_expression() which prints the expression passed to argument x . pacman::p_load(magrittr, rlang) x_expression <- function(x) { print(enquo(x)) } y <- 1 x_expression(y) #> <quosure> #> expr: ^y #> env: global y %>% x_expression() #> <quosure> #> expr: ^. #> env: 0x7ff27c36a610 So you can see that it knows y was passed to it, but when y is piped in with %>% , the function returns prints . . Is there a way to recover the y in the case that it is piped in, or is it gone forever

Get expression that evaluated to dot in function called by `magrittr`

回眸只為那壹抹淺笑 提交于 2020-03-18 12:43:19
问题 I have a function x_expression() which prints the expression passed to argument x . pacman::p_load(magrittr, rlang) x_expression <- function(x) { print(enquo(x)) } y <- 1 x_expression(y) #> <quosure> #> expr: ^y #> env: global y %>% x_expression() #> <quosure> #> expr: ^. #> env: 0x7ff27c36a610 So you can see that it knows y was passed to it, but when y is piped in with %>% , the function returns prints . . Is there a way to recover the y in the case that it is piped in, or is it gone forever

(R) Error when trying to load the SQLDF package

十年热恋 提交于 2020-03-05 00:26:59
问题 I have been having some trouble running the following code: install.packages("sqldf",dep=TRUE) library(sqldf) install.packages("RSQLite",dep=TRUE) library(RSQLite) After running this, my intention is to use the sqldf function to run some queries, but I've gotten stuck on an error I'm not sure how to solve and I've had trouble finding any other answers on Stack Overflow that could be helpful. The following is the error I receive after running the first line: Loading required package: RSQLite

What is the difference between ensym and enquo when programming with dplyr?

筅森魡賤 提交于 2020-02-22 07:37:06
问题 Relatively new to tidy evaluation and while the functions I'm making work, I want to know why different helper functions are used. For example, what is the difference between enquo and ensym ? In the function I made below to capture daily average and moving average they're interchangeable: library(dplyr) library(lubridate) library(rlang) library(zoo) manipulate_for_ma <- function(data, group_var, da_col_name, summary_var, ma_col_name) { group_var <- ensym(group_var) summary_var <- enquo