I was wondering if there is an elegant way of calling "self" in a R function. An easy example would be the modification of a date, let's say a is a date in a int format (like when you read from excel).
我想知道在R函数中是否有一种优雅的方式来调用"self"。一个简单的例子是修改日期,假设a是int格式的日期(就像从excel中读取一样)。
a = 41557
a = as.Date(a, origin = "1899-12-30")
Then "a" is updated with the proper format. Obviously this example is very simple, but in a context of long variable or more complicated procedure one would like to use something like "self". Does something like this exist in R. Self simply meaning take the variable at the left part of the = sign.
然后用适当的格式更新“a”。显然,这个例子非常简单,但是在长变量或更复杂的过程中,我们希望使用“self”之类的东西。像这样的东西在r。Self中存在仅仅意味着取=符号左边的变量。
a = 41557
a = as.Date(self, origin = "1899-12-30") # what to use for self.
As a first hint I found out (I think) that some functions are able to call "self" somehow using the "<-" operator for example:
作为第一个提示,我发现(我认为)有些函数可以使用“<-”运算符来调用“self”,例如:
"minc<-" <- function(x, value){x*value}
Gives :
给:
a = 2
a = minc(12)
# a = 24, which is basically : a = self*12
I don't know if such a keyword exist in R, but it would definitely helps in the readability of most of my codes.
我不知道在R中是否存在这样的关键字,但它肯定有助于我的大部分代码的可读性。
As always, thanks for your help !
一如既往,谢谢你的帮助!
Romain.
罗曼。
2 个解决方案
#1
2
The functionality you're looking for is implemented in the fantastic magrittr
package. The version on CRAN introduces a piping operator, %>%
, which passes what precedes is as the first argument of what follows it (by default), or replaces a .
with the preceding statement.
您正在寻找的功能在奇妙的magrittr包中实现。CRAN上的版本引入了管道操作符%>%,它传递前面的作为后面的第一个参数(默认情况下),或者替换a。与前面的声明。
More to the point of your question, the version on Github introduces many piping variants, including %<>%
which works just like the regular pipe, but includes an overwrite assignment.
更重要的是,Github上的版本引入了许多管道变体,包括%<>%,与常规管道一样工作,但是包含覆盖分配。
The following statements are equivalent (with magrittr
version >= 1.1.0, as available on Github, devtools::install_github("smbache/magrittr")
):
以下语句是等价的(使用magrittr版本>= 1.1.0,可以在Github上找到,devtools::install_github(“smbache/magrittr”):
a = as.Date(a, origin = "1899-12-30")
a = a %>% as.Date(origin = "1899-12-30")
a %<>% as.Date(., origin = "1899-12-30")
a %<>% as.Date(origin = "1899-12-30")
#2
1
Replacement functions can be used like this:
替换功能可以这样使用:
1) as.Date
1)as.Date
"as.Date<-" <- function(x, value) as.Date(x, origin = value)
Now test it:
现在测试它:
a <- 41557
as.Date(a) <- "1899-12-30"
a
## [1] 2013-10-10
2) minc
2)minc
"minc<-" <- function(x, value) x * value
Now test it:
现在测试它:
a <- 2
minc(a) <- 12
a
## [1] 24
Note: You can use self
in place of x
if you like:
注意:如果你喜欢,你可以用self代替x:
"as.Date<-" <- function(self, value) as.Date(self, origin = value)
"minc" <- function(self, value) self * value
#1
2
The functionality you're looking for is implemented in the fantastic magrittr
package. The version on CRAN introduces a piping operator, %>%
, which passes what precedes is as the first argument of what follows it (by default), or replaces a .
with the preceding statement.
您正在寻找的功能在奇妙的magrittr包中实现。CRAN上的版本引入了管道操作符%>%,它传递前面的作为后面的第一个参数(默认情况下),或者替换a。与前面的声明。
More to the point of your question, the version on Github introduces many piping variants, including %<>%
which works just like the regular pipe, but includes an overwrite assignment.
更重要的是,Github上的版本引入了许多管道变体,包括%<>%,与常规管道一样工作,但是包含覆盖分配。
The following statements are equivalent (with magrittr
version >= 1.1.0, as available on Github, devtools::install_github("smbache/magrittr")
):
以下语句是等价的(使用magrittr版本>= 1.1.0,可以在Github上找到,devtools::install_github(“smbache/magrittr”):
a = as.Date(a, origin = "1899-12-30")
a = a %>% as.Date(origin = "1899-12-30")
a %<>% as.Date(., origin = "1899-12-30")
a %<>% as.Date(origin = "1899-12-30")
#2
1
Replacement functions can be used like this:
替换功能可以这样使用:
1) as.Date
1)as.Date
"as.Date<-" <- function(x, value) as.Date(x, origin = value)
Now test it:
现在测试它:
a <- 41557
as.Date(a) <- "1899-12-30"
a
## [1] 2013-10-10
2) minc
2)minc
"minc<-" <- function(x, value) x * value
Now test it:
现在测试它:
a <- 2
minc(a) <- 12
a
## [1] 24
Note: You can use self
in place of x
if you like:
注意:如果你喜欢,你可以用self代替x:
"as.Date<-" <- function(self, value) as.Date(self, origin = value)
"minc" <- function(self, value) self * value