Display columns of bar chart based on their rank which is choosen by a numericInput()


#1

Hello I have shiny app which creates a bar chart with the count of "ClientName". This bar chart takes as inputs the "OriginId", the "RequestedDT" and the ranking of the counts of "ClientName". This ranking is included in a numericInput. What I want to achieve is when I choose "1" only the top "ClientName" to be displayed. When I choose 2 the top 2 and so on.

#dataframe
     OriginId = c("INT", "DOM", "INT","DOM","INT","DOM") 
        RequestedDtTm = c("2017-01-16 16:43:33
        ", "2017-01-17 16:43:33
        ", "2017-01-18 16:43:33
        ","2017-01-19 16:43:33",
                          "2017-01-18 16:43:33
        ","2017-01-19 16:43:33"                  )

        ClientName=c("test1","test2","test3","test4","test5","test6")
        testdata = data.frame(OriginId,RequestedDtTm,ClientName)  
#app    
    ## ui.R ##
    library(shinydashboard)
    library(plotly)

    dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody()
    )


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

      ## Sidebar content
      dashboardSidebar(
        sidebarMenu(
          menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
          menuItem("Change View", tabName = "widgets", icon = icon("th"))
        )
      ),

      ## Body content
      dashboardBody(
        tabItems(
          # First tab content
          tabItem(tabName = "dashboard",
                  fluidRow(

                    box(
                      plotlyOutput("plot1", height = 250)
                    )

                  )

          ),

          # Second tab content
          tabItem(tabName = "widgets",
                  fluidRow(
                    box(title="Histogram 1",width = 12,
                        column(4,

                               checkboxGroupInput("checkGroup2", label = h3("Checkbox group"), 
                                                  choices = list("Show Domestic" = "DOM", "Show International" = "INT"),
                                                  selected = "DOM")
                        ),
                        column(4,
                               uiOutput("dt1")

                        ),
                        column(4,
                               uiOutput("n1")

                        )))

          )

        )
      )
    )
    #server.r
    server <- function(input, output) {

      rt1<-reactive({
        data <- dplyr::tbl_df(subset(testdata[,c(2,3)],testdata$OriginId %in% input$checkGroup2))

        date_start <- as.character(input$dateRange[1])
        date_end <- as.character(input$dateRange[2])

        data$RequestedDtTm <- as.Date(data$RequestedDtTm, format = "%Y-%m-%d")
        # data <- data %>% filter(RequestedDtTm >= date_start & RequestedDtTm <= date_end)
        data <- data[as.Date(data$RequestedDtTm) >= date_start & as.Date(data$RequestedDtTm) <= date_end, ]

        df<-as.data.frame(table(data$ClientName))
        df$rank <- NA
        order.scores<-order(df$Freq,df$Var1)
        df$rank[order.scores] <- 1:nrow(df) 
        df
      })
      output$plot1 <- renderPlotly({
        subset(rt1(),as.numeric(rt1()[,3]) %in% input$num)
        p <- plot_ly(rt1(), x = rt1()[,1], y = rt1()[,2], type = 'bar', name = 'Clients', marker = list(color = 'rgb(49,130,189)'))

      })


      output$dt1<-renderUI({

      dateRangeInput('dateRange',
                     label = 'Date range',
                     start = min(subset(as.POSIXct(testdata$RequestedDtTm),testdata$OriginId %in% input$checkGroup2)), end = max(subset(as.POSIXct(testdata$RequestedDtTm),testdata$OriginId %in% input$checkGroup2))
      )
      })
      output$n1<-renderUI({
      numericInput("num", label = h3("Numeric input"), value = 1, min = 1,max = max(as.numeric(rt1()[,3])))
      })
        }