2

Can I access a list of all input widgets within a module (let us name it myModule) and check if their state is isTruthy().

I found a way that works when I know (or can deduce) the exact names of the widgets (see 1).

All_Inputs <- vapply(paste0('axis',1:3),
                     function(x) { isTruthy(input[[x]]) },
                     logical(1))

Of course, I also could do it with a long list of if (isTruthy(input$a) && isTruthy(input$b) && .... But both solutions are not satsifactory to me, mostly because of drawbacks regarding readability and maintainability.

I know that the input class has them all listed under names that start with myModule-[AnyName]. But I do not know how to use that information to access them using a loop or even better an apply function.

Jan
  • 4,974
  • 3
  • 26
  • 43

1 Answers1

1

As input is a named list, you could use vapply on names(input):

library(shiny)

counterButton <- function(id, label = "Counter") {
  ns <- NS(id)
  tagList(
    actionButton(ns("button"), label = label),
    verbatimTextOutput(ns("out"))
  )
}

counterServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      count <- reactiveVal(0)
      observeEvent(input$button, {
        count(count() + 1)
      })
      output$out <- renderText({
        count()
      })
      count
    }
  )
}

ui <- fluidPage(
  counterButton("counter1", "Counter #1"),
  counterButton("counter2", "Counter #2"),
  textOutput('istruthy')
)

server <- function(input, output, session) {
  counterServer("counter1")
  counterServer("counter2")
  output$istruthy <- renderText({ 
    vapply(names(input), 
           function(x) { 
               ifelse(startsWith(x, "counter2-"), isTruthy(input[[x]]), TRUE) 
           },
           logical(1)) 
  })
 
}

shinyApp(ui, server)

enter image description here

Jan
  • 4,974
  • 3
  • 26
  • 43
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • Of course! ... and if I am only interested in finding any "falsies" in `module2` I can use the namespace to filter them accordingly. One more question: do you have any idea how I can get the namespace prefix that Shiny generates? Certainly I want to avoid hard-coded dependencies if possible and `ns()` only works inside the module, does it not? – Jan Feb 07 '21 at 15:02
  • @Jan, for the namespace prefix, no idea yet ;) – Waldi Feb 07 '21 at 15:26
  • Sorry about that. Didn't want to steal your bounty by adding my own answer. – Jan Feb 12 '21 at 20:36