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)