互动图不起作用

时间:2019-09-27 04:21:30

标签: r

我正在尝试使用以下代码来执行我的要求之一,但无法成功。当我在Filter1下对趋势和相关性进行过滤时,将显示图。下面是它的代码。有没有办法做到这一点?反应函数内部有很多if语句,也许就是因为这个原因。

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

{r setup, include=FALSE}
library(flexdashboard)
library(readxl)
library(tidyverse)
library(lubridate)
library(ggplot2)
library(reshape)
library(shiny)
library(plotly)
```

```{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 = c("tbl_df", "tbl", "data.frame"))
colnames(Copy_of_mill_para) <- gsub(" ","_",colnames(Copy_of_mill_para))
Copy_of_mill_para <- as.data.frame(Copy_of_mill_para)
Copy_of_mill_para1 <- melt(Copy_of_mill_para,id=c("Date"))
Copy_of_mill_para1 <- Copy_of_mill_para1 %>%
   mutate(Date = ymd_hms(Date),
          Year = year(Date),
          Month = format(Date, "%b"),
          Day = format(Date, "%a"),
          DateDay = day(Date),
          Hour = format(Date, "%H"),
          Min =  format(Date, "%M"),
          Sec =  format(Date, "%S"))
```

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

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

```{r}
bLabel <- reactive({
    if(input$c == "Trend") {
      "Trend"
    } else {
      "First Variable"
    }
  })
bLabel2 <- reactive({
  if (bLabel() == "Trend") {
    levels(factor(Copy_of_mill_para1$variable))
  } else {
    levels(factor(Copy_of_mill_para1$variable))
  }
})
bLabel3 <- reactive({
  if(bLabel() == "Trend"){
    ""
  } else {
    print("Second Variable")
  }
})
    selectInput("c","Filter1",choices = c("","Trend","Correlation"))
    renderUI(selectInput("b", bLabel(), choices = c("ALL",bLabel2())))
    renderUI(selectInput(bLabel3(),bLabel3(),choices = c("ALL",bLabel2())))
```



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

### Chart A

```{r}
plotlyOutput("g1")
asd <- reactive({
  if (input$c == "Trend") {
    Copy_of_mill_para1
  }
  if (input$c == "Trend" && input$b != "ALL") {
    Copy_of_mill_para1 <- Copy_of_mill_para1 %>% filter(variable %in% input$b)
  }
  if (input$c == "Correlation" && input$b != "ALL" && input$bLabel3() != "ALL" ){
    Copy_of_mill_para
  }
})
  output$g1 <- renderPlotly({
    if (input$c =="Trend") {
       p1 <- ggplot(asd(),aes(x=Date,y=value,color=variable))+geom_line(size =  .2)+theme(axis.text.y=element_text(angle=0, hjust=1,size = 0.1))+theme(axis.title.y=element_blank(),axis.title.x=element_blank())+theme(axis.text.x=element_blank())+theme(legend.title = element_blank())+ theme(legend.text=element_text(size=7.5))+theme(legend.position = "none")
print(ggplotly(p1))
    } else if (input$c =="Correlation")
  p2 <- ggplot(asd(),aes(x=input$b,y=input$bLabel3(),fill=Date))+geom_point()+theme(axis.text.y=element_text(angle=0, hjust=1,size = 0.1))+theme(axis.title.y=element_blank(),axis.title.x=element_blank())+theme(axis.text.x=element_blank())+theme(legend.title = element_blank())+ theme(legend.text=element_text(size=7.5))+theme(legend.position = "none")
print(ggplotly(p2))
})
```


0 个答案:

没有答案