【问题标题】:Shiny - All sub-lists in "choices" must be named?Shiny - “选择”中的所有子列表都必须命名?
【发布时间】:2015-05-04 04:40:09
【问题描述】:

我想创建一个如下所示的选择选项,

<select id="species">
  <option value="1">A</option>
  <option value="2">B</option>
  <option value="3">C</option>
</select>

所以我使用数据框来创建一个存储数据的表,

# Create the species table for select input.
title <- c('A', 'B', 'C')
code <- c('1', '2', '3')
species <- data.frame(title, code)

# Set choices of title and code for select input.
choicesSpecies <- setNames(species$code, species$title)

Shiny 的 ui.R,

selectInput(inputId = "species",
                  label = "Species:",
                  choices = choicesSpecies),

我收到此错误,

Error in (function (choice, name)  : 
  All sub-lists in "choices" must be named.

这是什么意思?如何修复它以获得我需要的结果?

【问题讨论】:

  • 您可以尝试使用choices=species$code,然后使用switch 语句来获取等效代码吗?
  • 以这种方式设置名称时是否也会发生错误(使用这种方法时我从未遇到错误):choiceSpecies
  • 谢谢大家。但恐怕这些建议都不起作用......

标签: r shiny


【解决方案1】:

code 列作为数据框中的一个因素似乎是问题所在,也许可以尝试:

choicesSpecies <- setNames(as.numeric(species$code), species$title)

或者:

#create the named list
title <- c('A', 'B', 'C')
code <- c('1', '2', '3')
names(code) <- title

在你的ui.R:

selectInput(inputId = "species",
                label = "Species:",
                choices = code) 

【讨论】:

  • 谢谢尼斯。它适用于choicesSpecies &lt;- setNames(as.numeric(species$code), species$title),但如果我在我的代码变量中包含此数据,我会得到同样的错误 - code &lt;- c('a', 'b', 'c')...
【解决方案2】:

回应您的评论(没有足够的代表回复):

code <- c('a','b','c')

您需要将它们更改为字符,就像将它们更改为数字一样。 例如

choicesSpecies <- setNames(as.character(species$code), species$title)

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 2013-09-13
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多