0

Hi i am not able to understand where i am making mistake from the below query i am getting plot for option "B" but for when i am selecting the option "A" there no plot showing. I want to plot the first part from "l" & "k" when i select "A" but somehow it is missing "A" and this work fine with "B". Need some help.

library(shinydashboard)
library(shiny)
library(shinyWidgets)
library(ggplot2)
## test data
Quarter <- c("Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q3",
             "Fy17Q3","Fy17Q3","Fy17Q4","Fy17Q4","Fy17Q4","Fy18Q1","Fy18Q1",
             "Fy18Q1","Fy18Q2","Fy18Q2","Fy18Q2") 
RiskTierDesc <- c("Above Normal","High","Normal","Above Normal","High","Normal",
                  "Above Normal","High","Normal","Above Normal","High","Normal",
                  "Above Normal","High","Normal","Above Normal","High","Normal")
Freq <- c(502,62,1452,549,88,1582,617,80,1578,530,68,1455,536,61,1551,600,52,2038) 
FreqbyPercent <- c(25,3,72,25,4,71,27,4,69,26,3,71,25,3,72,22,2,76)
QuarterInNum<- c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6) 
TotalNRinM <- c(33.044,0,56.459,18.089,0.234,39.774,99.451,20.608,86.166,257.532,
                3.93,336.079,493.464,7.952,298.565,661.728,189.184,1172.245) 
TotalNRinMPercent <- c(37,0,63,31,0,68,48,10,42,43,1,56,62,1,37,33,9,58) 
File2<- data.frame(Quarter,RiskTierDesc,Freq,FreqbyPercent,QuarterInNum,TotalNRinM,
                   TotalNRinMPercent) 
File2$RiskTierDesc = factor(File2$RiskTierDesc, levels=c("High", "Above Normal", "Normal"))


#========================================UI=============================================================#

ui <- dashboardPage(
  dashboardHeader(title = "Basic Dashboard"),

  dashboardSidebar(
                   sidebarMenu( selectInput("app", 
                                         "Select App:", 
                                         choices = c("","A","B"), 
                                         selected = "A", 
                                         multiple = FALSE)),
                             sliderTextInput("Quarter","Select Quarter:",
                                              choices =  unique(File2$Quarter),
                                              selected =  unique(File2$Quarter)[c(2, 5)])),     

  dashboardBody(
    fluidRow(
      box(solidHeader = TRUE 
          ,collapsible = TRUE,align="center",offset = 2,title = "RiskTier Vs Quater",status = "warning", plotOutput("k", height = "300px"),width = 6)
      ,


      box(solidHeader = TRUE 
          ,collapsible = TRUE,align="center",offset = 4,title = "RiskTier Vs Quater(%)",status = "warning", plotOutput("l", height = "300px"),width = 6)
    )))



#==========================================SERVER=======================================================#

server <- function(input, output) {

  dataInput <- reactive({

    qfrom <- File2$QuarterInNum[match(input$Quarter[1], File2$Quarter)]
    qto <- File2$QuarterInNum[match(input$Quarter[2], File2$Quarter)]
    test <- File2[File2$QuarterInNum %in% seq(from=qfrom,to=qto),]
    #print(test)
    test
    })

  x<-reactive({input$app})


  output$k<- renderPlot({
    if (x()=="A"){
    ggplot(dataInput(), 
           aes(x=Quarter, y=Freq, group=RiskTierDesc, colour=RiskTierDesc)) + 
      geom_line(aes(size=RiskTierDesc)) +
      geom_point() + ylim(0,2500) +
      scale_color_manual(values=c("red","orange","green")) +
      scale_size_manual(values=c(1,1,1)) +
      labs( x = "Quarter", y = "Frequency") +
      geom_text(aes(label = Freq), position = position_dodge(0),vjust = -1) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())}})


  output$l<- renderPlot({
    if (x()=="A"){
    ggplot(dataInput(), 
           aes(x=Quarter, y=FreqbyPercent, group=RiskTierDesc, colour=RiskTierDesc)) + 
      geom_line(aes(size=RiskTierDesc)) +
      geom_point() + ylim(0,100) +
      scale_color_manual(values=c("red","orange","green")) +
      scale_size_manual(values=c(1,1,1)) +
      labs( x = "Quarter", y = "Frequency(%)") +
      geom_text(aes(label = FreqbyPercent), position = position_dodge(0),vjust = -1) +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())}})




  output$k<- renderPlot({
    if (x()=="B"){
      ggplot(dataInput(), 
             aes(x=Quarter, y=TotalNRinM, group=RiskTierDesc, colour=RiskTierDesc)) + 
        geom_line(aes(size=RiskTierDesc)) +
        geom_point() + ylim(0,2500) +
        scale_color_manual(values=c("red","orange","green")) +
        scale_size_manual(values=c(1,1,1)) +
        labs( x = "Quarter", y = "Frequency") +
        geom_text(aes(label = TotalNRinM), position = position_dodge(0),vjust = -1) +
        theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
  }})

  output$l<- renderPlot({
    if (x()=="B"){ggplot(dataInput(), 
             aes(x=Quarter, y=TotalNRinMPercent, group=RiskTierDesc, colour=RiskTierDesc)) + 
        geom_line(aes(size=RiskTierDesc)) +
        geom_point() + ylim(0,100) +
        scale_color_manual(values=c("red","orange","green")) +
        scale_size_manual(values=c(1,1,1)) +
        labs( x = "Quarter", y = "Frequency(%)") +
        geom_text(aes(label = TotalNRinMPercent), position = position_dodge(0),vjust = -1) +
        theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())}})

}

shinyApp(ui, server)
lbusett
  • 5,801
  • 2
  • 24
  • 47
JK1185
  • 109
  • 1
  • 8
  • 1
    Would you add some data (e.g., output from `dput(head(File2,n=20))`) so that we can test it? I don't get enough from the code to be able to construct my own (and the onus is on you to make it easy for somebody to play with this ... ala a fully [*reproducible example*](https://stackoverflow.com/questions/5963269)). – r2evans Feb 09 '19 at 17:34

2 Answers2

0

I think the problem here is that you have different renderers "named" in the same way ( i.e., you create both output$l and output$k twice in the server code). This can not work, because one is going to "mask" the other one since both are triggered when x() is changed. To make this work, you should rearrange your server code so to have only one renderer per plot.

Something like this should work (though I cannot test because I do not have your data - consider always providing a reproducible example when posting a question):


    server <- function(input, output) {

      dataInput <- reactive({

        qfrom <- File2$QuarterInNum[match(input$Quarter[1], File2$Quarter)]
        qto <- File2$QuarterInNum[match(input$Quarter[2], File2$Quarter)]
        test <- File2[File2$QuarterInNum %in% seq(from=qfrom,to=qto),]
        #print(test)
        test
      })

      x<-reactive({input$app})

     output$k<- renderPlot({
        if (x() == "A"){
          plotvar <- "Freq" 
        } else {
          plotvar <- "TotalNRinM" 
        }
        data_toplot <- dataInput()
        names(data_toplot)[names(data_toplot) == plotvar] <- "plotvar"
        ggplot(data_toplot, 
               aes(x=Quarter, y=plotvar, group=RiskTierDesc, colour=RiskTierDesc)) + 
          geom_line(aes(size=RiskTierDesc)) +
          geom_point() + ylim(0,2500) +
          scale_color_manual(values=c("red","orange","green")) +
          scale_size_manual(values=c(1,1,1)) +
          labs( x = "Quarter", y = "Frequency") +
          geom_text(aes(label = plotvar), position = position_dodge(0),vjust = -1) +
          theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
      })

      output$l<- renderPlot({
        if (x() =="A"){
          plotvar <- "FreqbyPercent" 
        } else {
          plotvar <- "TotalNRinMPercent" 
        }
        data_toplot <- dataInput()
        names(data_toplot)[names(data_toplot) == plotvar] <- "plotvar"
        ggplot(data_toplot, 
               aes(x=Quarter, y=plotvar, group=RiskTierDesc, colour=RiskTierDesc)) + 
          geom_line(aes(size=RiskTierDesc)) +
          geom_point() + ylim(0,100) +
          scale_color_manual(values=c("red","orange","green")) +
          scale_size_manual(values=c(1,1,1)) +
          labs( x = "Quarter", y = "Frequency(%)") +
          geom_text(aes(label = plotvar), position = position_dodge(0),vjust = -1) +
          theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
      })
    }

    shinyApp(ui, server)

lbusett
  • 5,801
  • 2
  • 24
  • 47
  • Hi Ibusett please find date – JK1185 Feb 09 '19 at 18:01
  • @Ibusett (Part1) Quarter<- c("Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q4","Fy17Q4","Fy17Q4","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q2","Fy18Q2","Fy18Q2") RiskTierDesc <- c("Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal") Freq <- c(502,62,1452,549,88,1582,617,80,1578,530,68,1455,536,61,1551,600,52,2038) FreqbyPercent <- c(25,3,72,25,4,71,27,4,69,26,3,71,25,3,72,22,2,76) – JK1185 Feb 09 '19 at 18:36
  • @Ibusett(Part2) QuarterInNum<- c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6) TotalNRinM <- c(33.044,0,56.459,18.089,0.234,39.774,99.451,20.608,86.166,257.532,3.93,336.079,493.464,7.952,298.565,661.728,189.184,1172.245) TotalNRinMPercent <- c(37,0,63,31,0,68,48,10,42,43,1,56,62,1,37,33,9,58) File2<- data.frame(Quarter,RiskTierDesc,Freq,FreqbyPercent,QuarterInNum,TotalNRinM,TotalNRinMPercent) File2$RiskTierDesc = factor(File2$RiskTierDesc, levels=c("High", "Above Normal", "Normal")) – JK1185 Feb 09 '19 at 18:37
  • Hi Thank you for your input i am not able to understand this part data_toplot <- dataInput() names(data_toplot)[names(data_toplot) == plotvar] <- "plotvar". How this will effect and i also shared the data – JK1185 Feb 10 '19 at 06:23
  • I edited the answer and it should now work. I also took the iberty to edit your question so that it includes the test data you provided (better than having it in comments). – lbusett Feb 10 '19 at 09:29
  • in this line(s): `data_toplot <- dataInput() names(data_toplot)[names(data_toplot) == plotvar] <- "plotvar` I am just finding the column in dataInput() that you want to plot (Freq or TotalNRinM, for exampl), and rename it to "plotvar". This allows to "recycle" the ggplot code without the need to use non standard evaluation (which could be a good idea, but is more complicated) – lbusett Feb 10 '19 at 09:33
0

Some short ideas: Why do you repeat the approx. same function four times ? what's about writing a function ? As far as I see only data for y- axis are different. Make a reactive, which changes value for the y-axis and pass that to the function.

yaxis <- reactive({
             if (input$app == "A")
                    x <- list("Freq","FreqbyPercent") 
             else if (input$yearset == "B")
                     x <- list("TotalNR","TotalNRinMPercent")
    })




plotter<- function(df,xname,yname){
x_var <- enquo(xname)
y_var <- enquo(yname)
ggplot(df, 
      aes(x=x_var, y=y_var, group=RiskTierDesc, colour=RiskTierDesc)) + 
 geom_line(aes(size=RiskTierDesc)) +
 geom_point() + ylim(0,100) +
 scale_color_manual(values=c("red","orange","green")) +
 scale_size_manual(values=c(1,1,1)) +
 labs( x = "Quarter", y = "Frequency(%)") +
 geom_text(aes(label = TotalNRinMPercent), position =position_dodge(0),vjust = -1) +
 theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank())}}) }

then call it in your.

renderPlot{(
 plotter(dataInput(),quarter,x[[1]])
)}

Sorry , little bit quick and dirty, maybe there are some mistakes in it.

Peter Hahn
  • 148
  • 8
  • Thank you for your reply, but if i have different y axis labels and also ylim is different can i get that updated using the function. or i need to use separate plots with the specification. Please assist i am new to this. @Peter Hahn – JK1185 Feb 09 '19 at 19:41