【问题标题】:Datatable displays empy selectInput while values are selected by defaultDatatable 显示 empy selectInput 而默认选择值
【发布时间】:2021-04-16 12:23:28
【问题描述】:

我在下面有一个闪亮的应用程序,在其中我通过selectImput() 内的字符传递了一个列表的值,但是虽然所有这些值似乎都被选中(而且它们应该是)通过检查它们在第三列中的计数来选择输入似乎是空的。我认为对于这个问题,我创建的列表words 负责。

library(shiny)
library(DT)
library(jsonlite)

selector <- function(id, values, items = values){
  options <- HTML(paste0(mapply(
    function(value, item){
      as.character(tags$option(value = value, item))
    }, c("", values), c("", items)
  ), collapse = ""))
  as.character(
    tags$select(
      id = id, class = "form-control", multiple = "multiple", options
    )
  )
}

name<-c("Jack","Bob","Jack","Bob")
item<-c("apple","olive","banana","tomato")
d<-data.frame(name,item,stringsAsFactors = FALSE)

words<-tapply(d$item, d$name, I)


nrows <- length(words)

js <- c(
  "function(settings) {",
  sprintf("var nrows = %d;", nrows),
  sprintf("var words = %s;", toJSON(words)),
  "  var table = this.api().table();",
  "  function selectize(i) {",
  "    $('#slct' + i).selectize({",
  "      items: words[i-1],",
  "      onChange: function(value) {",
  "        table.cell(i-1, 2).data(value.length);",
  "      }",
  "    });",
  "  }",
  "  for(var i = 1; i <= nrows; i++) {",
  "    selectize(i);",
  "    Shiny.setInputValue('slct' + i, words[i-1]);",
  "  }",
  "}"
)

ui <- fluidPage(
  br(),
  DTOutput("table"),
  div( # this is a hidden selectize input whose role is to make
    # available 'selectize.js'
    style = "display: none;",
    selectInput("id", "label", c("x", "y"))
  )
)

server <- function(input, output, session) {
  
  output[["table"]] <- renderDT({
    dat <- data.frame(
      FOO = c(unique(d$name)),
      Words = vapply(
        1:nrows,
        function(i){
          selector(paste0("slct", i), words[[i]])
        },
        character(1)
      ),
      Count = lengths(words),
      stringsAsFactors = FALSE
    )
    
    datatable(
      data = dat,
      selection = "none",
      escape = FALSE,
      rownames = FALSE,
      options = list(
        initComplete = JS(js),
        preDrawCallback = JS(
          'function() { Shiny.unbindAll(this.api().table().node()); }'
        ),
        drawCallback = JS(
          'function() { Shiny.bindAll(this.api().table().node()); }'
        )
      )
    )
  }, server = FALSE)
  
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r list shiny dt


    【解决方案1】:

    words 列表命名为:

    > name <- c("Jack","Bob","Jack","Bob")
    > item <- c("apple","olive","banana","tomato")
    > d <- data.frame(name, item)
    > 
    > ( words <- tapply(d$item, d$name, I) )
    $Bob
    [1] olive  tomato
    Levels: apple banana olive tomato
    
    $Jack
    [1] apple  banana
    Levels: apple banana olive tomato
    

    因此它的 JSON 表示是:

    > toJSON(words)
    {"Bob":["olive","tomato"],"Jack":["apple","banana"]} 
    

    这不是一个数组。删除名称,您将获得所需的数组数组:

    > toJSON(unname(words))
    [["olive","tomato"],["apple","banana"]] 
    

    或者不使用“jsonlite”,而是使用基本的 JSON 字符串化器:

    sprintf("[%s]", toString(vapply(words, function(x){
      sprintf("[%s]", toString(shQuote(x)))
    }, character(1))))
    # "[['olive', 'tomato'], ['apple', 'banana']]"
    

    【讨论】:

    • 感谢第一次启动应用程序时,第二列 selectize 中的输入似乎仍然为空
    • 在哪部分代码中设置了在selectInput()里面默认选择并显示所有的值?
    • @firmo23 抱歉,问题不在于因素,而在于名称;看我的编辑。关于您的问题,这是selectize 中的items 选项(在JS 代码中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2018-04-10
    • 2021-07-10
    • 2017-02-12
    • 1970-01-01
    • 2014-08-02
    • 2019-01-20
    相关资源
    最近更新 更多