当条件的选择相反时,筛选选项不起作用

时间:2019-10-11 15:19:31

标签: r shiny flexdashboard

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

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

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

1 个答案:

答案 0 :(得分:0)

合并您的import pandas as pd from geocodio import GeocodioClient API_KEY = 'insert_your_key_here' client = GeocodioClient(API_KEY) customers = pd.read_csv("example.csv", header=None) customers['address_string'] = customers[0].map(str) + ' ' + customers[1].map(str) + customers[2].map(str) geocoded_acuracy = [] geocoded_acuracy_type = [] for address in customers['address_string'].values: geocoded_address = client.geocode(address) accuracy = geocoded_address.best_match.get("accuracy") accuracy_type = geocoded_address.best_match.get("accuracy_type") geocoded_acuracy.append(accuracy) geocoded_acuracy_type.append(accuracy_type) customers['accuracy'] = geocoded_acuracy customers['accuracy_type'] = geocoded_acuracy_type results = customers[['address_string', 'accuracy', 'accuracy_type']] results.to_csv('results.csv') 语句,并添加if以确保这些输入在求值之前就存在(最初为NULL)可能会更容易。

此外,它对“ ALL”变量进行了错误检查以制作相关图-您可以先确定输入值包含在数据中(使用req(input$...)),然后再进行绘图(为这个)。

当您更改%in% c的依赖项时,

output$g1将被调用两次。解决此问题的一种方法是在不想触发selectInput的输入上使用isolate

但是请查看是否有帮助。在很大程度上取决于您希望最终结果如何。

output$g1

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

### 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")