【问题标题】:selectizeinput reading column-name instead of valueselectizeinput 读取列名而不是值
【发布时间】:2020-07-15 18:17:53
【问题描述】:

有人可以向我解释这种行为吗? 在 R 中,如果列表 l[[i]] 仅包含一个元素,则 selectizeinput 仅显示列名而不是元素。如果有更多元素,它不包括列名。

在控制台中,这两个之间的行为也不同:

> l[[1]][,'value']

            value 
    "some string" 


> l[[2]][,'value2']
[1] "more string"  "other string"

library(shiny)

l <- c()

value <- "some string"
id <- "1"
df <- cbind(id,value)
l[[1]] <- df

value2 <- c("more string", "other string")
id2 <- c("2","3")
df2 <- cbind(id2,value2)
l[[2]] <- df2

ui <- fluidPage(
  selectizeInput("input", "selectize 1", l[[1]][,'value'], multiple=TRUE),
  verbatimTextOutput("out"),
  selectizeInput("input2", "selectize 2", l[[2]][,'value2'], multiple=TRUE),
  verbatimTextOutput("out2")
)

server <- function(input,output){
  output$out <- renderText({
    input$input
  })
  output$out2 <- renderText({
    input$input2
  })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny selectinput


    【解决方案1】:

    我不确定为什么会发生这种情况的全部细节,但是您的第一个矩阵将其值存储为 Named chr 而不是 chr,如下所示:

    > str(l[[1]][, 'value'])
     Named chr "some string"
     - attr(*, "names")= chr "value"
    > str(l[[2]][, 'value2'])
     chr [1:2] "more string" "other string"
    

    您可以通过将第一个矩阵读取为向量来解决此问题,该向量在 ui 中如下所示:

    ...
    selectizeInput("input", "selectize 1", as.vector(l[[1]][,'value']), multiple=TRUE),
    ...
    

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多