non-standard-evaluation

What I'm doing wrong here when trying to convert an nlmrt object to an nls object

吃可爱长大的小学妹 提交于 2019-12-24 07:48:22
问题 I am trying to convert an "nlmrt object to an "nls" object using nls2 . However, I can only manage to do it if I write explicitly the names of the parameters in the call. Can't I define the parameter names programmatically? See the reproducible example: library(nlmrt) scale_vector <- function(vector, ranges_in, ranges_out){ t <- (vector - ranges_in[1, ])/(ranges_in[2, ]-ranges_in[1, ]) vector <- (1-t) * ranges_out[1, ] + t * ranges_out[2, ] } shobbs.res <- function(x) { # UNSCALED Hobbs weeds

R passing RSelenium driver environment as function argument

狂风中的少年 提交于 2019-12-24 01:18:02
问题 i'm probably not seeing something obvious, anyway i'd like to create functions to automatically extract text from an url already handled by a remote driver. I'd like to pass as a function arguments the xpath expression and the environment into which the remote driver could be found library(RSelenium) url="http://stackoverflow.com/search?q=r+program" remdir<-remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "firefox") remdir$open() remdir$navigate(url) env<-environment()

R dplyr - get variable name as string in mutate/summarise

心不动则不痛 提交于 2019-12-11 04:55:11
问题 I have been trying unsuccessfully to extract the name of a variable that was passed to a function in dplyr::mutate(). Below is a short example where I want to create a function that returns the string "mpg" inside mutate: # mtcars dataset with grouping variable dataset = mtcars dataset$group = c(1, 2, 3, 4) # function to call inside mutate() f = function(col, data){ str_col = deparse(lazyeval::expr_find(col)) str_col } # this does not work: returns the content of mpg # instead of the variable

Get the argument names of an R function

天大地大妈咪最大 提交于 2019-12-04 16:47:37
问题 For an arbitrary function f <- function(x, y = 3){ z <- x + y z^2 } I want to be able take the argument names of f > argument_names(f) [1] "x" "y" Is this possible? 回答1: formalArgs and formals are two functions that would be useful in this case. If you just want the parameter names then formalArgs will be more useful as it just gives the names and ignores any defaults. formals gives a list as the output and provides the parameter name as the name of the element in the list and the default as

Why does `substitute` work in multiple lines, but not in a single line?

南楼画角 提交于 2019-12-04 04:07:46
I was attempting to answer this nice question about creating a non-standard evaluating function for a data.table object, doing a grouped sum. Akrun came up with a lovely answer which I'll simplify here: akrun <- function(data, var, group){ var <- substitute(var) group <- substitute(group) data[, sum(eval(var)), by = group] } library(data.table) mt = as.data.table(mtcars) akrun(mt, cyl, mpg) # group V1 # 1: 6 138.2 # 2: 4 293.3 # 3: 8 211.4 I was also working on an answer, and had close to the same answer, but with the substitute s inline with the rest. Mine results in an error: gregor =

Get the argument names of an R function

半世苍凉 提交于 2019-12-03 11:50:51
For an arbitrary function f <- function(x, y = 3){ z <- x + y z^2 } I want to be able take the argument names of f > argument_names(f) [1] "x" "y" Is this possible? formalArgs and formals are two functions that would be useful in this case. If you just want the parameter names then formalArgs will be more useful as it just gives the names and ignores any defaults. formals gives a list as the output and provides the parameter name as the name of the element in the list and the default as the value of the element. f <- function(x, y = 3){ z <- x + y z^2 } > formalArgs(f) [1] "x" "y" > formals(f)

Dynamic select expression in function [duplicate]

我的未来我决定 提交于 2019-12-02 06:54:57
This question already has an answer here: Reshaping multiple sets of measurement columns (wide format) into single columns (long format) 7 answers I am trying to write a function that will convert this data frame library(dplyr) library(rlang) library(purrr) df <- data.frame(obj=c(1,1,2,2,3,3,3,4,4,4), S1=rep(c("a","b"),length.out=10),PR1=rep(c(3,7),length.out=10), S2=rep(c("c","d"),length.out=10),PR2=rep(c(7,3),length.out=10)) obj S1 PR1 S2 PR2 1 1 a 3 c 7 2 1 b 7 d 3 3 2 a 3 c 7 4 2 b 7 d 3 5 3 a 3 c 7 6 3 b 7 d 3 7 3 a 3 c 7 8 4 b 7 d 3 9 4 a 3 c 7 10 4 b 7 d 3 In to this data frame df %>%

Dynamic select expression in function [duplicate]

偶尔善良 提交于 2019-12-02 06:40:57
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed last year . I am trying to write a function that will convert this data frame library(dplyr) library(rlang) library(purrr) df <- data.frame(obj=c(1,1,2,2,3,3,3,4,4,4), S1=rep(c("a","b"),length.out=10),PR1=rep(c(3,7),length.out=10), S2=rep(c("c","d"),length.out=10),PR2=rep(c(7,3),length.out=10)) obj S1 PR1 S2 PR2 1 1 a 3 c 7 2 1 b 7 d 3 3 2

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")

Scoping of variables in aes(…) inside a function in ggplot

大憨熊 提交于 2019-11-27 23:11:21
Consider this use of ggplot(...) inside a function. x <- seq(1,10,by=0.1) df <- data.frame(x,y1=x, y2=cos(2*x)/(1+x)) library(ggplot2) gg.fun <- function(){ i=2 plot(ggplot(df,aes(x=x,y=df[,i]))+geom_line()) } if(exists("i")) remove(i) gg.fun() # Error in `[.data.frame`(df, , i) : object 'i' not found i=3 gg.fun() # plots df[,3] vs. x It looks like ggplot does not recognize the variable i defined inside the function, but does recognize i if it is defined in the global environment. Why is that? Note that this gives the expected result. gg.new <- function(){ i=2 plot(ggplot(data.frame(x=df$x,y