Module - reactivity stops after a dozen of clicks

Dear all,
The very simple code below does not work as I would expect: when I click a couple of times up and down on qty numeric field little arrows, tableOutput display freeze and then does not change any more. Reactivity is stopped - see attached image.
The first clicks do work though, it freezes only after a dozen clicks - depending how fast one clicks :slight_smile:

Code is:
ExampleUI <- function(id) {
tagList(
numericInput(NS(id,"qty"),"Quantity",value=0)
)
}

ExampleServer = function(id) {
moduleServer(id,
function(input,output,session) {
return(reactive({input$qty}))
})
}

ExampleDemo = function() {
ui = fluidPage(
ExampleUI("Entry"),
tableOutput("Res")
)

server = function(input,output,session) {
output$Res=renderTable(ExampleServer("Entry")())
}

shinyApp(ui,server)
}
And then execute ExampleDemo()

I run:

  • RStudio 2022.12.0+353 "Elsbeth Geranium" Release (7d165dcfc1b6d300eb247738db2c7076234f6ef0, 2022-12-03) for Windows
    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) RStudio/2022.12.0+353 Chrome/102.0.5005.167 Electron/19.1.3 Safari/537.36
  • R.version returns
    platform x86_64-w64-mingw32
    arch x86_64
    os mingw32
    crt ucrt
    system x86_64, mingw32
    status
    major 4
    minor 2.2
    year 2022
    month 10
    day 31
    svn rev 83211
    language R
    version.string R version 4.2.2 (2022-10-31 ucrt)
    nickname Innocent and Trusting
  • packageVersion("shiny")
    [1] β€˜1.7.4’
    Capture d’écran 2023-02-19 224029

Hi Alexis :wave:

I think the main issue is that you are calling the module server function each time you change the "qty" input value (because you are defining a reactive expression there that uses input$qty). This happens because you are calling ExampleServer inside the renderTable.

You can check this by just adding some trivial print like:

ExampleServer = function(id) {
  moduleServer(id,
               function(input,output,session) {
                 print("Module server called")
                 return(reactive({input$qty}))
               })
}

and running the app. Each time you change the value, you'll see the print message in the console.

So, an easy fix to this issue is to just extract the module server call outside the render function, like this:

 
  server = function(input,output,session) {
    qty_reactive_val <- ExampleServer("Entry")
    output$Res=renderTable(qty_reactive_val())
  }
  
  shinyApp(ui,server)
}

Interestingly, if for some reason you'd like to still call the module server many times, if you use reactiveVal instead of reactive you might get away with it:

ExampleServer = function(id) {
  moduleServer(id,
               function(input,output,session) {
                 print("Module server called")
                 return(reactiveVal(input$qty))
               })
}

ExampleDemo = function() {
  ui = fluidPage(
    ExampleUI("Entry"),
    tableOutput("Res")
  )
  
  server = function(input,output,session) {
    output$Res=renderTable(ExampleServer("Entry")())
  }
  
  shinyApp(ui,server)
}

Hope this helps :slight_smile:
Cheers!


This post was published by an Appsilon team member. Our company can help you get the most out of RShiny and Posit/RStudio products.

Check our open positions here.

Appsilon: Building impactful RShiny Dashboards and providing R coding services.
Appsilon_GIFsmall_whitebg

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.