作者andrew43 (apan)
看板R_Language
标题Re: [问题] 函数的output排版,与一些相关问题
时间Mon Dec 23 14:43:26 2013
不确定是不是你要的,但你试试看:
square <- function(x) {
input <- x
output <- x^2
y <- list(input=input, output=output)
class(y) <- "square"
return(y)
}
print.square <- function(w){
cat("calling print.square...\n")
cat("The square of (", w$input, ") equal to", w$output)
}
之後可以得到
tmp <- square(3) # 不会显示
tmp # 会显示以下二列
calling print.square...
The square of ( 3 ) equal to 9
※ 引述《coo20819 (Mike)》之铭言:
: 在此先非常感谢W大热心的回答,谢谢!
: 我也对 'lm' 这个函数有更深的了解,
: 但是我尝试一下还是无法用出我要的结果...
: 我表达能力不是很好,再把问题换种方式表达,希望您能理解
: 目前:
: ```r
: square <- function(x) {
: cat(" It is a test.", "\n")
: cat(" The square of (", x, ") equal to", x^2)
: }
: temp <- square(3)
: ```
: ```
: ## It is a test.
: ## The square of ( 3 ) equal to 9
: ```
: ```r
: temp
: ```
: ```
: ## NULL
: ```
: 我希望藉由'其他函数',可以像'cat'一样漂亮排版,但又能使用`invisible`来
: 让R放弃自动呼叫,变成以下:
: ```r
: temp <- square(3)
: temp
: ```
: ```
: ## It is a test.
: ## The square of ( 3 ) equal to 9
: ```
: 希望您能理解,非常感谢您!
--
http://apansharing.blogspot.com/
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.128.117.75
※ 编辑: andrew43 来自: 140.128.117.75 (12/23 14:44)
1F:→ coo20819:是!非常感谢您的范例!!!谢谢 12/23 16:59