0

SelectInput function in ui is supposed to give me an option to choose "YES" or "NO". When "NO" is selected, it will choose the " if(("NO" %in% input$qualify_pit))" block in renderDataTable function in server and execute that perfectly. However, when I choose "YES" option, its block does not run, not displaying any table. I tried everything to get it to run its block (if(("YES" %in% input$qualify_pit))) but to no avail.

library(shiny)
library(shinythemes)
library(DT)

pit <- read.csv("PIT_STAT.csv")

pit_stat <- c("MLB_name","MLBId","LastName","FirstName","LW","W","L","Sv","BS","HLD","G","GS","CG","GF","QS","SHO","IP","BFP","H","X1B","X2B","X3B",
              "HR","R","ER","SH","SF","HBP","BB","IBB","BB_noIBB","K","WP","BLK","GB","FB","LD","POPU","SB",
              "CS","PKO","SVO","OUTS","K9","BB9","AVG","BABIP","HR9","GB_percent","HRperFB","ERA","KperBB",
              "K_percent","BB_percent","K_minus_BB","WHIP","LD_percent","FB_percent","GBperFB")

pit_def <- c("MLB_name","MLBId","LastName","FirstName","LW","W","L","ERA","IP","H","HR","R","ER","BB","K","K9","BB9","HR9","WHIP",
             "GB_percent","FB_percent","LD_percent","K_percent","BB_percent","KperBB","K_minus_BB")

ui <- shinyUI(fluidPage(

  shinythemes::themeSelector(),

  theme = shinytheme("paper"),

  titlePanel("WSFB Stats Lab"),

  fluidRow(
    uiOutput("uis")
  ),

  fluidRow(

    tabsetPanel(id = "tabs",
                tabPanel("Pitch Table",dataTableOutput("pitch_table"))
    )
  )


)
)


server <- shinyServer(function(input, output, session){


    output$uis <- renderUI({

      if(input$tabs == "Pitch Table")
      {
        pit <- read.csv("PIT_STAT.csv")

        pit_stat <- c("MLB_name","MLBId","LastName","FirstName","LW","W","L","Sv","BS","HLD","G","GS","CG","GF","QS","SHO","IP","BFP","H","X1B","X2B","X3B",
                      "HR","R","ER","SH","SF","HBP","BB","IBB","BB_noIBB","K","WP","BLK","GB","FB","LD","POPU","SB",
                      "CS","PKO","SVO","OUTS","K9","BB9","AVG","BABIP","HR9","GB_percent","HRperFB","ERA","KperBB",
                      "K_percent","BB_percent","K_minus_BB","WHIP","LD_percent","FB_percent","GBperFB")

        pit_def <- c("MLB_name","MLBId","LastName","FirstName","LW","W","L","ERA","IP","H","HR","R","ER","BB","K","K9","BB9","HR9","WHIP",
                     "GB_percent","FB_percent","LD_percent","K_percent","BB_percent","KperBB","K_minus_BB")

        wellPanel(
          checkboxGroupInput('show_vars', 'Variables to display', pit_stat, inline = TRUE, selected = pit_def),
          selectInput("qualify_pit","MIN IP:",choices = c("YES","NO"))
        )

      }
    })


  output$pitch_table <- renderDataTable({

    if(("YES" %in% input$qualify_pit))
    {
      pit <- read.csv("PIT_STAT.csv")
      pit2 <- pit[pit$IP >= 162,]
      DT::datatable(pit2[,input$show_vars, drop = FALSE])

    }

    if(("NO" %in% input$qualify_pit))
    {
      pit <- read.csv("PIT_STAT.csv")
      DT::datatable(pit[,input$show_vars, drop = FALSE])

    }

  })


})

shinyApp(ui = ui, server = server)
James
  • 11
  • 4

1 Answers1

0

It works with NO because the DT:datatable is the last expression of the function, therefore it is the implicit return value.

But for the YES, which is not the last evaluation (the if("NO" %in%... is), you have to explicitly use return:

return(DT::datatable(pit2[,input$show_vars, drop = FALSE]))

Otherwise you can simply use else

  output$pitch_table <- renderDataTable({
    if(("YES" %in% input$qualify_pit))
    {
      DT::datatable(pit[pit$IP >= 162,input$show_vars, drop = FALSE])
    }
    else 
    {
      DT::datatable(pit[,input$show_vars, drop = FALSE])
    }
  })

For more details about using return you can read this thread

Community
  • 1
  • 1
HubertL
  • 19,246
  • 3
  • 32
  • 51