Count the number of data frame values or rows for every month based on dateRangeInput()


#1

Hello I have a shiny app which after being subseted with a checkbox it creates a line graph for the specific date range set by dateRangeInput(). The x axis should include the date (year and month) while the y-axis the count of "ClientName" for every month. Foe example January of 2016 should have 2 values, March of 2016 one value and so on. Of course if I choose the date range to begin from February, January should be exluded from the line graph.

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

ClientName=c("test1","test2","test3","test1","test1","test2")
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("plot3",height = 250))

              )
      ),

      # Second tab content
      tabItem(tabName = "widgets",
              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")

                    )

                ))
      )

    )
  )
)
#server.r
server <- function(input, output) {
  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")
    data <- data[as.Date(data$RequestedDtTm) >= date_start & as.Date(data$RequestedDtTm) <= date_end, ]

    p <- plot_ly(data, x = format(as.Date(data$RequestedDtTm)), y = nrow(data$ClientName), type = 'scatter', mode = 'lines')
  })

  output$dt3<-renderUI({
  dateRangeInput('dateRange3',
                 label = 'Date range',
                 format = "yyyy-mm",
                 start = min(subset(as.POSIXct(testdata$RequestedDtTm),testdata$OriginId %in% input$checkGroup4)), end = max(subset(as.POSIXct(testdata$RequestedDtTm),testdata$OriginId %in% input$checkGroup4))
  )
  })
    }