我正在尝试在selectInput()
中创建选项组,但会看到以下意外结果。
如果您在控制台中键入以下行:
selectInput("test", "I am test", choices = list("Group A" = c("a", "b", "c"), "Group B" = "d"))
您应该看到以下输出:
<div class="form-group shiny-input-container">
<label class="control-label" for="test">I am test</label>
<div>
<select id="test">
<optgroup label="Group A">
<option value="a" selected>a</option>
<option value="b">b</option>
<option value="c">c</option>
</optgroup>
<option value="d">Group B</option> <====== wrong
</select>
<script type="application/json" data-for="test" data-nonempty="">{}</script>
</div>
</div>
上述代码的问题是Group B
应该是optgroup label
而不是option text
。我想上面的代码应该返回以下内容:
<div class="form-group shiny-input-container">
<label class="control-label" for="test">I am test</label>
<div>
<select id="test">
<optgroup label="Group A">
<option value="a" selected>a</option>
<option value="b">b</option>
<option value="c">c</option>
</optgroup>
<optgroup label="Group B"> <====== correct
<option value="d">d</option> <====== correct
</optgroup> <====== correct
</select>
<script type="application/json" data-for="test" data-nonempty="">{}</script>
</div>
</div>
这是预期的吗?如果没有,我该如何解决?
答案 0 :(得分:2)
您可以将Group B
选项放在列表中以获得所需的输出:
selectInput("test", "I am test", choices = list("Group A" = c("a", "b", "c"), "Group B" = list("d")))