r - R Shiny:基于反应数据框的 updateSelectInput
问题描述
我有一个闪亮的应用程序,用户根据他想要查看的文章过滤我的数据集的文章列。然后将这些文章显示在表格中。文章通过单击自定义功能作为操作按钮做出反应。我希望每当用户点击某篇文章时,该文章都会被选中selectInput
。尽管如此,我不知道将哪个值传递selected
给updateSelectInput
.
我在卡住的地方打了三个问号。通过删除三个问号,代码是可执行的。任何帮助表示赞赏
library(shiny)
library(tidyverse)
library(kableExtra)
library(formattable)
df = tibble(article=c("one", "two", "three", "four", "five", "six"),
group=c("a", "a", "a", "b", "b", "b"),
sales=c(12,13,14,43,50,45))
ui = fluidPage(
sidebarPanel(
radioButtons(inputId = "select_a", label = "Choose a group", choices = unique(df$group), selected = "a"),
htmlOutput(outputId = "table")),
sidebarPanel(
selectInput(inputId = "select_b", label = "Choose an article", choices = df$article, selected = "one")
)
)
server = function(input, output, session){
shinyInput <- function(FUN, len, id, labels, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), label = labels[i], ...))
}
inputs
}
df_reactive = reactive({
df %>% filter(group == input$select_a) %>%
mutate(article = shinyInput(actionButton, n(), 'button_', labels = article, onclick = 'Shiny.onInputChange(\"select_button\", this.id)'))
})
output$table = function(){
df_reactive() %>%
kable("html", escape = F, align = "c") %>%
kable_styling(bootstrap_options = c("striped", "condensed", "responsive"), full_width = F, position = "center") %>%
scroll_box(width = "100%", height = "auto")
}
observeEvent(input$select_button, {
updateSelectInput(session = session, inputId = "select_b", selected = ???)
})
}
shinyApp(ui = ui, server = server)
解决方案
也许您可以this.innerText
在这里检索文章:
mutate(article = shinyInput(actionButton, n(), 'button_', labels = article,
onclick = 'Shiny.onInputChange(\"select_button\", this.innerText)'))
然后input$select_button
将包含文本字符串select
:
updateSelectInput(session = session, inputId = "select_b", selected = input$select_button)
推荐阅读
- c# - C# - 如何从列表中的位图上绘制形状?
- c# - Timer Tick 不增加时间间隔的值
- google-apps-script - 将条件格式应用于驱动器中谷歌工作表中文件夹中的所有工作表
- node.js - 如何在 Apache2 的 Ubuntu LTS 18 中部署 NodeJS Rest API
- python-2.7 - 如何创建两个文件的组合组合
- python - Emacs flycheck python false postive with print()
- consul - 领事执行官不做任何事情
- mysql - 输入字段中的文本值未存储在 PHP 数据库中
- testing - 如何使用量角器在非角度网站中测试日期选择器
- git - 如何恢复对分支的提交,将它们保留在自己的分支中?