Specify the number of selected rows in data frame with a numericInput() and display them accordinly in plots


#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 and in the line graph by using this numericInput(). So if for example the user chooses DOMESTIC from the checkbox the numericInput should take values from 1 to 3 and the histogram should only display the first available row if I choose 1 the first 2 if I choose 2 and so on. The problem is that when the user chooses "INT' or 'DOM' some rows are excluded and I think that this creatas a misfunctioanlity in my application. For example the line graph works only if I choose bot 'DOM' and 'INT'.

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"                  )
ClientZIP=c(20000,24455,56000,45000,80000,45000)
ClientName=c("test1","test2","test3","test4","test5","test6")
testdata = data.frame(OriginId,RequestedDtTm,ClientZIP,ClientName)



 ## 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("pie",height = 250)),

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

                  ),
                  fluidRow(
                    box(plotlyOutput("plot2", height = 250)),
                    box(plotlyOutput("plot3",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")

                    )
                  )),
                  fluidRow(
                    box(title="Line Graph",width = 12,
                        column(4,

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

                        ),
                        column(4,
                               uiOutput("n3")

                        )
                    ))
          )

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



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

  })

  output$plot3 <- renderPlotly({

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

    date_start <- as.character(input$dateRange3[1])
    date_end <- as.character(input$dateRange3[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, ]

    p <- plot_ly(data, x = testdata$RequestedDtTm, y = ~ClientZIP, type = 'scatter', mode = 'lines')
  })
  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$dt3<-renderUI({
  dateRangeInput('dateRange3',
                 label = 'Date range',
                 start = min(subset(as.POSIXct(testdata$RequestedDtTm),testdata$OriginId %in% input$checkGroup4)), end = max(subset(as.POSIXct(testdata$RequestedDtTm),testdata$OriginId %in% input$checkGroup4))
  )
  })
  output$n1<-renderUI({
  numericInput("num", label = h3("Numeric input"), value = 1, min = 1,max = max(nrow(subset(testdata,testdata$OriginId %in% input$checkGroup2))))
  })

  output$n3<-renderUI({
    numericInput("num3", label = h3("Numeric input"), value = 1, min = 1,max = max(nrow(subset(testdata,testdata$OriginId %in% input$checkGroup4))))
  })
    }