Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 默认情况下选择值时,Datatable显示empy selectInput_R_List_Shiny_Dt - Fatal编程技术网

R 默认情况下选择值时,Datatable显示empy selectInput

R 默认情况下选择值时,Datatable显示empy selectInput,r,list,shiny,dt,R,List,Shiny,Dt,我有一个闪亮的应用程序,我在下面的应用程序中传递一个列表的值,其中包含一个selectImput()中的字符,但是当所有这些值似乎都是通过检查第三列中的计数来选择的(它们应该是)时,selectize输入似乎是空的。我认为我创建的列表words应对这个问题负责 library(shiny) library(DT) library(jsonlite) selector <- function(id, values, items = values){ options <- HTML

我有一个闪亮的应用程序,我在下面的应用程序中传递一个列表的值,其中包含一个
selectImput()
中的字符,但是当所有这些值似乎都是通过检查第三列中的计数来选择的(它们应该是)时,selectize输入似乎是空的。我认为我创建的列表
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)
库(闪亮)
图书馆(DT)
图书馆(jsonlite)

选择器将
单词列表命名为:

> 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
这不是一个数组。删除这些名称,即可获得所需的数组:

> toJSON(unname(words))
[["olive","tomato"],["apple","banana"]] 
或者使用基本的JSON字符串生成器,而不是使用“jsonlite”:

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

感谢第一次启动应用程序时,第二列selectize中的输入似乎仍然为空,其中部分代码设置为默认情况下应在selectInput()中选择并显示所有值?@firmo23抱歉,问题不在于因素,而在于名称;请参阅我的编辑。关于您的问题,这是
选择
(JS代码中)中的
选项。
sprintf("[%s]", toString(vapply(words, function(x){
  sprintf("[%s]", toString(shQuote(x)))
}, character(1))))
# "[['olive', 'tomato'], ['apple', 'banana']]"