I'm trying to implement a Shiny text prediction function based on what user has currently input. The ideal scenario is suggested words are displayed as buttons and use can click on one of them and the word will appear on the text input area. However, currently the program seems double triggering the first button so that each time user selects a word, another one (the 1st one) will also pop into the text input area.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textAreaInput("typingArea", NULL)
),
mainPanel(
uiOutput("UI")
)
)
)
server = function(input, output, session){
wordlist <- data.frame("word"=sample(LETTERS, 5), stringsAsFactors = F)
Gibberish <- reactive({
input$typingArea
return(sample(letters, 5))
})
output$UI = renderUI({
res <- Gibberish()
obsList <- list()
lapply(
1:5,
function(i) {
btnID <- paste0("btn", i)
if (is.null(obsList[[btnID]])) {
obsList[[btnID]] <<- observeEvent(input[[btnID]], {
mytext <- paste0(input$typingArea, res[i], " ")
updateTextAreaInput(session, "typingArea", value=mytext)
})
}
fluidRow(
actionButton(btnID, res[i]), br(), br()
)
}
)
})
}
shinyApp(ui,server)
I suspect that is because of the input$typingArea has been called within two different reactive functions. But I have no idea how to fix this problem. Appreciate for any suggestion.