level not increasing in R shiny

I have written a code for my application where I have used Global variable( lvl in my case). But it's not changing when my condition is being met and it's proceeding on another condition.

server<- function(input, output, session)
{
  lvl <- 1

    switch(lvl,

           # Conditions for Level 1

           if ("A CONDITION") {
             Statement

             lvl <- lvl + 1
           }
           else
           {
             invalidInput()
           }
else if{
}

It's not a global if you aren't assigning it with <<- but only <-

But you should avoid using globals.
Shiny provides reactiveVal and reactiveValues for such purposes.

I'm going to repeat my advice is that you shouldnt write code for shiny with <<-
But

lvl <<- lvl + 1

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

This is my code-
here global variable (lvl) is not increasing when my if condition gets satisfied and the get function is called.

#Invoking Libraries Required
library(shiny)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)


# Defining UI Page Format

ui<- fluidPage(useShinyjs(),tags$head(tags$link(rel="stylesheet", href="style.css")),
               titlePanel("Chatbot"),
               fluidRow(column( width = 5,
                                panel(style = "overflow-y:scroll; max-height: 300px; position:relative; align: centre",
                                      textInput("message", label = "",placeholder = "Type yor message here."),
                                      actionButton("send", "Send"), heading = "Smart Advisor", status = "primary")
               )
               )
               
               
)


# Defining Server Controls

server<- function(input, output, session)
{
  lvl <<- 1
  
  # Function to Clear the Message Box
  clearInput<- function()
  {
    updateTextInput(session,"message", value = "")
  }
  
  
  # Function to print Invalid Input
  invalidInput<- function()
  {
    insertUI(
      selector = "#message",
      where = "beforeBegin",
      ui=div(class="chat-bubbles",
             div(class="bubble",p("Kindly provide a valid input."))
      )
    )
    clearInput()      
  }
  
  
  # Function to revert to a message
  replyMessage<- function(lvl,msg)
  {
    message('Level: ',lvl)
    message('Message:',msg)
    
    switch(lvl,
           
           # Conditions for Level 1
           
           if (grepl("^[a-zA-Z][a-zA-Z ]+[a-zA-Z]$",input$message, perl=T)) {
             
             insertUI(
               selector = "#message",
               where = "beforeBegin",
               ui=div(class="chat-bubbles",
                      div(class="bubble",style="text-align: right;",
                          p(tags$b(msg), tags$br(),
                            Sys.time())
                      ),
                      
                      div(class="bubble",
                          wellPanel(
                            p(          )
                            
                          )
                      )
               )
             )
             clearInput()
             lvl <- lvl + 1
           }
           else
           {
             invalidInput()
           },
           
           # Conditions for Level 2
           
          if (msg == 1 | msg == 2 | msg == 3 | msg == 4 | msg == 5 | msg == 6)
           {
             insertUI(
               selector = "#message",
               where = "beforeBegin",
               ui=div(class="chat-bubbles",
                      tags$style("position align-right;"),
                      div(class="bubble",style="text-align: right;",
                          p(tags$b(msg), tags$br(),
                            Sys.time())),
                      
                      div(class="bubble",
                          wellPanel(
                            
                          )
                          
                      )))
             
             clearInput()
             
           }
           else
           {
             invalidInput()
           }
           
           
           
           
    )
    
  }
  
  
  
  # Function to check blank in Message box
  getMessage<- function(lvl)
  {
    observeEvent(input$send,{
      if(input$message == '')
      {
        insertUI(
          selector = "#message",
          where = "beforeBegin",
          ui=div(class="chat-bubbles",
                 div(class="bubble",p("Kindly provide a valid input."))
          )
        )
        msg = ''
        clearInput()
      }
      else
      {
        msg = input$message
        replyMessage(lvl,msg)
      }
      
    })
  }
  
  
  
  # Main Function
  startConversation<- function()
  {
    insertUI(
      selector = "#message",
      where = "beforeBegin",
      ui=div(class="chat-bubbles",
             div(class="bubble",
                 p( "Hello, can I get your name please?")
             )))
    getMessage(lvl)
    message('Inside Main Function')
    
  }
  
  startConversation()
  
  
}

# Calling Shiny Application

shinyApp(ui, server)

library(shiny)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)


# Defining UI Page Format

ui<- fluidPage(useShinyjs(),tags$head(tags$link(rel="stylesheet", href="style.css")),
               titlePanel("Chatbot"),
               fluidRow(column( width = 5,
                                panel(style = "overflow-y:scroll; max-height: 300px; position:relative; align: centre",
                                      textInput("message", label = "",placeholder = "Type yor message here."),
                                      actionButton("send", "Send"), heading = "Smart Advisor", status = "primary")
               )
               )
               
               
)


# Defining Server Controls

server<- function(input, output, session)
{
  lvl <- reactiveVal(1)
  
  # Function to Clear the Message Box
  clearInput<- function()
  {
    updateTextInput(session,"message", value = "")
  }
  
  
  # Function to print Invalid Input
  invalidInput<- function()
  {
    insertUI(
      selector = "#message",
      where = "beforeBegin",
      ui=div(class="chat-bubbles",
             div(class="bubble",p("Kindly provide a valid input."))
      )
    )
    clearInput()      
  }
  
  
  # Function to revert to a message
  replyMessage<- function(lvl,msg)
  {
    message('Level: ',lvl)
    message('Message:',msg)
    
    switch(lvl,
           
           # Conditions for Level 1
           
           if (grepl("^[a-zA-Z][a-zA-Z ]+[a-zA-Z]$",input$message, perl=T)) {
             
             insertUI(
               selector = "#message",
               where = "beforeBegin",
               ui=div(class="chat-bubbles",
                      div(class="bubble",style="text-align: right;",
                          p(tags$b(msg), tags$br(),
                            Sys.time())
                      ),
                      
                      div(class="bubble",
                          wellPanel(
                            p(          )
                            
                          )
                      )
               )
             )
             clearInput()
             print("increment level")
             
             lvl(lvl + 1)
           }
           else
           {
             invalidInput()
           },
           
           # Conditions for Level 2
           
           if (msg == 1 | msg == 2 | msg == 3 | msg == 4 | msg == 5 | msg == 6)
           {
             insertUI(
               selector = "#message",
               where = "beforeBegin",
               ui=div(class="chat-bubbles",
                      tags$style("position align-right;"),
                      div(class="bubble",style="text-align: right;",
                          p(tags$b(msg), tags$br(),
                            Sys.time())),
                      
                      div(class="bubble",
                          wellPanel(
                            
                          )
                          
                      )))
             
             clearInput()
             
           }
           else
           {
             invalidInput()
           }
           
           
           
           
    )
    
  }
  
  
  
  # Function to check blank in Message box
  getMessage<- function()
  {
    observeEvent(input$send,{
      if(input$message == '')
      {
        insertUI(
          selector = "#message",
          where = "beforeBegin",
          ui=div(class="chat-bubbles",
                 div(class="bubble",p("Kindly provide a valid input."))
          )
        )
        msg = ''
        clearInput()
      }
      else
      {
        msg = input$message
        replyMessage(lvl(),msg)
      }
      
    })
  }
  
  
  
  # Main Function
  startConversation<- function()
  {
    insertUI(
      selector = "#message",
      where = "beforeBegin",
      ui=div(class="chat-bubbles",
             div(class="bubble",
                 p( "Hello, can I get your name please?")
             )))
    getMessage()
    message('Inside Main Function')
    
  }
  
  startConversation()
  
  
}

# Calling Shiny Application

shinyApp(ui, server)

I tried with reactiveVal and reactiveValues too. But still the same.

Thanks it Worked.. :slight_smile: