Dynamic Buttons Based on Text

148 views
Skip to first unread message

Michael O'Flaherty

unread,
Nov 20, 2014, 5:42:54 PM11/20/14
to shiny-...@googlegroups.com
Hi!

I am new to Shiny and I am trying to learn how to handle dynamic UI. I have a scenario where the user types text into a textInput control. I am planning on parsing the contents of the textbox, but want to expose a dynamic button that when the user clicks it, it appends text. Then the process needs to continue. So for example, the user types "I am" and the button will say "hungry." When "hungry" is appended to the textbox, the button would then need to change to the next word (for example "for.")

For the moment, I am testing this with a random state to try to get the button to change:

library(shiny)
runApp(
    shinyUI(fluidPage(
      textInput("sentence", "Type a sentence:"),
      uiOutput("uiOutputPanel"),
      h6(textOutput("sentenceEntered", container=span))
    ))
  ),
  server = function(input,output,session){
      output$sentenceEntered <- renderText({input$sentence})
      output$uiOutputPanel <- renderUI({
        word1 <- sample(state.name, 1)
        button1Click <- paste("$('#sentence').val($('#sentence').val() + ' ", word1, "')", sep='')
        tags$button(type="button", id="word1", word1, class="btn action-button shiny-bound-input" ,
                    onclick=button1Click)
      })
    })
  })
)

I assume this needs something like the "Observe" or "Reactive" construct, but everything I have tried has failed.

Is this possible, and if so, how do I accomplish it?

Thanks!
Michael

Joe Cheng [RStudio]

unread,
Nov 20, 2014, 5:54:22 PM11/20/14
to shiny-...@googlegroups.com
I tweaked your output$uiOutputPanel like this (changes in bold):

    output$uiOutputPanel <- renderUI({
      input$sentence # Just the act of reading input$sentence will cause this block to re-execute
      word1 <- sample(state.name, 1)
      button1Click <- paste("$('#sentence').val($('#sentence').val() + ' ", word1, "').trigger('change')", sep='')
      tags$button(type="button", id="word1", word1, class="btn action-button shiny-bound-input" ,
        onclick=button1Click)
    })

Without the .trigger('change'), that little snippet of JS I gave you doesn't actually push its updated value to Shiny. Sorry for the mistake.

This makes the button dynamic but as you get further I suspect you will need an observer and a couple of reactiveValues. What is this, a human-assisted markov chain generator? :)

Michael O'Flaherty

unread,
Nov 20, 2014, 8:19:03 PM11/20/14
to shiny-...@googlegroups.com
Thanks a bunch Joe! I would have never figured that out. Yes, I am building a word prediction model and testing it with Shiny. Do you have any good book recommendations for Shiny? Especially a cookbook? If not, you need to write one!

Michael O'Flaherty

unread,
Nov 20, 2014, 8:48:55 PM11/20/14
to shiny-...@googlegroups.com
Joe, can you explain tags$button? I would like to add two more buttons in there but it appears to only support 1 button. Thanks!


On Thursday, November 20, 2014 5:54:22 PM UTC-5, Joe Cheng [RStudio] wrote:

Dawine Pierre

unread,
Sep 3, 2015, 1:32:58 PM9/3/15
to Shiny - Web Framework for R
Hi joe this explanation was very helpful to me in word prediction application. But how do you deal with word with apostroph, for example: it's, aren't, won't.  I am struggling to append these words

Dawine
Reply all
Reply to author
Forward
0 new messages