具有HTML UI的Shiny R中下拉菜单的动态选项 [英] Dynamic Options For Dropdown Menu in Shiny R With HTML UI
问题描述
我正在尝试按照 http ://shiny.rstudio.com/articles/html-ui.html 我不确定如何将ui.r中的以下代码转换为HTML.
I'm trying to convert a ui.r into an HTML ui in Shiny following http://shiny.rstudio.com/articles/html-ui.html I'm not sure how to translate the following code from my ui.r into HTML.
我有一个使用以下代码的下拉列表
I have a dropdown that uses the following code
selectInput("data","Choose A Section:", choices=Sections[,2])
下拉菜单中的输入是根据服务器上加载的section变量生成的. section变量可能会不时更改.
The inputs in the dropdown are generated based on a sections variable that is loaded on my server. The sections variable may change from time to time.
I know I could paste in all of the sections and create a dropdown used in the example ui http://rstudio.github.io/shiny/tutorial/#html-ui
<label>Sections:</label><br />
<select name="dist">
<option value="Section1">Section1</option>
<option value="Section2">Section2</option>
<option value="Section3">Section3</option>
<option value="Section4">Section4</option>
</select>
但是我不确定如何为数据更改而可能会更改的选项设置它.是否有捷径可寻?
But I'm not sure how to set it up for options that may change if the data changes. Is there an easy way to do this?
推荐答案
虽然这不是最佳答案,但它可能会让您入门.在Shiny中,我上传了.csv文件,该文件将使用已上传文件的标题名称动态更新下拉菜单.
While this isn't the best answer, it may get you started. In Shiny, I upload .csv files, which will dynamically update the dropdown menu with the header names of the uploaded files.
在server.R中,我包含
observe({
infile <- input$datfile
print(infile)
if(is.null(infile))
return(NULL)
d <- read.csv(infile$datapath, header = T)
updateSelectInput(session, 'dropdown_1', choices = names(d))
updateSelectInput(session, 'dropdown_2', choices = names(d))
})
在ui.R中,我包括
selectInput('dropdown_1', '', ''),
selectInput('dropdown_2', '', '')
只要您可以指向数据源,我就认为逻辑应该成立.例如,这将在data_set
的column1
字段中获取唯一项.
As long as you can point to a data source, I'm thinking the logic should hold. For instance, this would grab the unique items in the field named column1
of data_set
.
observe({
data_set <- xxxxx
updateSelectInput(session, 'dropdown_menu', choices = unique(data_set$column1))
})
这篇关于具有HTML UI的Shiny R中下拉菜单的动态选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!