Subset the data displayed in a histogram by using a shiny numericInput()


#1

Hello I have a simple shiny app which I want to use like this: When the user chooses "INT" or "DOM" from the checkbox group the numeric input is affected as the max number that it can take is the max number or "DOM" OR "INT". What I want to do is specify the number of data displayed in the histogram by using this numericInput(). So if for example the user choose DOMESTIC from the checkbox the numericInput should take values from 1 to 3 and the histogram should only dsplay the first available row if I choose 1 the first 2 if I choose 2 and so on.

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"                  ) 
testdata = data.frame(OriginId,RequestedDtTm)  

## ui.R ##
library(shinydashboard)
library(plotly)

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

library(shinydashboard)

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) {



  output$plot1 <- renderPlotly({
    data<-table(subset(testdata[,2],testdata$OriginId %in% input$checkGroup2))
    p <- plot_ly(x = data[1:input$n1], type = "histogram")

  })


  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(nrow(subset(testdata,testdata$OriginId %in% input$checkGroup2))))
  })

    }