0

I have a population and area-type columns in the data table where I want the user to do conditional filtering (say select population>15000 & area-type is planned). I am using a reactive function, the areatype is working while the population isn't reactive. I am thinking of using the observe function but I don't know. My question is how to make both reactive & include dependency. Code:

    #UI 
    ui<-fluidpage(checkboxGroupInput(inputId = "popdensity", 
                                     label = " Population Per Km2:",
                                     choices = list(
                                       "< 15,000"=1, 
                                       "15,001 - 30,000"=2 , 
                                       ">30,000"=3
                                     ), 
                                     selected =list(1,2,3)
                                     ),


    selectInput(inputId = "area", 
                              label = " AreaType:",
                              choices = c(
                                "All",

                                unique(as.character(nakuru$AreaType))
                              ) 
                  ))

    #SERVER
server<-function(input,output)({
    output$table<-DT::renderDataTable({

        #filtering based on user selection

        dt<-reactive({
        df<-nakuru
         if(input$area!="All"){

          df<-df[df$AreaType==input$area,]

         }
        if(input$popdensity==1){
          df[df$PopDensity<=15000,]
        }
         if(input$popdensity==2){
          df[df$PopDensity>15000&df$PopDensity<=30000,]
        }
       if(input$popdensity==3){
          df[df$PopDensity>30000,]
        }

        df
        })
        DT::datatable(dt(),options = list(scrollX=TRUE))
      })
})

Nakuru data SHINY APP LAYOUT IMAGE

brian
  • 75
  • 1
  • 8
  • what's the output you are hoping for? – Shinobi_Atobe Aug 24 '18 at 08:34
  • Say if a user selects population >15000 & areatype==planned, the columns are filtered based on the user selection & output displayed on the data table. – brian Aug 24 '18 at 08:44
  • Hi @brian, please see [here](https://stackoverflow.com/questions/48343080/how-to-convert-a-shiny-app-consisting-of-multiple-files-into-an-easily-shareable) for tips on how to create a MCVE for Shiny. That will make it a lot easier to answer your question. – Florian Aug 24 '18 at 09:59

2 Answers2

0

It is difficult to understand the problem without a minimum reproducible example. But perhaps try this. Bring the reactive part outside of the renderDatatable portion

dt<-reactive({
    df<-nakuru
     if(input$area!="All"){

      df<-df[df$AreaType==input$area,]

     }
    if(input$popdensity==1){
      df[df$PopDensity<=15000,]
    }
     if(input$popdensity==2){
      df[df$PopDensity>15000&df$PopDensity<=30000,]
    }
   if(input$popdensity==3){
      df[df$PopDensity>30000,]
    }

    df
    })

output$table<-DT::renderDataTable({
DT::datatable(dt(),options = list(scrollX=TRUE))
  })
ashleych
  • 1,042
  • 8
  • 25
0

Please see if this solves the issue. You really have to provide a minimum reproducible example, including the dataset using dput

library(shiny)
library(dplyr)

ui<-fluidPage(checkboxGroupInput(inputId = "popdensity",
                                 label = " Population Per Km2:",
                                 choices = list(
                                   "< 15,000"=1,
                                   "15,001 - 30,000"=2 ,
                                   ">30,000"=3
                                 ),
                                 selected =list(2,3)
),

selectInput(inputId = "area",
            label = " AreaType:",
            choices = c(
              "All",unique(as.character(nakuru$AreaType)))
),
dataTableOutput("filtered_table")
)

#SERVER
server<-function(input,output){

  dt<-reactive({
    df<-nakuru
    print(head(df))
    if(input$area!="All"){
      df<-df[df$AreaType==input$area,]

    }
    str(df)
    df1<- df %>%  filter(Populationperkm2 < 15000)

    df2<- df %>%  filter(Populationperkm2 > 15000 & Populationperkm2<=30000)
    df3<- df %>%  filter(Populationperkm2>30000)
    temp<- data.frame()

    if(1 %in% input$popdensity ){

      temp<-rbind(temp,df1)

    }

    if("2" %in% input$popdensity){

      temp<-rbind(temp,df2)
    }
    if("3" %in% input$popdensity){
      temp<-rbind(temp,df3)
    }
 temp
  })
  output$filtered_table<-DT::renderDataTable({
    DT::datatable(dt(),options = list(scrollX=TRUE))
  })
}

shinyApp(ui, server)
ashleych
  • 1,042
  • 8
  • 25