【发布时间】:2017-07-15 05:29:05
【问题描述】:
我遇到了一个非常有趣的问题。我写了一个函数,想检查函数内一些变量的输出以及返回结果。
observe({
result <- myFunction()
})
myFunction <- function() {
# some calculations
# ...
# create Dataframe from previous calculated variables
# I was interested in the result of problematicVariable
# thats why I wanted to make it global for checking, after
# closing down the shiny app
problematicVariable <<- data.frame(...)
if(someCondition) {
# ...
} else {
# some calculations
# ...
# now I used problematicVariable for the first time
foo <- data.frame(problematicVariable$bar, problematicVariable$foo)
}
这给了我
data.frame:参数暗示不同的行数:...
但是,由于我将problematicVariable 设为全局,因此我运行了应用程序手动崩溃的行 (foo <- data.frame(problematicVariable$bar, problematicVariable$foo))。绝对没有问题。所以我想,这很奇怪......
我摆脱了双重<< 并将其更改为problematicVariable <- ...,现在它可以工作了。
因此,使用<<- 分配problematicVariable 以某种方式使problematicVariable 在if...else 中不可用。
为什么会导致<<- 出现这样的行为?这与范围混淆了?!
【问题讨论】:
-
这不是 C,正确定义一个函数及其输入和输出。如果问题仍然存在,请提供一个可重现的小示例。