-
-
Save leungi/521b90c933a80c7a3a1dabe97e169c01 to your computer and use it in GitHub Desktop.
Shiny updateTextInput between modules by passing the session object in between
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
# MODULES ---------------------------------------------------------------------- | |
text_ui <- function(id) { | |
ns <- shiny::NS(id) | |
shiny::tagList( | |
textInput(ns("text"), "Write here"), | |
verbatimTextOutput(ns("display")) | |
) | |
} | |
text_server <- function(input, output, session) { | |
rv.text <- reactiveValues(val = FALSE) | |
observeEvent(input$text, { | |
rv.text$val <- input$text | |
}) | |
output$display <- renderText({ | |
rv.text$val | |
}) | |
return({ session }) | |
} | |
reset_btn_ui <- function(id) { | |
ns <- shiny::NS(id) | |
shiny::tagList( | |
actionButton(ns("reset"), "Reset") | |
) | |
} | |
reset_btn_server <- function(input, output, session, TextModuleSession) { | |
observeEvent(input$reset, { | |
updateTextInput(session = TextModuleSession, "text", value = "") | |
}) | |
} | |
# MAIN ------------------------------------------------------------------------- | |
ui <- fluidPage( | |
fluidRow( | |
column(6, | |
text_ui("text"), | |
reset_btn_ui("btn") | |
) | |
) | |
) | |
server <- function(input, output, session) { | |
TextModuleSession <- callModule(module = text_server, | |
id = "text") | |
callModule(module = reset_btn_server, | |
id = "btn", | |
TextModuleSession) | |
} | |
# Shiny update and reset input with Shiny modules | |
# Related source: https://stackoverflow.com/a/51757792/ | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment