Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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_Function_Class_Development Environment_Environment - Fatal编程技术网

使用R的打印环境

使用R的打印环境,r,function,class,development-environment,environment,R,Function,Class,Development Environment,Environment,我想写一个函数,它的输出是myclass类的一个对象,带有向量、列表、整数等等。类似于LMF函数。我尝试使用环境,但当我打印函数值时,结果是 #Term 1 > fit1 <environment: 0x00000000220d1998> attr(,"class") [1] "myclass" 我知道使用$访问环境的各个值。但我希望打印的对象等于所示的lm函数。这就是您想要的吗 variable1 <- rnorm(10) variable2 <- rnorm(

我想写一个函数,它的输出是myclass类的一个对象,带有向量、列表、整数等等。类似于LMF函数。我尝试使用环境,但当我打印函数值时,结果是

#Term 1
> fit1
<environment: 0x00000000220d1998>
attr(,"class")
[1] "myclass"
我知道使用$访问环境的各个值。但我希望打印的对象等于所示的lm函数。

这就是您想要的吗

variable1 <- rnorm(10)
variable2 <- rnorm(10)
fit1 <- lm(variable1~variable2)
fit2 <- fit1
class(fit2) <- "myclass"

# have a look at stats:::print.lm
# and copy that function, hence define it as print method for your class or edit further:
print.myclass <- function (x, digits = max(3L, getOption("digits") - 3L), ...) {
  cat("\nCall:\n", paste(deparse(x$call), sep = "\n", collapse = "\n"), 
      "\n\n", sep = "")
  if (length(coef(x))) {
    cat("Coefficients:\n")
    print.default(format(coef(x), digits = digits), print.gap = 2L, 
                  quote = FALSE)
  }
  else cat("No coefficients\n")
  cat("\n")
  invisible(x)
}

# now print
print(fit2)

# or
fit2
variable1 <- rnorm(10)
variable2 <- rnorm(10)
fit1 <- lm(variable1~variable2)
fit2 <- fit1
class(fit2) <- "myclass"

# have a look at stats:::print.lm
# and copy that function, hence define it as print method for your class or edit further:
print.myclass <- function (x, digits = max(3L, getOption("digits") - 3L), ...) {
  cat("\nCall:\n", paste(deparse(x$call), sep = "\n", collapse = "\n"), 
      "\n\n", sep = "")
  if (length(coef(x))) {
    cat("Coefficients:\n")
    print.default(format(coef(x), digits = digits), print.gap = 2L, 
                  quote = FALSE)
  }
  else cat("No coefficients\n")
  cat("\n")
  invisible(x)
}

# now print
print(fit2)

# or
fit2