如何从UpdateSelectInput的选项中移除指定项?
解决方案
当然不用手动列出所有7个选项,直接在第一段代码的基础上对race_ethnicity的结果做过滤就能移除"Not Reported",这样既省事又能保证后续新增种族选项时自动适配。
你可以这样修改代码:
raw_df_filter <- raw_df # 先提取去重排序后的种族选项,再排除"Not Reported" race_options <- unique(sort(raw_df_filter$race_ethnicity)) race_options <- race_options[race_options != "Not Reported"] updateSelectInput(inputId = "race", choices = c(not_sel, race_options))
或者写成更紧凑的一行版本:
raw_df_filter <- raw_df updateSelectInput(inputId = "race", choices = c(not_sel, unique(sort(raw_df_filter$race_ethnicity))[unique(sort(raw_df_filter$race_ethnicity)) != "Not Reported"]))
原理很简单:先通过unique(sort())得到有序且无重复的种族列表,再用!= "Not Reported"筛选掉不需要的选项,最后和not_sel拼接成最终的下拉选择项。
内容的提问来源于stack exchange,提问作者Shannon
相关产品推荐
相关产品推荐

