r - updateTabsetPanel 和 updateSelectINput 与 htmlOutput
问题描述
我得到了这个带有 textInput 和 htmlOutput 的闪亮应用程序。用户想要查找一篇文章并将文章的名称写入文本字段。只要文章在我的数据集中,文章+一些信息就会在 htmlOutput 中显示为表格。
我想要实现的是,每当来自用户的 textInput 与数据集中的文章匹配时,该文章随后显示在 htmlOutput 中,该文章应该是可点击的。当用户点击该可点击文章时,第二个 tabPanel 将打开。
因此,我将文章列更改为带有链接属性的 html 输出,并将源代码中的#tab-6240-1 添加到该链接属性中。但是什么也没发生,我意识到每当我重新启动我的应用程序时,源代码中的 id 都会改变。
library(tidyverse)
library(shiny)
library(kableExtra)
library(formattable)
data = tibble(article=c(rep("article one",3), rep("article two",3), rep("article three",3)),
sales=c(100,120,140,60,80,100,200,220,240))
ui = fluidPage(
fluidRow(
column(width = 6,
textInput(inputId = "text", label = "Suchfeld")),
column(width = 6,
tabsetPanel(
tabPanel(title = "one",
htmlOutput(outputId = "table")),
tabPanel(title = "two",
selectInput(inputId = "article", label = "Look up articles", choices = data$article, multiple = F, selectize = T))))
)
)
server = function(input, output, session){
data_r = reactive({
data %>%
filter(str_detect(article, input$text))
})
output$table = function(){
data_r() %>%
mutate(article = cell_spec(article, "html", link = "#tab-6240-1")) %>%
kable("html", escape=F, align="l", caption = "") %>%
kable_styling(bootstrap_options=c("striped", "condensed", "bordered"), full_width=F)
}
#updateSelectInput()
}
shinyApp(ui = ui, server = server)
在下一步中,我想用 updateSelectInput 更新第二个 tabPanel 中的 selectInput。所选文章应与用户在第一个 tabPanel 中单击的文章完全相同
任何帮助都非常有用
解决方案
如果我理解正确,这是一种方法。
确保包含一个id
for your tabsetPanel
,以便您可以在server
.
尝试actionButton
在表格中使用而不是超链接来选择不同的文章。您可以使用自定义函数动态创建它们(请参见此处的相关示例)。
然后,您可以添加一个observeEvent
来捕捉点击actionButton
,确定选择了哪个按钮,然后切换选项卡并selectInput
相应地更改。
library(tidyverse)
library(shiny)
library(kableExtra)
library(formattable)
data = tibble(article=c(rep("article one",3), rep("article two",3), rep("article three",3)),
sales=c(100,120,140,60,80,100,200,220,240))
ui = fluidPage(
fluidRow(
column(width = 6,
textInput(inputId = "text", label = "Suchfeld")),
column(width = 6,
tabsetPanel(id = "tabPanel",
tabPanel(title = "one",
htmlOutput(outputId = "table")),
tabPanel(title = "two",
selectInput(inputId = "article", label = "Look up articles", choices = data$article, multiple = F, selectize = T))))
)
)
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
}
data_r = reactive({
data %>%
filter(str_detect(article, input$text)) %>%
mutate(action = shinyInput(actionButton, n(), 'button_', labels = article, onclick = 'Shiny.onInputChange(\"select_button\", this.id)'))
})
output$table = function(){
data_r() %>%
#mutate(article = cell_spec(article, "html", link = "#tab-6240-1")) %>%
select(action, sales) %>%
kable("html", escape=F, align="l", caption = "") %>%
kable_styling(bootstrap_options=c("striped", "condensed", "bordered"), full_width=F)
}
observeEvent(input$select_button, {
selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
updateTabsetPanel(session, inputId = "tabPanel", selected = "two")
updateSelectInput(session, inputId = "article", selected = data_r()[selectedRow,1])
})
}
shinyApp(ui = ui, server = server)
推荐阅读
- mysql - Datagrip IDE 无法使用 CLIENT_PLUGIN_AUTH 连接到 MySQL
- spring - Spring JMS - 关于设置持久订阅和共享订阅的默认消息侦听器容器问题
- python - ImportError:在 apache 服务器上运行时没有名为 flask 的模块错误
- java - 为 oracle 数据库执行更新时需要数据库链接名称
- javascript - 单击jquery ajax php中的注册后页面刷新
- amazon-web-services - 启动未出现在 AMI 列表中的 EC2 实例
- node.js - 如何使用 gm 模块在不保存图像的情况下获取图像流以将流数据发布到另一台服务器
- ios - 在集合视图中显示特定单元格。迅速
- angular - 在 ng-select 中添加搜索图标占位符而不是占位符文本?
- sql - 获取具有多个条件的行