activate module with actionButton

970 views
Skip to first unread message

Jamie M. Kass

unread,
Jun 15, 2017, 1:21:46 AM6/15/17
to Shiny - Web Framework for R
I have a much larger app that runs on reactiveValues() and other such not-so-great coding choices, and I have made the decision (inspired by Joe's talk in 2016) to rehaul it and work mostly with reactive functions and modules.

I have hit a bump though. I was wondering what the proper method is for activating a module using an actionButton. I'd like the user to press an actionButton to confirm a text input, and this retrieves the reactive output of a module. Most (all?) of the module examples are very simple and just plot stuff -- I want my modules to be activated with buttons, and that requires some tricky usage of observeEvent() -- unless I am out of my element here.

I boiled down my example below. The first ("modular") works, but the table updates whenever the text is changed. When I remove modularity ("unModularized"), everything works fine -- the table only updates when the button is pressed. I must be missing something fundamental.

If this is the wrong (or inadvisable) way to approach modules, please tell me what to do to make it better. Thanks for your help.

modular

formatName
<- function(n) paste(strsplit(n, split=' ')[[1]], collapse='_')

# UI part of the module
queryDB_UI
<- function(id) {
  ns
<- NS(id)
  tagList
(
    textInput
(ns("name"), label = "Input name")
 
)
}

queryDB
<- function(input, output, session, spName) {

  query
<- reactive({
    q
<- spocc::occ(input$name)
   
# extract occurrence tibble
    tbl
<- q[['gbif']]$data[[formatName(input$name)]]
   
return(tbl)
 
})
 
return(query)
}


# UI
ui
<- fluidPage(
  sidebarLayout
(
    sidebarPanel
(
      queryDB_UI
('id1'),
      actionButton
("go", "Search")
   
),
    mainPanel
(
      DT
::dataTableOutput('tbl')
   
)
 
)
)

# Server side
server
<- function(input, output, session){
 
  observeEvent
(input$go, {
    x
<- callModule(queryDB, 'id1', spName)
    output$tbl
<- DT::renderDataTable(x())
 
})
 
}

shinyApp
(ui, server)



unModularized

formatName
<- function(n) paste(strsplit(n, split=' ')[[1]], collapse='_')

# UI part of the module
queryDB_UI
<- function(id) {
  ns
<- NS(id)
  tagList
(
    textInput
(ns("name"), label = "Input name")
 
)
}

queryDB
<- function(input, output, session) {

  query
<- reactive({
    q
<- spocc::occ(input$name)
   
# extract occurrence tibble
    tbl
<- q[['gbif']]$data[[formatName(input$name)]]
   
return(tbl)
 
})
 
return(query)
}


# UI
ui
<- fluidPage(
  sidebarLayout
(
    sidebarPanel
(
     
# queryDB_UI('id1'),
      textInput
("name", label = "Input name"),
      actionButton
("go", "Search")
   
),
    mainPanel
(
      DT
::dataTableOutput('tbl')
   
)
 
)
)

# Server side
server
<- function(input, output, session){
 
 
# x <- callModule(queryDB, 'id1')
 
  observeEvent
(input$go, {
    q
<- spocc::occ(input$name)
   
# extract occurrence tibble
    tbl
<- q[['gbif']]$data[[formatName(input$name)]]
   
# output$tbl <- DT::renderDataTable(x())
    output$tbl
<- DT::renderDataTable(tbl)
 
})
 
}

shinyApp
(ui, server)


Joe Cheng

unread,
Jun 15, 2017, 6:40:19 PM6/15/17
to Jamie M. Kass, Shiny - Web Framework for R
Don't call callModule from inside observeEvent; keep it at the top level. Take the reactive expression that's returned, and use eventReactive to wrap it in the button click. And use the eventReactive from your outputs, etc.

x <- callModule(...)
y <- eventReactive(input$go, x())
output$tbl <- DT::renderDataTable(y())
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/48915e95-c6ce-497b-9c3e-36d1274535ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jamie M. Kass

unread,
Jun 21, 2017, 10:36:44 AM6/21/17
to Shiny - Web Framework for R, ndimhy...@gmail.com
Joe,

Sorry for the late reply. Thanks so much for the tip -- it works beautifully. I finally get the difference between eventReactive() and observeEvent().

It would be great if someone could include an addendum to the Shiny Modules page on the RStudio website to address this usage with actionButtons. I couldn't find any web resources that answered my question. Thanks again!

Jamie

Konrad

unread,
Nov 22, 2018, 9:30:01 AM11/22/18
to Shiny - Web Framework for R
Hi,

I would be interested in this as well. I've started a discussion on SO that outlines the problem I want to address with this solution. The SO answers solves basics but there are still some glitches in a more complex application that I've. It would be good to have a working example of solutioning this properly with a module consisting of multiple interactive elements and elaborate interface.

Kind regards,
Konrad
Reply all
Reply to author
Forward
0 new messages