how to create plotly line chart in shinyapp with multiple level variable

263 views Asked by At

Hi im trying to plot simple chart using plotly in the Shiny app. It is a line chart with Frequency variable on the X-Axis and a Metric on y-axis. i'm expecting two lines in the charts as i'm calling in a variable that has two levels in the UI. i'm getting the chart when i have one input from the groupcheckbox, but the goes blank when i select two options. i dont know what i'm doing wrong here, I'm not getting any error notifications.thanks for your inputs in advance

pasting the code with data here.

library(shiny)
library(plotly)
library(readxl)

prod<-c("BrandB", "BrandA", "BrandB", "BrandA", "BrandB", "BrandA","BrandB","BrandA","BrandB","BrandA","BrandB","BrandA","BrandB","BrandA","BrandB","BrandA")
Freq<-c("W7","W7","W8","W8","W9","W9","W10","W10","W10","W10","W7","W7","W8","W8","W9","W9")
Metric1<-c(0.515,0.444,0.518,0.446,0.529,0.405,0.497,0.376,0.505,0.494,0.515,0.444,0.518,0.446,0.529,0.405)
Cut<-c("RBs","RBs","RBs","RBs","RBs","RBs","Total","Total","RBs","RBs","Total","Total","Total","Total","Total","Total")

DF<-data.frame(prod,Freq,Metric1,Cut)

str(DF)

DF$Brand<-as.factor(DF$prod)
DF$Wave<-factor(DF$Freq,levels=c("W7","W8","W9","W10"))

#View(DF)

ui <- fluidPage(
    
    # useShinydashboard(),
    
    titlePanel("Dassy"),
    
    fluidRow(
        
        column(3,
               checkboxGroupInput(inputId = "Prod", 
                                  label = "Choose Fill For the Chart",
                                  choices = c("BrandA" = "BrandA","BrandB" = "BrandB"),
                                  selected = "BrandA"
               )),
        column(3,
               selectInput(inputId = "Cut", 
                           label = "Choose Fill For the Chart",
                           choices = c("Total" = "Total","RBs" = "RBs"),
                           selected = "Total")
        ),actionButton("go_button", "GO !")
    ),
    
    mainPanel(
        plotlyOutput("distPlot"))
)


server <- function(input, output) {
    
    observeEvent(input$go_button,{
        output$distPlot <- renderPlotly({
            
            DF1 <- reactive({
                
                DF%>%filter(Cut == input$Cut & prod == input$Prod)
            }
            )
           
            p1<-plot_ly(DF1(),x=~Freq, y=~Metric1, color =  ~prod, width=1200, height=300)%>% add_lines(line = list(shape = "spline")) 
            p1<-p1%>%layout(yaxis=list(tickformat ="%"))
            
        })
        #observe Event
    })
    
}
#??plot_ly()

shinyApp(ui = ui, server = server)

With single option selected line graph generated

With two option selected blank chart gets generated

0

There are 0 answers