How to use Emoji as an action button? Trying to do but failed..

Hi all, I am trying to use the Emojis as action button. I am trying to do.. it's not working..
Below is the reprex-

library(tidyverse)
library(dplyr)
library(shiny)
library(shinydashboard)
library(shinyjs)
library(shinyWidgets)
library(emo)
library(emojifont)
library(shinyBS)

ui<- fluidPage(useShinyjs(),
               #extendShinyjs(text = "shinyjs.button = function() {window.scrollTo(0, 50);}"),               
               titlePanel("Form"),
               fluidRow(column( width = 4,
                                div(id='outDiv',
                                    panel(style = "overflow-y:scroll; max-height: 300px; max-width: 500px; position:relative; align: centre",
                                          textInput("message", label = "",placeholder = "Type your message here."),
                                          actionButton("send", "Send"), heading = "Bot", status = "primary"))
               )
               ))

# Defining Server Controls
server<- function(input, output, session)
{
  
  
  # Declaring and Initializing Global Variables
  i <- 1
  lvl <- reactiveVal()
  lvl(i)
  
  categories <- matrix(c("Personal Challenges", "Academic Challenges","Lifestyle Challenges"), byrow =F, nrow = 1)
  Terms <- reactiveValues(Selection = 1 )  
  
  
  emojis<- matrix(c("smiley", "confused","worried"), byrow =F, nrow = 1)
  em <- reactiveValues(e = 1 ) 
  
  
 
  
  #Clear Function 
  clearInput<- function()
  {
    updateTextInput(session,"message", value = "")
  }
  
  #Invalid Function
  invalidInput<- function()
  {
    insertUI(
      selector = "#message",
      where = "beforeBegin",
      ui=div(class="chat-bubbles",
             div(class="bubble admin",
                 p("Kindly provide a valid input."))
      ), immediate = TRUE
    )
    clearInput()      
  }
  
  
  replyMessage<- function(lvl,msg)
  {
    
    
    switch(lvl,
           
           # Check for Level 1
           if(grepl("^[a-zA-Z][a-zA-Z ]+[a-zA-Z]$",msg, perl=T)){
             lvl(lvl + 1)
             insertUI(selector = "#message", where = "beforeBegin",
                      ui= div(class="chat-bubbles",
                              div(class="bubble admin",
                                  wellPanel(
                                    p("Hi",tags$b(input$message),tags$br(),"How are you doing today?",
                                      tags$br(),tags$br(),
                                      actionBttn("S1",label = emoji("smiley"), style="jelly" ), 
                                      actionBttn("S2",label = emoji("confused"), style="jelly"),
                                      actionBttn("S3",label = emoji("worried"), style="jelly"))))
                              
                            
                      ), immediate = TRUE)
             
             shinyjs::disable("message")
             shinyjs::hide("send")
             
             clearInput()
             
           }
           else
           {
             invalidInput()
             runjs('
                    document.getElementById("bottom").scrollIntoView();
                 ')
           },
           
           
           # Check for Level 2
           if(msg==emoji("smiley"))
           {
             lvl(lvl + 1)
             insertUI(selector = "#message", where = "beforeBegin",
                      ui= div(class="chat-bubbles",
                              div(class="bubble admin",
                                  wellPanel(
                                    p("I can help you in-"), tags$br(),
                                    p(actionBttn("analyse1","Personal Challenges", color = "warning", style = "material-flat", block = TRUE, size = "xs") , tags$br(),
                                      actionBttn("analyse2","Academic Challenges", color = "success",style = "material-flat", block = TRUE, size = "xs") , tags$br(),
                                      actionBttn("analyse3","Lifestyle Challenges", color = "primary", style = "material-flat",block =TRUE, size = "xs")
                                    )
                                  ))), immediate = TRUE)
             
             shinyjs::disable("message")
             shinyjs::hide("send")
             clearInput()
             
           }
           else
           {
             invalidInput()
             runjs('
                    document.getElementById("bottom").scrollIntoView();
                 ')
           }
           
    )
  }
  
  
  
  
  # Function to check blank in Message box
  getMessage<- function(lvl)
  {
    # Observer Event for Message Box
    observeEvent(input$send,{
      if(input$message == '')
      {
        insertUI(
          selector = "#message",
          where = "beforeBegin",
          ui=div(class="chat-bubbles",
                 div(class="bubble admin",
                     p("Kindly provide a valid input."))
          )
        )
        clearInput()
      }
      else
      {
        replyMessage(lvl(),input$message)
      }
      
    })
    
   
    lapply(sprintf("S%s", 1:3),
           function(x)
           {
             observeEvent(input[[x]],{
               em$e<- as.numeric(sub("S", "", x))
               insertUI(selector = "#message", where = "beforeBegin",
                        p(paste(
                          replyMessage(lvl(),emojis[, em$e])))
               )
               
             })
             
             
           })
    
  }
  
  
  
  # Main Function
  startConversation<- function()
  {
    
    clearInput()
    insertUI(
      selector = "#message",
      where = "beforeBegin",
      ui=div(class="chat-bubbles",
             div(class="bubble admin",
                 p("Hey! Could I get your name?")
             )))
    getMessage(lvl)
    
  }
  startConversation()
}

shinyApp(ui,server)

Hi Ankush, please prioritise making reprex as small as possible.
Here is a solution.

#renv::install("hadley/emo")
library(emo)
library(shiny)

ui <- fluidPage(
  actionButton("mybutton",
               label=emo::ji("smile")),
  verbatimTextOutput("tout")
)

server <- function(input, output, session) {
  output$tout <- renderPrint({
    cat("you clicked the button",input$mybutton,"\n")
  })
}

shinyApp(ui, server)

Hahaha.

Thanks for the solution Nir. I was just trying to send it with the functions I built. !!!!!!

Though, ur code is working. Can you help me in my code, where am I going wrong?

You would have to explain your problem, and provide a minimal reprex.

My problem is mentioned in my reprex.

I am asking for Name, and then asking for the mood, for which I am giving the option of the selecting the emojis. By selecting the emojis, one can process ahead(as in my reprex).

so, im confused what you are confused about...
It seems you didnt realise that the package emo contains function ji for placing a smiley.
you have written emoji() when you should have emo::ji() ,or just ji()

It's simple-
Stage 1- Kindly provide your name?
User- provides input
Stage 2- Whats's your mood?
Emojis- Happy, Sad, Confused
user- selects the emoji.
Stage 3- I can help you in-
.....and so on...

This is what I want!!!

having fixed your emo::ji issue, the next problem is that
replyMessage must return a character value (because you paste it inside your getmessage function) however, you only use it to make side effects. you should put in explicit return(NULL) statements where replyMessage exits a switch branch or put some content in it return("whatever") if you want some value in the reply message.

Got it Nir.. a return has to be called..

Thanks :slight_smile:

This topic was automatically closed 7 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.