Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny Application actionButton click on page load

I am building a Shiny application using the navbarPage() type. I have three tabs - the initial tab has a textInput() box that has default text defined. The mainPanel() of that page has a histogram and a table. On page load those update and reflect the proper information when the application is launched based on that default text.

The second tab is supposed to present a wordcloud based on that default text. When I switch over to that tab there is an error - if I go back to the first tab and enter new text and hit the actionButton - the wordcloud will update, but it won't do so until I perform that action.

Is there a way to have the actionButton() or some sort of submit happen when the page loads so the tab with the wordcloud can update? Or maybe I just need to make a variable global or something. I'm not sure. I've spent quite a bit of time on this and have hit a wall. Any help would be greatly appreciated.

Code for the UI:

tabPanel("Word Cloud Diagram",
         fluidRow(
           sidebarPanel(
             width = 3,
             h5("The sentence input:"),
             wellPanel(span(h5(textOutput(
               'sent'
             )), style = "color:red")),
             sliderInput(
               "maxWC",
               h5("Maximum Number of Words:"),
               min = 10,
               max = 100,
               value = 50
             ),
             br(),
             #actionButton("update", "Update Word Cloud"),
             hr(),
             helpText(h5("Help Instruction:")),
             helpText(
               "Please have a try to make the prediction by using
               the dashboard on right side. Specifically, you can:"
             ),
             helpText("1. Type your sentence in the text field", style =
                        "color:#428ee8"),
             helpText(
               "2. The value will be passed to the model while you are typing.",
               style = "color:#428ee8"
             ),
             helpText("3. Obtain the instant predictions below.", style =
                        "color:#428ee8"),
             hr(),
             helpText(h5("Note:")),
             helpText(
               "The App will be initialized at the first load.
               After",
               code("100% loading"),
               ", you will see the prediction
               for the default sentence example \"Nice to meet you\"
               on the right side."
             )
             ),
           mainPanel(
             h3("Word Cloud Diagram"),
             hr(),
             h5(
               "A",
               code("word cloud"),
               "or data cloud is a data display which uses font size and/
               or color to indicate numerical values like frequency of words. Please click",
               code("Update Word Cloud"),
               "button and",
               code("Slide Input"),
               "in the side bar to update the plot for relevant prediction."
             ),
             plotOutput("wordCloud"),
             # wordcloud
             br()
           )
             )), 

Code for the server:

wordcloud_rep <- repeatable(wordcloud)
output$wordCloud <- renderPlot({
  v <- terms()
  wordcloud_rep(
    v[, 2],
    v[, 1],
    max.words = input$maxWC,
    scale = c(5, 1.5),
    colors = brewer.pal(4, "Dark2")
  )
})

Also, I am using a single file application "app.R" - not sure if this is useful information or not. Again, on the first tab, default text is presented on the first page load, I just want this to extend to the wordcloud on page load so the plot is shown immediately without having to enter and submit new text. Thanks!

like image 278
azdatasci Avatar asked Apr 19 '16 16:04

azdatasci


2 Answers

Here is an example that should be close to what you want. The trick is to use a submitButton. The wordcloud will have a default plot based on initial input, but will change when you change the text and press the submit button.

library(shiny)
library(wordcloud)

ui <- shinyUI(fluidPage(

   titlePanel("Old Faithful Geyser Data"),

   sidebarLayout(
      sidebarPanel(
        textInput("text", "Input Text", "Random text random text random is no yes"),
        submitButton("Submit")
      ),

      mainPanel(  
          tabsetPanel(
              tabPanel("Tab1", 
                       plotOutput("hist"),
                       tableOutput("hist_table")),
              tabPanel("Tab2",
                       plotOutput("wordcloud"))
          )
      )
   )
))

server <- shinyServer(function(input, output) {

    observe({
        word_list = strsplit(input$text, " ")
        word_table = as.data.frame(table(word_list))

        output$hist = renderPlot({
            barplot(table(word_list))
        })
        output$hist_table = renderTable({
            word_table
        })
        output$wordcloud = renderPlot({
            wordcloud(word_table[,1], word_table[,2])
        })
    })

})

shinyApp(ui = ui, server = server)
like image 165
Xiongbing Jin Avatar answered Sep 29 '22 06:09

Xiongbing Jin


Since the use of submitButton() is generally discouraged in favour of the more versatile actionButton() (see here for function documentation), here is a version of the answer above that uses a combination of actionButton() and eventReactive() with ignoreNULL = FALSE so that the plots show up upon launching the app.

library(shiny)
library(wordcloud)

ui <- fluidPage(
  sidebarLayout(

    sidebarPanel(
      textInput("text", "Input Text", "Random text random text random is no yes"),
      actionButton("submit", "Submit")
    ),

    mainPanel(
      tabsetPanel(
        tabPanel(
          "Tab1",
          plotOutput("hist"),
          tableOutput("hist_table")
        ),
        tabPanel(
          "Tab2",
          plotOutput("wordcloud")
        )
      )
    )
  )
)

server <- shinyServer(function(input, output) {
  word_list <- eventReactive(input$submit,{
    strsplit(input$text, " ")
    },
    ignoreNULL = FALSE
  )

  word_table <- reactive(
    as.data.frame(table(word_list()))
  )

  output$hist <- renderPlot({
    barplot(table(word_list()))
  })
  output$hist_table <- renderTable({
    word_table()
  })
  output$wordcloud <- renderPlot({
    wordcloud(word_table()[, 1], word_table()[, 2])
  })

})

shinyApp(ui = ui, server = server)
like image 45
mine Avatar answered Sep 29 '22 06:09

mine