Swastik Mohapatra
Swastik Mohapatra

Reputation: 77

showModal not triggering inside reactive in R Shiny

I am trying to take in some user input, compare it with values in a database and then display modals based on the output. I have the following sample code:
emp_table is my master table which has the list of employees and their details
emp_name is a text field that takes in the employee name
emp_id is a text field that takes in employee ID
emp_info_submit is the action button that the user clicks after entering their Employee Name and Employee ID

 library(shiny)
 library(shinydashboard)
 library(dplyr)
 #This is my master database with all the employee details
    emp_database<-data.frame("emp_name" = "Mark", "emp_id"= 103221)

    #defining the ui
    ui <- dashboardPage(
      dashboardHeader(title = "Placeholder text"),

      #Creating the appropirate navigation tabs

      dashboardSidebar(
        sidebarMenu(id = "nav_bar",
                menuItem(text = "Employee Info",tabName = "emp_info",icon = icon("info-circle"))
              )
            ),
     #Populating the Employee Info tab
      dashboardBody(tabItems(
        tabItem(
          tabName = "emp_info",
          fluidRow(
            box(
              textInput(inputId = "emp_name",label = "Enter your Name",placeholder = "enter name here",width = "100%"),
              textInput(inputId = "emp_id",label = "Enter your Employee ID",placeholder = "enter employee id here",width = "100%"),
              actionButton(inputId = "emp_info_submit",label = "Submit",icon = icon("thumbs-up"))
            )
          )
        )
      ))
     )


    server <- function(input, output) {
      observeEvent(input$emp_info_submit,{
        name1<-reactive({as.character(input$emp_name)})
        id1<-reactive({as.numeric(input$emp_id)})
        reactive({
          if(name1 %in% emp_database$emp_name){
            showModal(modalDialog("Exists"))
          }
          else{
            showModal(modalDialog("DOes not exist"))
          }
        })

      })


    }


    shinyApp(ui = ui, server = server)

Basically, what I learnt through trial and error is that I cannot compare the user input values with my database unless I wrap it in a reactive environment. However, the modal does not trigger when I run the app.
Where am I going wrong? Also, please suggest some good coding practices if my code is not well written.

EDIT1: Included the UI Function as well.

Upvotes: 2

Views: 1857

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

That's weird to use an observer inside an observer. Try:

server <- function(input, output) {

  name1 <- eventReactive(input$emp_info_submit, {
    as.character(input$emp_name)
  })
  id1 <- eventReactive(input$emp_info_submit, {
    as.character(input$emp_id)
  })
  observeEvent(input$emp_info_submit,{
    if(name1() %in% emp_database$emp_name){ # was an error here: name1 instead of name1()
      showModal(modalDialog("Exists"))
    }
    else{
      showModal(modalDialog("DOes not exist"))
    }
  })

}

Upvotes: 2

Related Questions