0

我无法使用日期范围来选择性地显示 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))
4

1 回答 1

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))
于 2018-08-21T17:04:38.750 回答