光泽flexdashboard中的facet_grid给出错误消息"Faceting变量必须具有至少一个值". [英] facet_grid in Shiny flexdashboard giving error "Faceting variables must have at least one value"

查看:100
本文介绍了光泽flexdashboard中的facet_grid给出错误消息"Faceting变量必须具有至少一个值".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让ggplot2 facet_grid图在评估系统上工作时遇到了一些麻烦.该图呈现良好,但在浏览器和控制台中出现以下错误:

I am having some trouble getting a ggplot2 facet_grid plot working for an evaluation system. The plot renders well but I get the following error in the browser and console:

以下方面的错误:构面变量必须至少具有一个值

每次我根据输入input$brand切换品牌条目时,都会发生这种情况.该应用程序不会崩溃,但错误消息令人讨厌.

This occurs every time I switch the brand entry based on the input input$brand. The application doesn't crash but the error message is annoying.

我准备了这个可重现的示例:

I have prepared this reproducible example:

---
title: "Power ranking for mtcars"
runtime: shiny
output:
  flexdashboard::flex_dashboard:
    orientation: rows
    source_code: embed
---



```{r rows.print = 25}
library(dplyr)
library(ggplot2)

mtcars_tidy <- mtcars %>% 
    tibble::rownames_to_column() %>% 
    rename(model = rowname) %>% 
    mutate(brand = gsub( " .*$", "", model )) %>% 
    mutate(model = model) %>% 
    select(brand, model,  everything())  %>% 
    tidyr::gather(key = 'measure', value = "value", mpg:carb) %>%
    mutate(ranking = as.factor(sample(x = c(1, 2, 3), size = n(), replace = TRUE))) %>%

    mutate(power = case_when(
        .$measure == "hp" & value > 200 | (.$measure == "cyl" & value == 8) ~ "high",
        .$measure == "hp" & value < 200 | (.$measure == "cyl" & value == 8) ~ "medium",
        .$measure == "hp" & value > 100 | (.$measure == "cyl" & value == 6) ~ "high",
        .$measure == "hp" & value < 100 | (.$measure == "cyl" & value == 6) ~ "medium",
        .$measure == "hp" & value > 50  | (.$measure == "cyl" & value == 6) ~ "high",
        .$measure == "hp" & value < 50  | (.$measure == "cyl" & value == 6) ~ "medium",

        .$measure == "hp" & value > 200 | (.$measure == "carb" & value >  4) ~ "high",
        .$measure == "hp" & value < 200 | (.$measure == "carb" & value <= 4) ~ "medium",
        .$measure == "hp" & value > 100 | (.$measure == "carb" & value >  2.8) ~ "high",
        .$measure == "hp" & value < 100 | (.$measure == "carb" & value <= 2.8) ~ "medium",
        .$measure == "hp" & value > 50  | (.$measure == "carb" & value > 2) ~ "high",
        .$measure == "hp" & value < 50  | (.$measure == "carb" & value <= 2) ~ "medium",
        TRUE ~ "low"
    )) 
```

# Sidebar {.sidebar data-width="350"}

```{r}
selectInput("brand", "Brand of the car", 
            choices = unique(mtcars_tidy$brand))

renderUI({
    selectInput("model", "Car model",
                choices = mtcars_tidy$model[mtcars_tidy$brand == levels(mtcars_tidy$brand)[1]])
})

br()

observe({
    brand <- input$brand
    updateSelectInput(session, "model", 
                      choices = mtcars_tidy$model[mtcars_tidy$brand == brand])
})    


# when switching the brand of the car, input$brand this error pops up:
# Error in : Faceting variables must have at least one value
```


# Main

##

### Plot power ranking for each measure

```{r}
nameorder <- make.unique(mtcars_tidy$measure[order(mtcars_tidy$power, mtcars_tidy$ranking)])
mtcars_tidy$measure <- factor(mtcars_tidy$measure, levels=nameorder, 
                                   ordered = TRUE)

dataset <- reactive({
    subset(mtcars_tidy, brand == input$brand & model == input$model) 
})


renderPlot({
    ggplot(dataset(), aes(x = ranking, y = measure)) +
        geom_segment(aes(yend = measure), xend=0, color = "grey50") +
        geom_point(size = 3, aes(colour = power)) +
        scale_colour_brewer(palette="Set1", limits = c("high","medium", "low")) +
        theme_bw() +
        theme(panel.grid.major.y = element_blank()) +   # No horizontal grid lines
        facet_grid(power ~ ., scales="free_y", space="free_y") +
        ggtitle(paste0("Brand: ", input$brand, ", Model: " , input$model))
})    
```

编辑1 :我将facet_grid更改为facet_wrap,但错误仍然存​​在.

EDIT 1: I changed facet_grid to facet_wrap but the error still there.

编辑2 :根据建议,我使用以下公式切换到facet_wrap:p <- p + facet_wrap(power ~ .).还是一样的错误.我也尝试了这个其他公式 p <- p + facet_wrap(power ~ ranking).错误仍然存​​在.

EDIT 2: As per suggestion, I switched to facet_wrap with this formula: p <- p + facet_wrap(power ~ .). Still same error. I also tried this other formula p <- p + facet_wrap(power ~ ranking). Error still there.

编辑3 :在facet_wrap函数上,我也尝试使用以下公式:

EDIT 3: On the facet_wrap function I also tried with these formulas as well:

  • facet_wrap(~power )
  • facet_wrap(vars(power ))
  • facet_wrap(vars(power , ranking)).
  • facet_wrap(~power )
  • facet_wrap(vars(power ))
  • facet_wrap(vars(power , ranking)).

错误仍然相同(相同).不变(Error in : Faceting variables must have at least one value).

The error is still the same (identical). No change (Error in : Faceting variables must have at least one value).

编辑4 :如果我尝试使用facet_wrap(power),则错误甚至更严重,因为此满嘴使Shiny崩溃:

EDIT 4: If I try with facet_wrap(power), the error is even worse because crashes Shiny with this mouthful:

Error: Column `function (lambda = 1) \n{\n    if (!is.numeric(lambda) || is.na(lambda)) \n        stop("invalid argument 'lambda'")\n    if (lambda <= 0) \n        return(make.link("log"))\n    if (lambda == 1) \n        return(make.link("identity"))\n    linkfun <- function(mu) mu^lambda\n    linkinv <- function(eta) pmax(eta^(1/lambda), .Machine$double.eps)\n    mu.eta <- function(eta) pmax((1/lambda) * eta^(1/lambda - \n        1), .Machine$double.eps)\n    valideta <- function(eta) all(is.finite(eta)) && all(eta > \n        0)\n    link <- paste0("mu^", round(lambda, 3))\n    structure(list(linkfun = linkfun, linkinv = linkinv, mu.eta = mu.eta, \n        valideta = valideta, name = link), class = "link-glm")\n}` must be a 1d atomic vector or a list

推荐答案

dataset()中没有任何行时,将发生此错误.当我运行您的代码(带有facet_grid(power ~ .,的当前版本)时,它实际上可以正常工作.当我选择一个新品牌时,在input$model列表更新时显示此错误的间隙很小.一旦完成,并且brandmodel的组合返回行,该图将很好地显示.

This error is occuring when dataset() has no rows in it. When I run your code (the current version with the facet_grid(power ~ .,), it actually works fine. When I choose a new brand, there is a short gap where it displays this error while the input$model list updates. Once it does, and the combination of brand and model return rows, the plot displays nicely.

您可以通过使用req推迟渲染图直到满足某些要求来防止出现此间隙.只需在renderPlot

You can prevent this gap by using req to hold off rendering the plot until certain requirements are met. Just insert the following code at the top of your renderPlot

req(nrow(dataset()) > 0)

如果dataset()不包含至少一行,这将阻止renderPlot运行.在这种情况下,绘图将一直为空白(删除可怕的错误消息),直到可以使用数据为止.添加该行后,您的应用程序似乎运行良好(顺便说一句,看起来还不错).

This will prevent the renderPlot from running if dataset() doesn't contain at least one row. In that case, the plot will just be blank (removing the scary error message) until the data is ready to use. With that line added, your app seems to be running fine (and looks quite nice, by the way).

通过在shiny上下文之外测试代码,可以查看该错误消息的来源.这是您的情节的最小示例:

You can see the source of that error message by testing your code outside the shiny context. Here's a minimal example of your plot:

ggplot(dataset, aes(x = ranking, y = measure)) +
        geom_segment(aes(yend = measure), xend=0, color = "grey50") +
        geom_point(size = 3, aes(colour = power)) +
        facet_grid(power ~ ., scales="free_y", space="free_y")

当我使用此呼叫进行dataset时:

when I make dataset using this call:

dataset <- subset(mtcars_tidy, brand == 'Honda' & model == 'Honda Civic')

该图可以正确渲染.当我使用不返回任何行的subset时:

The plot renders correctly. When I use a subset that doesn't return any rows:

dataset <- subset(mtcars_tidy, brand == 'Honda' & model == 'Civic')

我也遇到同样的错误:

Error: Faceting variables must have at least one value

这篇关于光泽flexdashboard中的facet_grid给出错误消息"Faceting变量必须具有至少一个值".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆