当输入更改(例如,选项卡更改)时,我如何告诉我的 modalDialog 自动关闭?
How can i tell my modalDialog to be automatic closed when an input changes (eg. a tab changes)?
我有一个闪亮的应用程序,其中有一个 navbarPage
和两个 tabPanel
。在第一个选项卡中,我有一个 actionLink
,它初始化一个 modalDialog
,包括一个 reactable
,单元格作为按钮。当我单击 table 中的按钮时,我想更改 navbarPage
中的输入,即将 tabPanel
从“tabone”更改为“tabtwo”。同时,我希望 modalDialog
在 tabPanel
更改时关闭。那我怎么才能让我的 modalDialog
关闭呢?
library(Shiny)
library(reactable)
ui = fluidPage(
navbarPage(title = "tabs", id = "nav",
tabPanel(title = "tabone",
actionLink(inputId = "action", "open modalbox")),
tabPanel(title = "tabtwo")
))
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
}
observeEvent(input$action, {
showModal(
modalDialog(
tagList(reactableOutput(outputId = "table"))
))})
output$table = renderReactable({
data = tibble(c = "click to change navbar input") %>%
mutate(c = shinyInput(actionButton, n(), 'id', labels = c, onclick = "Shiny.setInputValue('change', this.innerText)")) %>%
reactable(data = .,
sortable = FALSE,
columns = list(
`c` = colDef(
html = TRUE)
))})
observeEvent(input$change, {
updateTabsetPanel(session = session, inputId = "nav", selected = "tabtwo")
})
}
shinyApp(ui, server)
您可以在 change
按钮的 observeEvent
中添加 removeModal()
。
library(shiny)
library(reactable)
ui = fluidPage(
navbarPage(title = "tabs", id = "nav",
tabPanel(title = "tabone",
actionLink(inputId = "action", "open modalbox")),
tabPanel(title = "tabtwo")
))
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
}
observeEvent(input$action, {
showModal(
modalDialog(
tagList(reactableOutput(outputId = "table"))
))
})
output$table = renderReactable({
data = tibble(c = "click to change navbar input") %>%
mutate(c = shinyInput(actionButton, n(), 'id', labels = c, onclick = "Shiny.setInputValue('change', this.innerText)")) %>%
reactable(data = .,
sortable = FALSE,
columns = list(
`c` = colDef(
html = TRUE)
))})
observeEvent(input$change, {
updateTabsetPanel(session = session, inputId = "nav", selected = "tabtwo")
removeModal()
})
}
shinyApp(ui, server)
我有一个闪亮的应用程序,其中有一个 navbarPage
和两个 tabPanel
。在第一个选项卡中,我有一个 actionLink
,它初始化一个 modalDialog
,包括一个 reactable
,单元格作为按钮。当我单击 table 中的按钮时,我想更改 navbarPage
中的输入,即将 tabPanel
从“tabone”更改为“tabtwo”。同时,我希望 modalDialog
在 tabPanel
更改时关闭。那我怎么才能让我的 modalDialog
关闭呢?
library(Shiny)
library(reactable)
ui = fluidPage(
navbarPage(title = "tabs", id = "nav",
tabPanel(title = "tabone",
actionLink(inputId = "action", "open modalbox")),
tabPanel(title = "tabtwo")
))
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
}
observeEvent(input$action, {
showModal(
modalDialog(
tagList(reactableOutput(outputId = "table"))
))})
output$table = renderReactable({
data = tibble(c = "click to change navbar input") %>%
mutate(c = shinyInput(actionButton, n(), 'id', labels = c, onclick = "Shiny.setInputValue('change', this.innerText)")) %>%
reactable(data = .,
sortable = FALSE,
columns = list(
`c` = colDef(
html = TRUE)
))})
observeEvent(input$change, {
updateTabsetPanel(session = session, inputId = "nav", selected = "tabtwo")
})
}
shinyApp(ui, server)
您可以在 change
按钮的 observeEvent
中添加 removeModal()
。
library(shiny)
library(reactable)
ui = fluidPage(
navbarPage(title = "tabs", id = "nav",
tabPanel(title = "tabone",
actionLink(inputId = "action", "open modalbox")),
tabPanel(title = "tabtwo")
))
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
}
observeEvent(input$action, {
showModal(
modalDialog(
tagList(reactableOutput(outputId = "table"))
))
})
output$table = renderReactable({
data = tibble(c = "click to change navbar input") %>%
mutate(c = shinyInput(actionButton, n(), 'id', labels = c, onclick = "Shiny.setInputValue('change', this.innerText)")) %>%
reactable(data = .,
sortable = FALSE,
columns = list(
`c` = colDef(
html = TRUE)
))})
observeEvent(input$change, {
updateTabsetPanel(session = session, inputId = "nav", selected = "tabtwo")
removeModal()
})
}
shinyApp(ui, server)