通过学习R
,我刚碰到下面的代码here。
open.account <- function(total) {
list(
deposit = function(amount) {
if(amount <= 0)
stop("Deposits must be positive!\n")
total <<- total + amount
cat(amount, "deposited. Your balance is", total, "\n\n")
},
withdraw = function(amount) {
if(amount > total)
stop("You don't have that much money!\n")
total <<- total - amount
cat(amount, "withdrawn. Your balance is", total, "\n\n")
},
balance = function() {
cat("Your balance is", total, "\n\n")
}
)
}
ross <- open.account(100)
robert <- open.account(200)
ross$withdraw(30)
ross$balance()
robert$balance()
ross$deposit(50)
ross$balance()
ross$withdraw(500)
什么是最我对这个代码的兴趣,学习使用"$"
美元符号的其中提到的具体internal function
在open.account()
功能。我的意思是这部分:
ross$withdraw(30)
ross$balance()
robert$balance()
ross$deposit(50)
ross$balance()
ross$withdraw(500)
问题:
1 - 什么是美元符号"$"
的R
function()
意思?
2-如何确定其功能的属性,特别是您从其他(,即,你没有写它)采用的功能?
我用下面的脚本
> grep("$", open.account())
[1] 1 2 3
,但它是没有用的,我想找到一种方法来提取的,可以通过“$”不只是指通过调用和查询内部函数的名称(S)书面代码为> open.account()
。
例如在open.account()
情况下,我希望看到这样的事情:
$deposit
$withdraw
$balance
3-有没有办法,我可以阅读更多关于它的内容?
tnx!
请参阅帮助文件:'?“$”'。 – lmo