【发布时间】:2017-07-17 02:34:02
【问题描述】:
为什么我会收到此错误消息:
missing value where TRUE/FALSE needed
我想检查它是否是真的然后做点什么。这在我的 RStudio 上工作正常,但是当代码在生产服务器上时出现上述错误:
na <- all(is.na(Df[, columnName]))
if (isTRUE(na)) {
# do something
}
对于:
if (na == TRUE) {
知道为什么吗??
编辑:
似乎是all(is.na(Df[, columnName])) 导致了这个错误。
我的生产服务器上的 Shiny 使用的是旧版本 - 0.12.1。当我的本地机器在 1.0.3. 时,如何验证是否是 Shiny 版本的问题?
以下是我的数据样本。
此数据没有错误:
structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = "LW2", class = "factor"), code = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "LW2", class = "factor"),
date = structure(c(1493251200, 1493254800, 1493258400, 1493262000,
1493265600, 1493269200, 1493272800, 1493276400, 1493280000,
1493283600, 1493287200, 1493290800, 1493294400, 1493298000,
1493301600, 1493305200, 1493308800, 1493312400, 1493316000,
1493319600, 1493323200, 1493326800, 1493330400, 1493334000
), class = c("POSIXct", "POSIXt"), tzone = "GMT"), PM25 = c(4.4,
4.3, 5.3, 2.1, 4.3, 16.1, 21.1, 22.7, 27.8, 22.2, 11.3, 13.4,
14.2, 13.8, 15.9, 17.6, 18.5, 24.7, 23, 31.6, 18.9, 23, 11.7,
11.7)), row.names = c(NA, -24L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), .Names = c("site", "code", "date",
"PM25"), vars = list(site, code), drop = TRUE, indices = list(
0:23), group_sizes = 24L, biggest_group_size = 24L, labels = structure(list(
site = structure(1L, .Label = "LW2", class = "factor"), code = structure(1L, .Label = "LW2", class = "factor")), row.names = c(NA,
-1L), class = "data.frame", vars = list(site, code), drop = TRUE, .Names = c("site",
"code")))
这个数据给出了上面的错误:
structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = "HK6", class = "factor"), code = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "HK6", class = "factor"),
date = structure(c(1493251200, 1493254800, 1493258400, 1493262000,
1493265600, 1493269200, 1493272800, 1493276400, 1493280000,
1493283600, 1493287200, 1493290800, 1493294400, 1493298000,
1493301600, 1493305200, 1493308800, 1493312400, 1493316000,
1493319600, 1493323200, 1493326800, 1493330400, 1493334000
), class = c("POSIXct", "POSIXt"), tzone = "GMT"), PM25 = c(NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN)), row.names = c(NA,
-24L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), .Names = c("site",
"code", "date", "PM25"), vars = list(site, code), drop = TRUE, indices = list(
0:23), group_sizes = 24L, biggest_group_size = 24L, labels = structure(list(
site = structure(1L, .Label = "HK6", class = "factor"), code = structure(1L, .Label = "HK6", class = "factor")), row.names = c(NA,
-1L), class = "data.frame", vars = list(site, code), drop = TRUE, .Names = c("site",
"code")))
我正在检查PM25的列名。
【问题讨论】:
-
请提供一个可重现的例子。当
if语句中的条件为NA时,通常会出现该错误。如果你尝试if (NA) print(4),你会看到同样的错误。 -
请注意,
if(na == TRUE)完全等同于if(na)。 -
@RuiBarradas 谢谢。我也试过了,同样的错误。
-
@nicola 我添加了数据样本。请参阅我上面的编辑。谢谢。