R - 检查是否为真:缺少需要TRUE / FALSE的值

时间:2017-07-16 16:51:36

标签: r shiny

为什么会出现此错误消息:

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]))

我的生产服务器上的My 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的列名。

1 个答案:

答案 0 :(得分:0)

if条件中的语句可能是NA。你可以使用:

来避免这种情况
if (all(is.na(Df[, columnName]))) {
   # do something
}

我不确定您的具体情况,但您可以尝试一下。