“无法解决”;警告:因子“官衔”包含隐含NA,考虑使用“福克斯::FCTA ExpReCusithNA”。

“无法解决”;警告:因子“官衔”包含隐含NA,考虑使用“福克斯::FCTA ExpReCusithNA”。,r,shiny,forcats,R,Shiny,Forcats,我尝试了以下方法。为什么我仍然收到猫的警告 #1我执行 plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle) if (nrow(plotdata) > 0){ str <- paste("nrows > 0. Number of rows is ", nrow(plotdata)) print(str) print(plotdata) 结果如下: [1]

我尝试了以下方法。为什么我仍然收到猫的警告

#1我执行

plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)

  if (nrow(plotdata) > 0){
    str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
    print(str)
    print(plotdata)
结果如下:

[1] "nrows > 0. Number of rows is  1"
# A tibble: 1 x 2
  officialTitle     n
  <fct>         <int>
1 NA                0
Warning message:
Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`
[1] "nrows < 0. Number of rows is  0"
# A tibble: 0 x 2
# ... with 2 variables: officialTitle <fct>, n <int>
Warning message:
Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`
[1]“nrows<0。行数为0”
#一个tibble:0x2
# ... 有2个变量:officialTitle,n
警告信息:
因子“官方标题”包含隐式NA,考虑使用“FoCeS::FCTXEXPLICANTIONNA”
完整代码:

# Load packages ----
library(shiny)
library(ggplot2)
library(dplyr)
library(scales)
library(treemapify)
library(forcats)

# Load data ----
data(Marriage, package="mosaicData")

plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
#plotdata$officialTitle <- fct_explicit_na(plotdata$officialTitle)
plotdata <- na.omit(plotdata)

  if (nrow(plotdata) > 0){
    str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
    print(str)
    print(plotdata)
    ggplot(plotdata, 
           aes(fill = officialTitle, 
               area = n,
               label = officialTitle)) +
      geom_treemap() + 
      geom_treemap_text(colour = "white", 
                        place = "centre") +
      labs(title = "Marriages by officiate") +
      theme(legend.position = "none")
  } else {
    str <- paste("nrows < 0. Number of rows is ", nrow(plotdata))
    print(str)
    print(plotdata)
  }
#加载包----
图书馆(闪亮)
图书馆(GG2)
图书馆(dplyr)
图书馆(比例尺)
图书馆(treemapify)
图书馆(供猫用)
#加载数据----
数据(婚姻,套餐=“马赛克数据”)
plotdata%计数(OfficialTile)

#plotdata$officialTitle错误消息不是由
forcats
引发的。在您的情况下,它是由
count
ing空df产生的。试试这个:

#空数据帧
DF警告:因子“x”包含隐式NA,考虑使用
#>‘forcats::fct_explicit_na’`
#>#A tible:1 x 2
#>x n
#>    
#> 1       0
#为了避免警告,请使用.drop=FALSE,这也会导致df为空
dplyr::count(df,x,.drop=FALSE)
#>#tible:0 x 2
#> # ... 有两个变量:x,n
由(v0.3.0)于2020年4月14日创建

使用
婚姻
数据集:

#加载数据----
图书馆(dplyr)
数据(婚姻,套餐=“马赛克数据”)
dplyr::筛选器(婚姻,假)%%>%计数(官方时间)
警告:因子“官方标题”包含隐式NA,考虑使用
#>‘forcats::fct_explicit_na’`
#>#A tible:1 x 2
#>官方的
#>            
#> 1               0
dplyr::筛选器(婚姻,FALSE)%%>%计数(officialTitle,.drop=FALSE)
#>#tibble:9 x 2
#>官方的
#>                  
#>1“主教”0
#>2“天主教牧师”0
#>3“总书记”0
#>4“巡回法官”0
#>5“长者”0
#>6“婚姻官”0
#>7“部长”0
#>8“牧师”0
#>9“牧师”0

由(v0.3.0)于2020年4月14日创建。

您的意思是此警告不可避免?我正在尝试创建基于用户输入变量显示树状图的闪亮应用程序。在某些情况下,输出0行,因此这是针对猫的警告。使用
dplyr::count(…,.drop=FALSE)
将导致没有警告的空df。我认为这应该解决你的问题。我仍然得到警告:-(嗯,我刚刚编辑了我的帖子。没有警告。至少在我的例子中是这样。').drop=FALSE`通常防止删除空因子级别,即数据集中没有obs的因子级别。具有
drop
的函数的其他示例有
dplyr::group_by
ggplot2::facet_wrap
plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
plotdata <- na.omit(plotdata)

  if (nrow(plotdata) > 0){
    str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
    print(str)
    print(plotdata)
[1] "nrows < 0. Number of rows is  0"
# A tibble: 0 x 2
# ... with 2 variables: officialTitle <fct>, n <int>
Warning message:
Factor `officialTitle` contains implicit NA, consider using `forcats::fct_explicit_na`
# Load packages ----
library(shiny)
library(ggplot2)
library(dplyr)
library(scales)
library(treemapify)
library(forcats)

# Load data ----
data(Marriage, package="mosaicData")

plotdata <- dplyr::filter(Marriage, FALSE) %>% count(officialTitle)
#plotdata$officialTitle <- fct_explicit_na(plotdata$officialTitle)
plotdata <- na.omit(plotdata)

  if (nrow(plotdata) > 0){
    str <- paste("nrows > 0. Number of rows is ", nrow(plotdata))
    print(str)
    print(plotdata)
    ggplot(plotdata, 
           aes(fill = officialTitle, 
               area = n,
               label = officialTitle)) +
      geom_treemap() + 
      geom_treemap_text(colour = "white", 
                        place = "centre") +
      labs(title = "Marriages by officiate") +
      theme(legend.position = "none")
  } else {
    str <- paste("nrows < 0. Number of rows is ", nrow(plotdata))
    print(str)
    print(plotdata)
  }