小编Kat*_*ana的帖子

关于R中的词汇范围

我是R的新手,当我阅读手册时,我遇到了一个关于词法范围的段落以及这个代码示例:

 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) …
Run Code Online (Sandbox Code Playgroud)

variables scope r lexical lexical-scope

5
推荐指数
1
解决办法
887
查看次数

标签 统计

lexical ×1

lexical-scope ×1

r ×1

scope ×1

variables ×1