我想创建一个如下所示的选项,
<select id="species">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
Run Code Online (Sandbox Code Playgroud)
所以我使用数据框来创建一个存储数据的表,
# 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)
Run Code Online (Sandbox Code Playgroud)
闪亮的ui.R,
selectInput(inputId = "species",
label = "Species:",
choices = choicesSpecies),
Run Code Online (Sandbox Code Playgroud)
我收到这个错误,
Error in (function (choice, name) :
All sub-lists in "choices" must be named.
Run Code Online (Sandbox Code Playgroud)
这是什么意思?如何修复它以获得我需要的结果?
将code列作为数据框中的一个因素似乎是个问题,也许可以尝试:
choicesSpecies <- setNames(as.numeric(species$code), species$title)
Run Code Online (Sandbox Code Playgroud)
要么:
#create the named list
title <- c('A', 'B', 'C')
code <- c('1', '2', '3')
names(code) <- title
Run Code Online (Sandbox Code Playgroud)
在你的ui.R:
selectInput(inputId = "species",
label = "Species:",
choices = code)
Run Code Online (Sandbox Code Playgroud)