How to control reactivity in the Shiny app?

Hello guys.
I finally managed to make something like a reprex of my app. I just put the part I'm having problems with. I'm creating an app using the shinydashboard layout. I select options but I want it to only run after clicking the button. That is, I want to control reactivity. The problem is, he doesn't. I put a spiner to confirm. It runs when selecting the menu option and ignores the button. Below is my code. It doesn't have a database. Could anyone help me how can I solve the problem?

Thanks.

library(DT)
library(kableExtra)
library(knitr)
library(lubridate)
library(magrittr)
library(stringr)
library(shiny)
library(tidyverse)

head <- dashboardHeader(title = "Menu de Opções", titleWidth = 300)

side <- dashboardSidebar(width = "300px",

                     sidebarMenu(
                       menuItem("Inicio",tabName = "inicio"
                       ),
                       menuItem("Tabelas Pivot", tabName = "pivot",
                                
                                dateRangeInput('date_range1',
                                               label = 'Amplitude da período: yyyy-mm-dd',
                                               start = Sys.Date() - 61, end = Sys.Date()
                                ),
                                
                                 menuSubItem("Internação",tabName = "pvt_medico"),
                                 menuSubItem("Ambulatório",tabName = "pvt_atend"),
                                 menuSubItem("Exames",tabName = "pvt_exames"),
                                 actionButton("execute","Executar!!!"),
                                 br()
                              )

                     )

)

################ Corpo do dahsboarb

body <- dashboardBody(

fluidPage(

tabItems(

    tabItem(tabName = "pvt_medico",
            
            
        fluidRow(
              
           box(width = 12,height = "600px",
                  tags$head(tags$style( type = 'text/css',  '#myPivotAM{ overflow-x: scroll; overflow-y: scroll; }')),
                  withSpinner(verbatimTextOutput("myPivotAM"))))
         ),

    tabItem(tabName = "pvt_atend",
            
        fluidRow(
              
              box(width = 12,height = "600px",
                  tags$head(tags$style( type = 'text/css',  '#myPivotAM{ overflow-x: scroll; overflow-y: scroll; }')),
                  withSpinner(verbatimTextOutput("myPivotAT"))))
        ),
    
    tabItem(tabName = "pvt_exames",
           
            fluidRow(
              
              box(width = 12,height = "600px",
                  tags$head(tags$style( type = 'text/css',  '#myPivotAM{ overflow-x: scroll; overflow-y: scroll; }')),
                  withSpinner(verbatimTextOutput("myPivotEX"))))
            
    )

 )

)
)

dashboardPage(head, side, body, skin = c("green"))

server <- function(input, output) {

observeEvent(input$execute,{

#-----------------------------------------------------------------------------

output$myPivotAM <- renderPrint({

print("Opção Internação")
print(input$date_range1)

})

#------------------------------------------------------------------------------
output$myPivotAT <- renderPrint({

 print("Opção Anbulatório")
 print(input$date_range1)

})

#------------------------------------------------------------------------------
output$myPivotEX <- renderPrint({

print("Opção Exames")
print(input$date_range1)

})
})
}

You should avoid wrapping render*-functions in observeEvent. You can use bindEvent instead:

library(DT)
library(kableExtra)
library(knitr)
library(lubridate)
library(magrittr)
library(stringr)
library(shiny)
library(tidyverse)
library(shinydashboard)
library(shinycssloaders)

ui <- dashboardPage(
  header = dashboardHeader(title = "Menu de Opções", titleWidth = 300),
  sidebar = dashboardSidebar(width = "300px",
                             sidebarMenu(
                               menuItem("Inicio", tabName = "inicio"),
                               menuItem(
                                 "Tabelas Pivot",
                                 tabName = "pivot",
                                 dateRangeInput(
                                   'date_range1',
                                   label = 'Amplitude da período: yyyy-mm-dd',
                                   start = Sys.Date() - 61,
                                   end = Sys.Date()
                                 ),
                                 menuSubItem("Internação", tabName = "pvt_medico"),
                                 menuSubItem("Ambulatório", tabName = "pvt_atend"),
                                 menuSubItem("Exames", tabName = "pvt_exames"),
                                 actionButton("execute", "Executar!!!"),
                                 br()
                               )
                             )),
  body = dashboardBody(fluidPage(tabItems(
    tabItem(tabName = "pvt_medico",
            fluidRow(
              box(
                width = 12,
                height = "600px",
                tags$head(
                  tags$style(type = 'text/css',  '#myPivotAM{ overflow-x: scroll; overflow-y: scroll; }')
                ),
                withSpinner(verbatimTextOutput("myPivotAM"))
              )
            )),
    tabItem(tabName = "pvt_atend",
            fluidRow(
              box(
                width = 12,
                height = "600px",
                tags$head(
                  tags$style(type = 'text/css',  '#myPivotAM{ overflow-x: scroll; overflow-y: scroll; }')
                ),
                withSpinner(verbatimTextOutput("myPivotAT"))
              )
            )),
    tabItem(tabName = "pvt_exames",
            fluidRow(
              box(
                width = 12,
                height = "600px",
                tags$head(
                  tags$style(type = 'text/css',  '#myPivotAM{ overflow-x: scroll; overflow-y: scroll; }')
                ),
                withSpinner(verbatimTextOutput("myPivotEX"))
              )
            ))
  ))),
  skin = c("green")
)

server <- function(input, output, session) {
    output$myPivotAM <- renderPrint({
      print("Opção Internação")
      print(input$date_range1)
    }) |> bindEvent(input$execute)
    
    output$myPivotAT <- renderPrint({
      print("Opção Anbulatório")
      print(input$date_range1)
    }) |> bindEvent(input$execute)
    
    output$myPivotEX <- renderPrint({
      print("Opção Exames")
      print(input$date_range1)
    }) |> bindEvent(input$execute)
}

shinyApp(ui, server)

OK. I will use your tip.

Thanks.

Hello.
The app gave an error. See below.

runApp()
Warning: Good news!
You don't need to call useShinyalert() anymore. Please remove this line from your code.
If you really want to pre-load {shinyalert} to the UI for any reason, use:
useShinyalert(force = TRUE)

Listening on http://127.0.0.1:3291
This Font Awesome icon ('gears') does not exist:

  • if providing a custom html_dependency these name checks can
    be deactivated with verify_fa = FALSE
    Warning: Error in force: object 'ui' not found
    52: force
    51: uiHttpHandler
    50: shinyApp
    Error in force(ui) : object 'ui' not found

It seems your ui function wasn't loaded on running shinyApp(ui, server).

Hello. Good Morning.

Thank you for your help. Still having the same problem.

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.