首页 > 解决方案 > 当标准选择相反时,过滤选项不起作用

问题描述

我在这里面临一个奇怪的问题,真的不知道为什么会这样。所以需要大家的帮助。

当您运行以下应用程序并从下拉列表中选择“相关性”时,不会显示任何绘图。但是,当您首先选择“趋势”然后选择“相关性”时,将显示该图。这里很奇怪。不知道我在这里做错了什么。请帮忙

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: scroll
    runtime: shiny
    theme: cosmo
---

```{r setup, include=FALSE}
library(shiny)
library(flexdashboard)
library(tidyverse)
```

```{r}
Copy_of_mill_para <- structure(list(Date = structure(c(1505779200, 1505779500, 1505779800, 
1505780100, 1505780400, 1505780700, 1505781000, 1505781300, 1505781600
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), A = c(42, 
40, 41, 45, 25, 39, 44, 25, 39), B = c(27, 36, 40, 31, 44, 34, 
39, 44, 41), C = c(39, 42, 33, 26, 29, 42, 24, 34, 35)), row.names = c(NA, 
-9L), class = "data.frame")
Copy_of_mill_para1 <- Copy_of_mill_para %>% 
    gather(variable, value, -Date)
```

Summary
=================

Inputs {.sidebar}
-----------------------------------------------------------------------

```{r}
selectInput("c", "Filter1", choices = c("","Trend","Correlation"))
output$filter_2 <- renderUI({
    if (input$c == "") {
      return()
    } else if (input$c == "Trend") {
      label = "Trend"
      selectInput("b",
                label,
                choices = c("ALL", levels(factor(Copy_of_mill_para1$variable))))
    } else {
      label = "First Variable"
      selectInput("b",
                label,
                choices = c(levels(factor(Copy_of_mill_para1$variable))))
    }

  })

output$filter_3 <- renderUI({
    # If missing input, return to avoid error later in function
    if (input$c == "Trend"|input$c == "") {
      return()
    } else {
      selectInput("a",
                "Second Variable",
                choices = c(levels(factor(Copy_of_mill_para1$variable))))
    }
  })

uiOutput("filter_2")
uiOutput("filter_3")
output$filter_7 <- renderUI({
  if (input$c == "Trend")
  radioButtons("r",h5("Highlight"),choices = list("No", "Yes"),selected = "No", inline = T)  
})
uiOutput("filter_7")
```

Column {data-width=350}
-----------------------------------------------------------------------

### Chart A

```{r}
output$g1 <- renderPlot({
    req(input$c)
    if (input$c == "Trend") {
        plot_data <- Copy_of_mill_para1
    }
    if (input$c == "Trend" & input$b != "ALL") {
        plot_data <- Copy_of_mill_para1 %>% filter(variable == input$b)
    }
    if (input$c == "Correlation"){
        plot_data <- Copy_of_mill_para
    }

    if (input$c == "Trend" & input$r != "Yes") {
        ggplot(plot_data, aes(x = Date, y = value, color = variable)) +
            geom_line(size =  .2)
    } 
   else if (input$c == "Trend"& input$r == "Yes") {
        ggplot(plot_data, aes(x = Date, y = value, color = variable)) +
            geom_line(size =  .2)+geom_point(data = plot_data %>% filter(variable == "A"),aes(x=Date, y = value),color='red',size=3)
    }
  else if (input$c == "Correlation") {
        req(input$a)
        req(input$b)
        ggplot(plot_data, aes_string(x = input$b, y = input$a)) +
            geom_point()
    }
})

plotOutput("g1")
```

标签: rshinyflexdashboard

解决方案


if合并您的语句可能更容易,并添加req(input$...)以确保这些输入在评估之前存在(最初将为 NULL)。

此外,它对“ALL”变量进行错误检查以制作相关图 - 您可以在绘制之前首先确保输入值包含在(使用%in%)数据中(为此添加了语句)。

output$g1selectInput当您使用依赖项更改 c 时,将被调用两次。解决此问题的一种方法是isolate在您不想触发的输入上使用output$g1

但请看看这是否有帮助。很大程度上取决于您希望最终结果的样子。

### Chart A

```{r}
output$g1 <- renderPlot({
    req(input$b, input$c)

    if (input$c == "Trend") {
      req(input$r)

      if (input$b != "ALL") {
        plot_data <- Copy_of_mill_para1 %>% filter(variable == input$b)
      } else {
        plot_data <- Copy_of_mill_para1
      }

      if (input$r != "Yes") {
        p <- ggplot(plot_data, aes(x = Date, y = value, color = variable)) +
          geom_line(size =  .2)
      } else {
        p <- ggplot(plot_data, aes(x = Date, y = value, color = variable)) +
          geom_line(size =  .2)+geom_point(data = plot_data %>% filter(variable == "A"),aes(x=Date, y = value),color='red',size=3)
      }
    }

    if (input$c == "Correlation") {
      req(input$a)

      plot_data <- Copy_of_mill_para
      if (input$b %in% Copy_of_mill_para1$variable && input$a %in% Copy_of_mill_para1$variable) {
        p <- ggplot(plot_data, aes_string(x = input$b, y = input$a)) +
          geom_point()
      }
    }

    p
})

plotOutput("g1")

这是完整的 Rmarkdown,其中包含一些输入合并和其他小的修改:

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: scroll
    runtime: shiny
    theme: cosmo
---

```{r setup, include=FALSE}
library(shiny)
library(flexdashboard)
library(tidyverse)
```

```{r}
Copy_of_mill_para <- structure(list(
  Date = structure(c(1505779200, 1505779500, 1505779800, 1505780100, 1505780400, 1505780700, 1505781000, 1505781300, 1505781600), 
                   class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
  A = c(42, 40, 41, 45, 25, 39, 44, 25, 39), 
  B = c(27, 36, 40, 31, 44, 34, 39, 44, 41), 
  C = c(39, 42, 33, 26, 29, 42, 24, 34, 35)), 
  row.names = c(NA, -9L), class = "data.frame")

Copy_of_mill_para1 <- Copy_of_mill_para %>% 
  gather(variable, value, -Date)
```

Summary
=================

Inputs {.sidebar}
-----------------------------------------------------------------------

```{r}
selectInput("c", "Filter1", choices = c("", "Trend", "Correlation"))

output$filter_2 <- renderUI({
  if (input$c == "") {
    return()
  } else if (input$c == "Trend") {
    div(
      selectInput("b", "Trend", choices = c("ALL", levels(factor(Copy_of_mill_para1$variable)))),
      radioButtons("r", h5("Highlight"), choices = list("No", "Yes"), selected = "No", inline = T)  
    )
  } else {
    div(
      selectInput("b", "First Variable", choices = c(levels(factor(Copy_of_mill_para1$variable)))),
      selectInput("a", "Second Variable", choices = c(levels(factor(Copy_of_mill_para1$variable))))
    )
  }
})

uiOutput("filter_2")
```

Column {data-width=350}
-----------------------------------------------------------------------

### Chart A

```{r}
output$g1 <- renderPlot({
    req(input$b)

    if (isolate(input$c) == "Trend") {
      req(input$r)

      if (input$b != "ALL") {
        plot_data <- Copy_of_mill_para1 %>% filter(variable == input$b)
      } else {
        plot_data <- Copy_of_mill_para1
      }

      if (input$r != "Yes") {
        p <- ggplot(plot_data, aes(x = Date, y = value, color = variable)) +
          geom_line(size =  .2)
      } else {
        p <- ggplot(plot_data, aes(x = Date, y = value, color = variable)) +
          geom_line(size =  .2)+geom_point(data = plot_data %>% filter(variable == "A"),aes(x=Date, y = value),color='red',size=3)
      }
    }

    if (isolate(input$c) == "Correlation") {
      req(input$a)

      plot_data <- Copy_of_mill_para
      if (input$b %in% Copy_of_mill_para1$variable && input$a %in% Copy_of_mill_para1$variable) {
        p <- ggplot(plot_data, aes_string(x = input$b, y = input$a)) +
          geom_point()
      }
    }

    p
})

plotOutput("g1")
```

推荐阅读