0
votes

In the below code, I am attempting to create an input to show all of my markets, or just a selection within a plot and a data table. I am doing this through, or attempting, through ifelse statements within my render functions, however I am getting errors, and neither the plot or data table will render. They do render without the if else statements. I have included an Example data set to hopefully help place in context.

ui <- dashboardPage(
dashboardHeader(title = "Example"),
dashboardSidebar(
  sidebarMenu(
    menuItem("Dashboard", tabName = "Dashboard"),
    menuItem("Example", tabName = "example"))),
 dashboardBody(
    tabItems(
      tabItem(tabName = "Dashboard",
              fluidRow(
                valueBoxOutput("example"))),
    tabItem(tabName = "example",
            fluidRow(
              box(title = "Example",
                  plotOutput("plotexample"), width = 12),
              box(title = "Data Selection",
                  selectInput("market","Market(s): ", c(unique(data$marketnum),"All"),multiple = T, selectize = T, selected = "All"))),
fluidRow(
    box(DT::dataTableOutput("markettable"), width = 12))))))


server <- function(input,output) {
 ExampleAllMarkets <- reactive({
     ExampleData %>%
       group_by(Event,marketnum) %>%
       summarise(ItemCount = n_distinct(ItemNumber))
})

  Example <- reactive({
     ExampleData %>%
       filter(marketnum == input$market) %>%
       group_by(Event,marketnum) %>%
       summarise(PolicyCount = n_distinct(Policy_Number_9_Digit))

  })


  output$example <- renderValueBox({
   valueBox(
   paste0("44", "%"), "example", icon = icon("car"),
   color = "red"
   )
  })

I am placing ifelse statements within my render blocks reactive to whether or not "All" is selected.

  output$plotexample <- renderPlot({
    ifelse(input$market=="All",
    ggplot(Example(), aes(x=MBC_Number, y=ItemCount)) +
      geom_bar(stat="identity"),
    ggplot(ExampleAllMarkets(), aes(x=marketnum, y=ItemCount)) 
 +
      geom_bar(stat="identity"))


  })


  output$markettable <- DT::renderDataTable({
    ifelse(input$market == "All",
    ExampleAllMarkets(),
    Example())

   })

 }


shinyApp(ui,server)

Example Data in csv format

marketnum,ItemNumber 2,118 7,101 1,109 2,109 10,101 4,102 8,100 12,103 5,106 13,116 5,112 10,103 7,113 9,114 10,112 6,114 2,116 11,113 3,107 13,102 8,107 10,109 12,110 1,120 4,106 8,116 2,112 2,106 11,101 6,108 11,107 10,111 6,120 10,118 11,119 13,117

1

1 Answers

0
votes

You probably cannot use ifelse in this scenario.

Analyzing the source code for ifelse, since a plot object is not so simple, it does not just return the plot itself, but

rep(plot, length.out = 1) 

or equivalently plot[1] which is just the dataset of the plot. A plot object has a length > 1 and for those, ifelse only returns its first element.

This can be easily confirmed by evaluating

> ifelse(T, c(1, 2), c(3, 4))
 [1] 1

So the render function cannot draw anything, since it's input is just this dataset.

You will simply have to use the regular if else.