Hi all,
I am creating an application in which I am trying to create action buttons from the user's input. Suppose, I have a data frame as shown.
What I am trying to do is-
1)Ask for the user name?
2) Ask him- "Select the countries" and 4 action buttons with country names.
3) If I select any country, it should again ask- select cities and show the number of cities as action buttons.
4) If I select any city it should ask for the Popular for-item as action buttons.
5) when I select any of the popular button it should show the ratings .
How to get this?
My reprex-
library(tidyverse)
library(dplyr)
library(purrr)
library(magrittr)
library(shinyWidgets)
library(emojifont)
library(shinyBS)
library(renv)
library(shiny)
library(shinydashboardPlus)
library(shinydashboard)
library(shinyjs)
library(reshape2)
library(pool)
library(shiny.info)
library(shinycssloaders)
library(shinybusy)
library(jsonlite)
library(readxl)
library(tidyverse)
dat<- read_xlsx(path = "Dummy.xlsx")
ui<- fluidPage(
useShinyjs(),
titlePanel("Info"),
fluidRow(
column( width = 4,
panel(
style = "overflow-y:scroll; max-height: 300px; position:relative; align: centre",
textInput("message", label = "",
placeholder = "Type Message"),
actionButton("send", "Send"), heading = "Smart-Advisor", status = "primary"
))))
# Defining Server Controls
server<- function(input, output, session)
{
# Declaring and Initializing Global Variables
i <- 1
lvl <- reactiveVal()
lvl(i)
emojis<- matrix(c("India", "US","UK", "Canada"), byrow =F, nrow = 1)
emj <- reactiveValues(em = 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()
}
ind<- reactive({
dat%>%filter(Country=="India")%>%distinct(Cities)
})
ind_2<- reactive({
dat%>%filter(Country=="India" , Cities=="New Delhi")%>%distinct(`Popular for-Item`)
})
#Main fucntion
replyMessage<- function(lvl,msg)
{
switch(lvl,
# Check for Level 1
if(grepl("^[a-zA-Z][a-zA-Z ]+[a-zA-Z]$",msg, perl=T))
{
insertUI(selector = "#message", where = "beforeBegin",
ui= div(class="chat-bubbles",
div(class="bubble admin",
img(),
wellPanel(
p("Hi",tags$b(msg),".",tags$br(), "My name is Zeta!","How are you feeling today?")), tags$br()),
p(actionBttn("mood1","India", icon = icon("grin"), style = "pill", color = "warning",size = "xs"),
actionBttn("mood2","US",icon = icon("frown-open"), style = "pill",size = "xs",color = "success" ),
actionBttn("mood3","UK", icon = icon("meh"), style = "pill",size = "xs", color = "primary"),
actionBttn("mood4","Canada", icon = icon("meh"), style = "pill",size = "xs", color = "primary")
)), immediate = TRUE)
clearInput()
shinyjs::disable("message")
shinyjs::hide("send")
lvl(lvl + 1)
},
# level 3
if(msg=='India')
{
insertUI(
selector = "#message",
where = "beforeBegin",
ui=div(class="chat-bubbles",
div(class="bubble admin",
img(),
wellPanel(
p("What Personal challenges are you facing?", emoji("raising_hand")))), tags$br(),
p(actionBttn("P1",paste(ind()[1,1]), icon=icon("user-friends"), color = "warning", style = "pill", size = "xs"),tags$br(), tags$br(),
actionBttn("P2",paste(ind()[2,1]),icon=icon("dollar-sign"), color = "success",style = "pill", size = "xs"),tags$br(), tags$br(),
actionBttn("P3",paste(ind()[3,1]),icon=icon("first-aid"), color = "primary", style = "pill", size = "xs")
)), immediate = TRUE)
shinyjs::disable("message")
shinyjs::hide("send")
lvl(lvl + 1)
},
if(paste(ind()[1,1])==TRUE)
{
insertUI(
selector = "#message",
where = "beforeBegin",
ui=div(class="chat-bubbles",
div(class="bubble admin",
img(),
wellPanel(
p("What Personal challenges are you facing?", emoji("raising_hand")))), tags$br(),
p(actionBttn("P1",paste(ind_2()[1,1]), icon=icon("user-friends"), color = "warning", style = "pill", size = "xs"),tags$br(), tags$br(),
actionBttn("P2",paste(ind_2()[1,1]),icon=icon("dollar-sign"), color = "success",style = "pill", size = "xs"),tags$br(), tags$br(),
actionBttn("P3",paste(ind_2()[1,1]),icon=icon("first-aid"), color = "primary", style = "pill", size = "xs")
)), immediate = TRUE)
shinyjs::disable("message")
shinyjs::hide("send")
lvl(lvl + 1)
}
) }
# 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",
img(),
p("Kindly provide a valid input."))
)
)
clearInput()
}
else
{
replyMessage(lvl(),input$message)
}
})
lapply(sprintf("mood%s", 1:4),
function(x)
{
observeEvent(input[[x]],{
emj$em<- as.numeric(sub("mood", "", x))
insertUI(selector = "#message", where = "beforeBegin",
p(paste(
replyMessage(lvl(),emojis[, emj$em])))
)
})
shinyjs::disable("mood1")
shinyjs::disable("mood2")
shinyjs::disable("mood3")
shinyjs::disable("mood4")
})
}
# Main Function
startConversation<- function()
{
clearInput()
insertUI(
selector = "#message",
where = "beforeBegin",
ui=div(class="chat-bubbles",
div(class="bubble admin",
tags$div(class = "chat_avatar",
tags$img(src = "chat-robot.gif",
class = "img-responsive")),
wellPanel(
p("Hey! Could I get your name?")
))))
getMessage(lvl)
}
startConversation()
}
shinyApp(ui,server)