我创建了一个函数,用户整齐地询问用户是否要输入3个变量中的每一个,如果是,则每个变量的值应该是多少。我希望函数输出两个向量的列表:输入变量的向量,以及输入值的向量。
gray = (r == b) & (b == g)
print np.all(gray)
然而,我收到错误:
Input <- function() {
answer <- readline(prompt = "Enter VIEW_ID? y/n ")
if(answer == "y") { VIEW_ID <- readline(prompt = "Enter value: ") }
answer <- readline(prompt = "Enter ACTION_TYPE? y/n ")
if(answer == "y") { ACTION_TYPE <- readline(prompt = "Enter value: ") }
answer <- readline(prompt = "Enter CLIENT_ID? y/n ")
if(answer == "y") { CLIENT_ID <- readline(prompt = "Enter value: ") }
rm(answer)
VarsValues <- list(ls(), sapply(ls(), get))
VarsValues
}
我不明白为什么这不起作用。当我在全局环境中尝试它时,> Input()
Enter VIEW_ID? y/n y
Enter value: a
Enter ACTION_TYPE? y/n y
Enter value: b
Enter CLIENT_ID? y/n y
Enter value: c
Error in FUN(X[[i]], ...) : object 'ACTION_TYPE' not found
工作正常,打印每个变量的值。
答案 0 :(得分:1)
您需要为get
指定环境。这应该有效:
Input <- function() {
answer <- readline(prompt = "Enter VIEW_ID? y/n ")
if(answer == "y") { VIEW_ID <- readline(prompt = "Enter value: ") }
answer <- readline(prompt = "Enter ACTION_TYPE? y/n ")
if(answer == "y") { ACTION_TYPE <- readline(prompt = "Enter value: ") }
answer <- readline(prompt = "Enter CLIENT_ID? y/n ")
if(answer == "y") { CLIENT_ID <- readline(prompt = "Enter value: ") }
rm(answer)
VarsValues <- list(ls(), sapply(ls(), get, envir=environment()))
VarsValues
}