dateInput在光亮的DT上没有工作 [英] dateInput not working on DT in shiny

查看:100
本文介绍了dateInput在光亮的DT上没有工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Shiny中的几个输入创建DT。 selectInput和numericInput的列工作正常,但是dateInputs的列没有。这就像输入$ date_n根本不存在。

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$date_n does not exist at all.

我对JS几乎一无所知,我认为问题在于drawCallback。我刚从这里的另一个问题中复制了这段代码,并且在我尝试使用dateInput之前它工作正常。

I know close to nothing about JS, and I think the problem is probbaly in the drawCallback. I just copied this piece of code from another question in here, and it was working fine until I tried to use a dateInput.

下面,有一个小代码重现问题。前两个输出都没问题,但第三个输出正好显示出来。

Below, there's a small code reproducing the problem. The first two outputs are OK, but the third just doesnst show up.

library(shiny)
library(DT)

ui = fluidPage(title = 'Test',
               DTOutput('table'),
               verbatimTextOutput('chosen_letter'),
               verbatimTextOutput('chosen_value'),
               verbatimTextOutput('chosen_date'))

server <- shinyServer(function(input, output, session) {
output$table = renderDT({

sel_letter = paste0("<select id='letter_", 1:3, "'>
                    <option value='a' selected='selected'>A</option>
                    <option value='b'>B</option>
                    <option value='c'>C</option>
                    </select>")

sel_value = paste0("<input id='value_", 1:3, "' class='shiny-bound-input' 
                    value = '", 1:3, "' type='number' step = '1'>")

sel_date = paste0("<input id='date_", 1:3, "' type='date' value='2018-07-31'
                   class='shiny-bound-input' min='2018-07-31' max='2018-12-31'>")

datatable(data.frame(Letter = sel_letter, Value = sel_value, Date = sel_date), 
          rownames = F, selection = 'none', escape = F,
          options = list(drawCallback = JS('function(settings) {
                                           Shiny.bindAll(this.api().table().node());}'),
                         dom = 't', ordering = F, pageLength = 15))
})

output$chosen_letter = renderText({c(input$letter_1, input$letter_2, input$letter_3)})

output$chosen_value = renderText({sum(input$value_1, input$value_2, input$value_3)})

output$chosen_date = renderText({input$date_1})

})

shinyApp(ui, server)


推荐答案

dateInput



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



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()); } ')
                )
    )
  })

这篇关于dateInput在光亮的DT上没有工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆