dateInput not working on DT in shiny

前端 未结 1 1261
情歌与酒
情歌与酒 2021-01-07 03:52

I\'m trying to create a DT with several inputs in Shiny. The columns with selectInput and numericInput work fine, but the one with dateInputs doesn\'t. It\'s like the input$

相关标签:
1条回答
  • 2021-01-07 04:36

    dateInput

    library(shiny)
    library(DT)
    
    ui <- fluidPage(
    
      # define a hidden dateInput in order that Shiny loads the dependencies
      div(style = "display: none;",
        dateInput("x", label = NULL)
      ),
    
      DTOutput("table")
    )
    
    
    js <- c(
      "function(settings){",
      "  $('#calendar').bsDatepicker({",
      "    format: 'yyyy-mm-dd',",
      "    todayHighlight: true",
      "  });",
      "}"
    ) # available options: https://bootstrap-datepicker.readthedocs.io/en/latest/
    
    
    server <- function(input, output, session){
    
      output[["table"]] <- renderDT({
        dat <- data.frame(
          V1 = "A",
          V2 = 99, 
          V3 = '<input id="calendar" type="text" class="form-control" value = "2019-03-08"/>',
          stringsAsFactors = FALSE
        )
        datatable(dat, escape = FALSE,
                  options = 
                    list(
                      initComplete = JS(js),
                      preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                      drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
                    )
        )
      })
    
    }
    
    
    shinyApp(ui, server)
    

    dateRangeInput

    html <- '
    <div class="input-daterange input-group" id="calendar">
        <input type="text" class="input-sm form-control" name="start" value = "2019-03-08" />
        <span class="input-group-addon">to</span>
        <input type="text" class="input-sm form-control" name="end" value = "2019-03-12" />
    </div>'
    
      output[["table"]] <- renderDT({
        dat <- data.frame(
          V1 = "A",
          V2 = 99, 
          V3 = html,
          stringsAsFactors = FALSE
        )
        datatable(dat, escape = FALSE,
                  options = 
                    list(
                      initComplete = JS(js),
                      preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                      drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
                    )
        )
      })
    
    0 讨论(0)
提交回复
热议问题