Unable to pass user inputs into R shiny modules

I am working on a shiny app where on load it shows a default table. Have a user input which when I enter and click on run should update the table. And also when clicked on reset it should show the default table.As of now I am able to view the default table and nothing happens when I click on the run button.

optimzation <- function(input, output, session, data,budget,run,reset) {
  
  v <- reactiveValues(data = data)
  
  observeEvent(run, {
    v$data <- data %>% mutate(carb = mpg * budget)
  })
  
  observeEvent(reset, {
    v$data <- data # your default data
  })  
  
  output$mod_table <- DT::renderDataTable({
    DT::datatable(v$data, filter = "top")
  })
}

optimzationUI <- function(id) {
  ns <- NS(id)
  dataTableOutput(ns("mod_table"))
  
}

shinyApp(
    ui = basicPage(
      mainPanel(
        numericInput("budget_input", label = h5("Total Budget"), value = 9000000),
        actionButton("opt_run", "Run"),
        actionButton("opt_reset", "Reset"),
        tags$hr(),
        optimzationUI("optimize")
      )
    ),
    server = function(input, output) {
      demodata<-mtcars
      callModule(optimzation,"optimize", demodata,budget=input$budget_input,run=input$opt_run,reset = input$opt_reset)
      
    }
  )

Created on 2019-02-13 by the reprex package (v0.2.1.9000)

Hi @sten, I think it makes sense to include the buttons in your module UI, since you are relying on them in the server part of your module. You cannot access input variables from the application in your module (by design, see documentation). Something like this should work:

library(DT)
library(dplyr)

optimization <- function(input, output, session, data) {

    v <- reactiveValues(data = data)

    observeEvent(input$opt_run, {
        v$data <- data %>% mutate(carb = mpg * input$budget_input)
    })

    observeEvent(input$opt_reset, {
        v$data <- data # your default data
    })

    output$mod_table <- DT::renderDataTable({
        DT::datatable(v$data, filter = "top")
    })
}

optimizationUI <- function(id) {
    ns <- shiny::NS(id)

    tagList(
        numericInput(ns("budget_input"), label = h5("Total Budget"), value = 9000000),
        actionButton(ns("opt_run"), "Run"),
        actionButton(ns("opt_reset"), "Reset"),
        tags$hr(),
        DT::dataTableOutput(ns("mod_table"))
    )
}

shinyApp(
    ui = basicPage(
        mainPanel(
           optimizationUI("optimize")
        )
    ),
    server = function(input, output) {
        demodata <- mtcars
        callModule(optimization, "optimize", demodata)
    }
)
1 Like

This is a duplicate from StackOverflow and has been answered yesterday

@sten in the future, please make it clear where you cross post the same question to other websites. It's a polite thing to do so that if you get an answer in one place, others won't waste their time thinking nobody has answered your question yet.

1 Like

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.