onNextInput: Wait for the next input change

Description Usage Arguments Details Value Examples

View source: R/spsServerCollections.R

Description

This is a server function that runs like a callback when the next time any input value changes. This is useful for to watch dynamically added components from the server and then do something. For example, loading a shiny module UI from server by renderUI and loading the shiny module server from server by moduleServer. Loading the server must wait until renderUI is finished. However, in shiny renderUI is asynchronous. It means moduleServer is immediately executed after renderUI. The result is module's server part cannot find the UI, because it is still updating. This function is hack to solve this problem by waiting for the next input settlement operation called from Shiny javascript to R so one can start other server actions.

Usage

1
onNextInput(expr, session = getDefaultReactiveDomain())

Arguments

expr

code expression, wrap inside {}

session

shiny session

Details

Common usage

This function adds a on.exit statement to the parent observer, renderXX, and other reactive events, so make sure you use them inside these functions instead of plain server.

server = function(input, output, session) {
  # ok
  output$someID <- renderUI({
    onNextInput({...})
    div(...)
  })

  # following is not ok
  onNextInput({...})
}

About this function

This function fixes the issue in shiny #3348. Until there is an official support for this feature, this function is useful.

Value

an observeEvent that runs only one time to watch for the next input change.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
if(interactive()){
  library(shiny)

  # Simple example
  ui <- fluidPage(
    uiOutput("someui")
  )
  server <- function(input, output, session) {
    output$someui <- renderUI({
      # we update the text of new rendered text input to 3 random letters
      # after `textInput` is displayed, and it only works for one time.
      onNextInput({
        updateTextInput(inputId = "mytext", value = paste0(sample(letters, 3), collapse = ""))
      })
      textInput("mytext", "some text")
    })
    # if you directly have update event like following line, it won't work
    # updateTextInput(inputId = "mytext", value = paste0(sample(letters, 3), collapse = ""))
  }
  shinyApp(ui, server)


  # complex example with modules
  modUI <- function(id) {
    ns <- NS(id)
    textInput(ns("mytext"), "some text")
  }
  modServer = function(id) {
    moduleServer(
      id,
      function(input, output, session) {
        updateTextInput(inputId = "mytext", value = paste0(sample(letters, 3), collapse = ""))
      }
    )
  }
  ui = fluidPage(
    actionButton("a", "load module UI"),
    uiOutput("mod_container")
  )
  server = function(input, output, session) {
    # everytime you click, render a new module UI and update the text value
    # immediately
    observeEvent(input$a, {
      output$mod_container <- renderUI({
        onNextInput(modServer("mod"))
        modUI("mod")
      })
    })
    # Without `onNextInput`, module server call will not work
    # uncomment below and, comment `onNextInput` line to see the difference
    # modServer("mod")
  }

  shinyApp(ui, server)
}

spsComps documentation built on Feb. 3, 2022, 1:07 a.m.