Interactive plots are not working

I am trying with the below code to execute one of my requirements, But not able to succeed. When I filter on Trend and correalation under Filter1 the plots are getting displayed. Below is the code for it

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



### Table

```{r}
verbatimTextOutput("click")
output$click <- renderPrint({
    d <- event_data("plotly_click")
    if (is.null(d)) "Click events appear here (double-click to clear)" else d
  })
```

Hi @vinayprakash808!

Would you mind editing your post to fix the formatting of your R Markdown source, to make your question easier to read and understand? Here’s the trick for getting it to appear correctly: FAQ: How to Format R Markdown Source

It also might help if you can explain more clearly what you are expecting to have happen, and what happens instead.

Thanks, I have edited the question. Actually when you run this code, you do see some filters. When you click on Trend or Correlation. The plots are not displayed. Some issues in the code and I am not able to figure it out. Please guide

Actually when I run this code I see an error message since it is not a reproducible example, you have being asked many times to make your questions with a proper reprex and is for a good reason, so please save your self time and start with a proper reprex from the beginning.

Error: `path` does not exist: 'D:/Analytics/Freelancing projects/New folder/Copy of mill para.xlsx'
1 Like

I apologies for this. I have edited my code.

Im going to give you a simplified example that I think is similar to what you are trying to accomplish (not a final solution for your code, just an example for you to expand) and if is not then you could use it as a starting point for building a proper reprex so you could more easily get help.

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



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") {
        ggplot(plot_data, aes(x = Date, y = value, color = variable)) +
            geom_line(size =  .2)
    } 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 Like

Perfect. This is what I wanted exactly. Thanks a lottttttttttttt

I used ggplotly instead of ggplot and it is working. One more thing. Actually I just tried with the below code at the end to have dataframe from the selected area. Like if I click on the plot at the some area, i will get the data points below


```{r}
verbatimTextOutput("click")
output$click <- renderPrint({
    d <- event_data("plotly_click")
    if (is.null(d)) "Click events appear here (double-click to clear)" else d
  })
```

Hi . Actually with the help of this code, i was able to add extra features to my dashboard. But one thing I really understand here. To make it simple, I have added only 1 feature here (Highlight radiobutton). Now as soon as I open the app, and select "Correlation", there is no display. But When i select "Trend" first and then select "Correlation" there is a plot displayed. Not sure why this is happening. I have 1 extract line to the code that;s it. Also can you also explain the logic here as to why this is happening. Looks very strange here right? Please guide. Below is the updated code

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

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.