non-standard-evaluation

R user-defined/dynamic summary function within dplyr::summarise

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-11 12:14:29
问题 Somewhat hard to define this question without sounding like lots of similar questions! I have a function for which I want one of the parameters to be a function name, that will be passed to dplyr::summarise, e.g. "mean" or "sum": data(mtcars) f <- function(x = mtcars, groupcol = "cyl", zCol = "disp", zFun = "mean") { zColquo = quo_name(zCol) cellSummaries <- x %>% group_by(gear, !!sym(groupcol)) %>% # 1 preset grouper, 1 user-defined summarise(Count = n(), # 1 preset summary, 1 user defined !

Passing column name into function

 ̄綄美尐妖づ 提交于 2021-02-04 21:06:56
问题 I have a simple problem with non-standard evaluation: passing a variable name as an argument into a function. As a reproducible example, here's a simple thing: taking the mean of one variable, mpg from the mtcars dataset. My end goal is to have a function where I can input the dataset and the variable, and get the mean. So without a function: library(tidyverse) mtcars %>% summarise(mean = mean(mpg)) #> mean #> 1 20.09062 I've tried to use get() for non-standard evaluation, but I'm getting

Passing column name into function

两盒软妹~` 提交于 2021-02-04 21:05:59
问题 I have a simple problem with non-standard evaluation: passing a variable name as an argument into a function. As a reproducible example, here's a simple thing: taking the mean of one variable, mpg from the mtcars dataset. My end goal is to have a function where I can input the dataset and the variable, and get the mean. So without a function: library(tidyverse) mtcars %>% summarise(mean = mean(mpg)) #> mean #> 1 20.09062 I've tried to use get() for non-standard evaluation, but I'm getting

R - Checking if a string is a valid mathematical expression using non-standard evaluation

爱⌒轻易说出口 提交于 2021-01-28 12:16:02
问题 I would like to check if the strings below are valid mathematical expressions: s1 = 'sin(x)' s2 = 'sin(x*m)' s3 = 'sin' s4 = 'sin(xm)' By 'valid', I mean the expression is a combination of operators (must be used in conjunction with variables or constants) variables x and/or m constants. By this definition s1 and s2 are valid while s3 and s4 are not. To identify if a string is valid, I wrote a function checkFxn that first attempts to convert the string into a call or one of its parts. If

deparse(substitute()) returns function name normally, but function code when called inside for loop

你。 提交于 2021-01-26 20:38:52
问题 I'm a bit surprised by R's behaviour in a very specific case. Let's say I define a function square that returns the square of its argument, like this: square <- function(x) { return(x^2) } I want to call this function within another function, and I also want to display its name when I do that. I can do that using deparse(substitute()) . However, consider the following examples: ds1 <- function(x) { print(deparse(substitute(x))) } ds1(square) # [1] "square" This is the expected output, so all

curly curly Tidy evaluation and modifying inputs or their names

核能气质少年 提交于 2020-08-22 14:34:42
问题 The new curly curly method of tidy evaluation is explained in this article. Several examples are given demonstrating the use of this style of non-standard evaluation (NSE). library(tidyverse) # Example 1 -------------------------- max_by <- function(data, var, by) { data %>% group_by({{ by }}) %>% summarise(maximum = max({{ var }}, na.rm = TRUE)) } starwars %>% max_by(height) starwars %>% max_by(height, by = gender) # Example 2 -------------------------- summarise_by <- function(data, ..., by

How to pass “everything possible” to by in a function?

大兔子大兔子 提交于 2020-02-24 01:13:22
问题 I am trying to use data.table within a user facing function in a package I'm working on. I would like this function to behave as data.table -like as possible. This means for example that my function also features a by argument, which is passed to the underlying data.table call within the function. The user should be free to pass anything into "my" by which is possible directly in a data.table . Citing from ?data.table this includes: A single unquoted column name: e.g., DT[, .(sa=sum(a)), by=x

NSE challenge: break out of deparse(substitute(…))

房东的猫 提交于 2020-01-25 07:47:18
问题 Let's define: f <- function(x) deparse(substitute(x)) The challenge: find <something> so that f(<something>) returns "abc" . Excluding, of course, f(abc) . With "tidy NSE", i.e. quasiquoting, this is very easy. However, according to the NSE references (1, 2, 3), it is impossible since substitute is a pure quoting (as opposed to quasiquoting) function. I wonder if there is anything obscure or undocumented (not that uncommon!) that allows to unquote in substitute , hence the challenge. 回答1:

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

不想你离开。 提交于 2019-12-29 00: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

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

亡梦爱人 提交于 2019-12-29 00:41:06
问题 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