R Shiny:基于反应数据帧更新SelectInput



我有一个闪亮的应用程序,用户可以根据他想看的文章来过滤我数据集的文章列。然后将这些文章显示在表格中。文章的反应就像操作按钮,点击后会有一个自定义功能。我希望每当用户点击某篇文章时,这篇文章都会在selectInput中被选中。然而,我不知道该将哪个值传递给updateSelectInputselected属性。

我在被卡住的地方打了三个问号。通过去掉这三个问号,代码是可执行的。感谢的任何帮助

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) 

最新更新