How do I add functionality for Apply and Reset buttons in Shiny when using a module?

Hello. I have a simple shiny app that has 2 text inputs. The app lets the user edit the inputs and click an Apply button, then the updated user inputs are displayed. The app also has a Reset button that lets the user reset the text inputs back to the defaults.

I am trying to use a module for creating the textInput widgets. However when using a module and adding the functionally for Apply and Reset, I'm confused on where to put the eventReactive (for the Apply) and the observeEvent for the Reset) buttons. Do they go in the server side of the app or do they go in the module?

Here is code to reproduce the app;

# Libraries
library(shiny)
library(shinyjs)

source("module.R")

# UI
ui <- fluidPage(
    shinyjs::useShinyjs(),

    fluidRow(

        br(),

        actionButton("apply", "Apply"),

        actionButton("reset", "Reset"),

        br(),

        textInput(
            inputId = "text_input",
            label = "Text Input",
            value = c("facebook", "google")
        ),

        textInput(
            inputId = "text_value",
            label = "Text Value",
            value = c(100, 200)
        )
    ),

    fluidRow(
        h3("Inputs Section"),
        verbatimTextOutput("input_text"),
        verbatimTextOutput("text_value")
        

    )

)

# Server
server <- function(input, output, session) {

    # apply
     input_text <- eventReactive(eventExpr = input$apply, valueExpr = {
        input$text_input
    },ignoreNULL = FALSE)

    output$input_text <- renderPrint({
        input_text()
    })
    
    input_value <- eventReactive(eventExpr = input$apply, valueExpr = {
        input$text_value
    },ignoreNULL = FALSE)
    
    output$text_value <- renderPrint({
        input_value()
    })

    # reset
    shiny::observeEvent(eventExpr = input$reset, handlerExpr = {
        
        updateTextAreaInput(
            session = getDefaultReactiveDomain(),
            inputId = "text_input",
            value = c("facebook", "google")
        )
        
        updateTextAreaInput(
            session = getDefaultReactiveDomain(),
            inputId = "text_value",
            value = c(100, 200)
        )
        
        

        shinyjs::delay(ms = 300, expr = {
            shinyjs::click(id = "apply")
        })
    })



}

# Run the Shiny app
shinyApp(ui, server)

Notice that I am sourcing a module. Here is the code for the module;


textInputUI <- function(id, label, value) {
  ns <- NS(id)
  
  textInput(ns("text_input"), label = label, value = value)
  
}

textInputServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      
    }
  )
}

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.