How to operate non-standard-evaluation in correct manner for summarize{dplyr} - r

I want to pass variables to 'summarize' by way of non-standard-evaluation approach (see http://adv-r.had.co.nz/Computing-on-the-language.html#capturing-expressions).
My script is as follows:
library(dplyr)
library(pryr)
x2<-data.frame(x=runif(1000,1,10),y=rnorm(1:1000))
y2<-group_by(x2,x)
field2<-"x"
z<-substitute(summarize(y2,check=sum(x)),list(x=as.name(field2)))
eval(quote(z),parent.frame())
But the output is not a dataframe as I supposed but a string:
>eval(quote(z),parent.frame())
summarize(y2, check = sum(x))
I am a little bit confused with non-standard-evaluation although I have looked through a number of examples.
Could you specify what is wrong with my approach?

Related

R - Access an object own's name in apply functions

This is a problem I often encounters: I try to access an object's own name when using a function from apply family and spend hours figuring out how to do it... For instance (this is not the core of my question), today I was willing to inspect an attached package trying to figure out if it contained some non function objects. After a lot of tries and fails, I finally came up with (for the rrapply package - I know looking at the documentation is also easy but this one illustrates well the problem):
library(rrapply)
eapply(rlang::pkg_env('rrapply'), function(x) {if(!is.function(x)) x}) %>%
`[`(sapply(., function(x) !is.null(x))) %>%
names()
## [1] "renewable_energy_by_country" "pokedex"
I feel that is really too complicated for a simple test !
So my question: is there an easy way to loop through an object in base R (or maybe tidyverse) and return only the names of those elements that correspond to a certain condition ? rrapply seems to be able to achieve that but:
it is fairly complicated
and it seems to work on lists only and to loop through all sub-elements as well which is not desired
Thanks !
Identify the environment of interest, e, and then use eapply with the indicated function taking the names of the extracted elements at the end. This isn't conceptually different from the code in the question but does seem somewhat less complex when done in base R in the following way:
e <- as.environment("package:rrapply")
names(Filter(`!`, eapply(e, is.function)))
or the same code written as a pipeline:
library(magrittr)
"package:rrapply" %>%
as.environment %>%
eapply(is.function) %>%
Filter(`!`, .) %>%
names

R statistics programming : using magrittr piping to pass 2 parameters to function

I am using magrittr, and was able to pass one variable to an R function via pipes from magrittr, and also pick which parameter to place where in the situation of multivariable function : F(x,y,z,...)
But i want to pass 2 parameters at the same time.
For example, i will using Select function from dplyr and pass in tableName and ColumnName:
I thought i could do it like this:
tableName %>% ColumnName %>% select(.,.)
But this did not work.
Hope someone can help me on this.
EDIT :
Some below are saying that this is a duplicate of a link provided by others.
But based on the algebra structure of the magrittr definition of Pipe for multivariable functions, it should be "doable" just based on the algebra definition of the pipe function.
The link provided by others, goes beyond the base definition and employs other external functions and or libraries to try to achieve passing multiple parameter to the function.
I am looking for a solution, IF POSSIBLE, just using the magrittr library and other base operations.
So this is the restriction that is placed on this problem.
In most of my university courses in math and computer science we were restricted to use only those things taught in the course. So when I said I am using dplyr and magrittr, that should imply that those are the only things one is permitted to use, so its under this constraint.
Hope this clarifies the scope of possible solutions here.
And if it's not possible to do this via just these libraries I want someone to tell me that it cannot be done.
I think you need a little more detail about exactly what you want, but as I understand the problem, I think one solution might be:
list(x = tableName, y = "ColumnName") %>% {select(eval(.$x),.$y) }
This is just a modification of the code linked in the chat. The issue with other implementations is that the first and second inputs to select() must be of specific (and different) types. So just plugging in two strings or two objects won't work.
In the same spirit, you can also use either:
list(x = "tableName", y = "ColumnName") %>% { select(get(.$x),.$y) }
or
list(tableName, "ColumnName") %>% do.call("select", .).
Note, however, that all of these functions (i.e., get(), eval(), and do.call()) have an environment specification in them and could result in errors if improperly specified. They work just fine in these examples because everything is happening in the global environment, but that might change if they were, e.g., called in a function.

How to have functions chaining in R like in c# with linq we have method chaining?

I am a new-bee to R one thing I noticed in R that we need to keep on saving the result to the variable each time before further processing is required. Is there some way where I can store the result to some buffer and later on use this buffer result in further processing.
For people who are familiar with c# using LINQ we have a feature called Method Chaining, here we keep on passing the intermediate result to various functions on the fly without the need of storing them into separate variables and in the end, we get the required output.This saves lots of extra syntax, so is there something like this in R?
Function composition is to functional programming as method chaining is to object-oriented programming.
x <- foo(bar(baz(y)))
is basically the same as
x = baz(y).bar().foo()
in the languages you might be familiar with.
If you're uncomfortable with nested parens and writing things backwards, the magrittr package provides the %>% operator to unpack expressions:
library(magrittr)
x = y %>% baz() %>% bar() %>% foo()
R also provides a couple of frameworks for conventional OO programming: reference classes and R6. With those, you can write something like
x = y$baz()$bar()$foo()
but I'd suggest learning how to deal with "normal" R expressions first.
In R we have something called Pipes(%>%) through which one can send the output of one function to another, i.e output from one function becomes input for subsequent function in the chain.
Try something like in this in R console Consider a tibble MyData containing Username and pwd as two columns u can use pipes as:
MyData %>%
select(username,pwd)
%>%
filter(!is.na(username))%>%
arrange(username).
This will print all the usernames and pwd sorted by username that contains non NA's value
Hope that helps

With vs. lapply - why does with not work here?

I am trying to learn R and can't really figure out when to use with appropriately. I was thinking about this example:
The goal is to convert "dstr" and "died" in the whole dataframe "stroke" (in the ISwR database) to date format in several ways (just for practice). I've managed to do it like this:
#applying a function to the whole data frame - use the fact that data frames are lists actually
rawstroke=read.csv2(system.file("rawdata","stroke.csv",package="ISwR"),na.strings=".")
names(rawstroke)=tolower(names(rawstroke))
ix=c("dstr","died")
rawstroke[ix]=lapply(rawstroke[ix],as.Date,format="%d.%m.%Y")
head(rawstroke)
However, when I try using with function it does not give data frame as output, but only writes the definition of the function myfun. Here is the code I tried.
myfun=function(x)
{y=as.Date(x,format="%d.%m.%Y")
return(y)}
rawstroke=read.csv2(system.file("rawdata","stroke.csv",package="ISwR"),na.strings=".")
names(rawstroke)=tolower(names(rawstroke))
ix=c("dstr","died")
bla=with(rawstroke[ix],myfun)
head(bla)
If somebody could help me with this, it would be great.
Yeah, this doesn't seem like a job for with. To use your function here, you'd just replace as.Date in your first code with myfun and remove the format parameter, like
rawstroke[ix]=lapply(rawstroke[ix], myfun)
with is used to more cleanly access variables in data frames and environments. For example, instead of
t.test(dat$x, dat$y)
you could do
with(dat, t.test(x, y))

R - Operate on a column w/o explicitely reassigning it?

I'm often writing things like:
dataframe$this_column <- as.Date(dataframe$this_column)
That is, when changing some column in my data frame [table], I'm constantly writing the column twice. Is there some function that allows me to directly change the data frame w/o explicitly reassigning it? Say: ch(dataframe$this_column, as.Date())
EDIT: While similar, the potential duplicate is not the same. I am not looking for a way to shorten self-referential reassignments. I'm looking to avoid the explicit reassignment all together. The answer I accepted here is an appropriate solution (and much better than the answers provided in the "duplicate" question, in regards to their relevance to my question).
Here is the example using magrittr package:
library(magrittr)
x = c('2015-12-12','2015-12-13','2015-12-14')
df = data.frame(x)
df$x %<>% as.Date

Resources