根据选定的日期范围显示可录音的输出

时间:2018-08-01 12:21:08

标签: r shiny rhandsontable

我无法使用日期范围来选择性地显示可录音的输出。运行该应用程序时,未选择日期范围的默认值,这会导致NULL给出错误。

请告知我该如何解决。

library(shiny)
library(lubridate)
library(rhandsontable)


ui <- shinyUI(fluidPage(

    mainPanel(
      uiOutput("date_range"),
      br(),
      rHandsontableOutput("hot")

    )
  )
)


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


  start_date <- as.Date(dmy("01-Jan-2018"), tz = "UTC")
  end_date <- as.Date(dmy("10-Jan-2018"), tz = "UTC")

  DF <- data.frame(Date = seq.Date(start_date, end_date, by = 1),
                   value = 1:10, stringsAsFactors = FALSE)

  output$date_range <- renderUI({
    fluidRow(
      dateRangeInput(inputId = "dates", label = paste0("Enter Date Range"), start = start_date + 1, end = end_date - 1,
                     min = start_date, max = end_date, format = "dd-mm-yyyy")
    )
  })

  DF_selected <- reactive({
    date_vec <-DF$Date
    out <- DF[which((date_vec >= input$dates[1]) & (date_vec <= input$dates[2])),]
    return(out)
  })

  hot_mat <- reactive({
    if(is.null(input$hot)){
      DF = DF_selected()
    }else{
      DF = hot_to_r(input$hot)
    }
    return(DF)
  })

  output$hot <- renderRHandsontable({
    DF_out <- hot_mat()
    rhandsontable(DF_out, useTypes = TRUE) %>% hot_col(col = 1, readOnly = TRUE)
  })

})

runApp(list(ui=ui, server=server))

1 个答案:

答案 0 :(得分:0)

这似乎可行:

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


  start_date <- as.Date(dmy("01-Jan-2018"), tz = "UTC")
  end_date <- as.Date(dmy("10-Jan-2018"), tz = "UTC")

  DF <- data.frame(Date = seq.Date(start_date, end_date, by = 1),
                   value = 1:10, stringsAsFactors = FALSE)

  output$date_range <- renderUI({
    fluidRow(
      dateRangeInput(inputId = "dates", label = paste0("Enter Date Range"), start = start_date + 1, end = end_date - 1,
                     min = start_date, max = end_date, format = "dd-mm-yyyy")
    )
  })

  DF_selected <- reactive({
    date_vec <-DF$Date
    out <- DF[which((date_vec >= input$dates[1]) & (date_vec <= input$dates[2])),]
    return(out)
  })



  output$hot <- renderRHandsontable({
    DF_out <- DF_selected()
    rhandsontable(DF_out, useTypes = TRUE) %>% hot_col(col = 1, readOnly = TRUE)
  })

})

runApp(list(ui=ui, server=server))