Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“发光flexdashboard中的面网格给出错误”;镶嵌面变量必须至少有一个值;_R_Ggplot2_Shiny_Flexdashboard - Fatal编程技术网

“发光flexdashboard中的面网格给出错误”;镶嵌面变量必须至少有一个值;

“发光flexdashboard中的面网格给出错误”;镶嵌面变量必须至少有一个值;,r,ggplot2,shiny,flexdashboard,R,Ggplot2,Shiny,Flexdashboard,我在为评估系统绘制ggplot2facet\u网格时遇到一些问题。绘图渲染良好,但在浏览器和控制台中出现以下错误: 错误:镶嵌面变量必须至少有一个值 每次我根据输入input$brand切换品牌条目时都会发生这种情况。应用程序不会崩溃,但错误消息令人讨厌 我准备了这个可复制的例子: --- title: "Power ranking for mtcars" runtime: shiny output: flexdashboard::flex_dashboard: orientatio

我在为评估系统绘制ggplot2
facet\u网格时遇到一些问题。绘图渲染良好,但在浏览器和控制台中出现以下错误:

错误:镶嵌面变量必须至少有一个值

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

我准备了这个可复制的例子:

---
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))
})    
```

dataset()
中没有行时,会发生此错误。当我运行您的代码时(当前版本带有
facet\u网格(power~,
),它实际上工作得很好。当我选择一个新品牌时,当
输入$model
列表更新时,它会显示此错误。一旦更新,并且
品牌
模型
返回行的组合,绘图会显示得很好

您可以通过使用
req
延迟渲染绘图,直到满足某些要求,以防止出现此间隙。只需在
renderPlot
顶部插入以下代码即可

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


您可以通过在
shining
上下文之外测试代码来查看错误消息的来源。下面是一个简单的绘图示例:

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
时:

dataset <- subset(mtcars_tidy, brand == 'Honda' & model == 'Honda Civic')
我得到了你同样的错误:

错误:镶嵌面变量必须至少有一个值


尝试
facet\u wrap(~power,…)
。您需要显式地将这两个变量指定给
facet\u grid
。您是否也修改了公式?是的,我将其更改为
p,所以…不是。请注意,
facet\u wrap
中没有点。我还尝试了以下公式:facet\u wrap(~power)、facet\u wrap(vars(power))、facet\u wrap(vars)(电源,排名))。错误仍然相同。另一个,
facet\u wrap(电源)
,崩溃。请参阅上面的编辑。
dataset <- subset(mtcars_tidy, brand == 'Honda' & model == 'Honda Civic')
dataset <- subset(mtcars_tidy, brand == 'Honda' & model == 'Civic')