Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中,将函数中的输入名称保存为注释_R - Fatal编程技术网

在R中,将函数中的输入名称保存为注释

在R中,将函数中的输入名称保存为注释,r,R,我想将函数中的输入/命令保存为函数末尾的注释。请参见下面的示例 Name1 <- c(1, 2, 3) Name2 <- c(4, 5, 6) addition <- function(x, y){ z <- x + y comment(z) <- paste(x, y) z } z <- addition(x=Name1, y=Name2) comment(z) Name1将注释存储为“comment”属性 R已经为“注释”属性提供了以下函数

我想将函数中的输入/命令保存为函数末尾的注释。请参见下面的示例

Name1 <- c(1, 2, 3)
Name2 <- c(4, 5, 6)

addition <- function(x, y){
  z <- x + y
  comment(z) <- paste(x, y)
  z
}
z <- addition(x=Name1, y=Name2)
comment(z)
Name1将注释存储为“comment”属性

R已经为“注释”属性提供了以下函数,但您可以自己定义它们,例如,如果您想以不同的方式命名属性。内置的“comment”属性的优点是
print
不打印它。缺点是其内容仅限于文本

##assignment function
#"comment<-" <- function(x, value) {
#  attr(x, "comment") <- value
#  x
#}
#
##accessor function    
#comment <- function(x) attr(x, "comment")
就个人而言,我更喜欢
注释(z)
addition <- function(x, y){
  z <- x + y
  comment(z) <- paste(substitute(x), substitute(y))
  z
}
z <- addition(x=Name1, y=Name2)
comment(z)
#[1] "Name1 Name2"