Re: [問題] 函數的output排版,與一些相關問題

作者: andrew43 (討厭有好心推文後刪文者)   2013-12-23 14:43:26
不確定是不是你要的,但你試試看:
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
: ```
: 希望您能理解,非常感謝您!
作者: coo20819 (小李)   2013-02-23 16:59:00
是!非常感謝您的範例!!!謝謝

Links booklink

Contact Us: admin [ a t ] ucptt.com