title: facet_grid in Shiny flexdashboard giving error "Faceting variables must have at least one value"
body: <p>I am having some trouble getting a ggplot2 <code>facet_grid</code> plot working for an evaluation system. The plot renders well but I get the following error in the browser and console:</p>
  
  <p><strong>Error in : Faceting variables must have at least one value</strong></p>
  
  <p>This occurs every time I switch the brand entry based on the input <code>input$brand</code>. The application doesn't crash but the error message is annoying.</p>
  
  <p>I have prepared this reproducible example:</p>
  
  <pre><code>---
  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 &lt;- mtcars %&gt;% 
      tibble::rownames_to_column() %&gt;% 
      rename(model = rowname) %&gt;% 
      mutate(brand = gsub( " .*$", "", model )) %&gt;% 
      mutate(model = model) %&gt;% 
      select(brand, model,  everything())  %&gt;% 
      tidyr::gather(key = 'measure', value = "value", mpg:carb) %&gt;%
      mutate(ranking = as.factor(sample(x = c(1, 2, 3), size = n(), replace = TRUE))) %&gt;%
  
      mutate(power = case_when(
          .$measure == "hp" &amp; value &gt; 200 | (.$measure == "cyl" &amp; value == 8) ~ "high",
          .$measure == "hp" &amp; value &lt; 200 | (.$measure == "cyl" &amp; value == 8) ~ "medium",
          .$measure == "hp" &amp; value &gt; 100 | (.$measure == "cyl" &amp; value == 6) ~ "high",
          .$measure == "hp" &amp; value &lt; 100 | (.$measure == "cyl" &amp; value == 6) ~ "medium",
          .$measure == "hp" &amp; value &gt; 50  | (.$measure == "cyl" &amp; value == 6) ~ "high",
          .$measure == "hp" &amp; value &lt; 50  | (.$measure == "cyl" &amp; value == 6) ~ "medium",
  
          .$measure == "hp" &amp; value &gt; 200 | (.$measure == "carb" &amp; value &gt;  4) ~ "high",
          .$measure == "hp" &amp; value &lt; 200 | (.$measure == "carb" &amp; value &lt;= 4) ~ "medium",
          .$measure == "hp" &amp; value &gt; 100 | (.$measure == "carb" &amp; value &gt;  2.8) ~ "high",
          .$measure == "hp" &amp; value &lt; 100 | (.$measure == "carb" &amp; value &lt;= 2.8) ~ "medium",
          .$measure == "hp" &amp; value &gt; 50  | (.$measure == "carb" &amp; value &gt; 2) ~ "high",
          .$measure == "hp" &amp; value &lt; 50  | (.$measure == "carb" &amp; value &lt;= 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 &lt;- 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 &lt;- make.unique(mtcars_tidy$measure[order(mtcars_tidy$power, mtcars_tidy$ranking)])
  mtcars_tidy$measure &lt;- factor(mtcars_tidy$measure, levels=nameorder, 
                                     ordered = TRUE)
  
  dataset &lt;- reactive({
      subset(mtcars_tidy, brand == input$brand &amp; 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))
  })    
  ```
  </code></pre>
  
  <p><strong>EDIT 1</strong>: I changed <code>facet_grid</code> to <code>facet_wrap</code> but the error still there.</p>
  
  <p><strong>EDIT 2</strong>: As per suggestion, I switched to <code>facet_wrap</code> with this formula: <code>p &lt;- p + facet_wrap(power ~ .)</code>. Still same error. I also tried this other formula 
  <code>p &lt;- p + facet_wrap(power ~ ranking)</code>. Error still there.</p>
  
  <p><strong>EDIT 3</strong>: On the <code>facet_wrap</code> function I also tried with these formulas as well: </p>
  
  <ul>
  <li><code>facet_wrap(~power )</code></li>
  <li><code>facet_wrap(vars(power ))</code> </li>
  <li><code>facet_wrap(vars(power , ranking))</code>. </li>
  </ul>
  
  <p>The error is still the same (identical). No change (<code>Error in : Faceting variables must have at least one value</code>).</p>
  
  <p><strong>EDIT 4</strong>: If I try with <code>facet_wrap(power)</code>, the error is even worse because crashes Shiny with this mouthful:</p>
  
  <pre><code>Error: Column `function (lambda = 1) \n{\n    if (!is.numeric(lambda) || is.na(lambda)) \n        stop("invalid argument 'lambda'")\n    if (lambda &lt;= 0) \n        return(make.link("log"))\n    if (lambda == 1) \n        return(make.link("identity"))\n    linkfun &lt;- function(mu) mu^lambda\n    linkinv &lt;- function(eta) pmax(eta^(1/lambda), .Machine$double.eps)\n    mu.eta &lt;- function(eta) pmax((1/lambda) * eta^(1/lambda - \n        1), .Machine$double.eps)\n    valideta &lt;- function(eta) all(is.finite(eta)) &amp;&amp; all(eta &gt; \n        0)\n    link &lt;- 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
  </code></pre>