通过学习R
,我刚刚遇到了下面的代码解释here https://cran.r-project.org/doc/manuals/R-intro.html#Scope.
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
in open.account()
功能。我的意思是这部分:
ross$withdraw(30)
ross$balance()
robert$balance()
ross$deposit(50)
ross$balance()
ross$withdraw(500)
问题:
1- 美元符号的含义是什么"$"
in R
function()
?
2- 如何做identify它在函数中的属性,特别是对于您从其他函数采用的函数(i.e.你没写)?
我使用了以下脚本
> grep("$", open.account())
[1] 1 2 3
但这没有用我想找到一种方法来提取可以通过“$”引用的内部函数的名称,而不仅仅是通过调用和搜索书面代码作为> open.account()
.
例如,如果open.account()
我想看到这样的东西:
$deposit
$withdraw
$balance
3- 有什么参考资料可以让我阅读更多相关内容吗?
tnx!