闪亮的应用程序actionButton单击页面加载

时间:2016-04-19 16:00:51

标签: r shiny-server shiny

我正在使用navbarPage()类型构建一个Shiny应用程序。我有三个选项卡 - 初始选项卡有一个textInput()框,其中定义了默认文本。该页面的mainPanel()有一个直方图和一个表。在页面上加载这些更新并在基于该默认文本启动应用程序时反映正确的信息。

第二个标签应该根据默认文本显示wordcloud。当我切换到该选项卡时出现错误 - 如果我返回第一个选项卡并输入新文本并点击actionButton - wordcloud将更新,但在我执行该操作之前不会这样做。

当页面加载时,有没有办法让actionButton()或某种类型的提交发生,所以带有wordcloud的标签可以更新?或者我可能只需要变量全局或其他东西。我不确定。我花了很多时间在这上面并且已经撞墙了。任何帮助将不胜感激。

用户界面代码:

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()
           )
             )), 

服务器代码:

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")
  )
})

另外,我使用单个文件应用程序“app.R” - 不确定这是否是有用的信息。同样,在第一个选项卡上,默认文本显示在第一页加载中,我只想在页面加载时扩展到wordcloud,以便立即显示图,而无需输入和提交新文本。谢谢!

2 个答案:

答案 0 :(得分:3)

这是一个应该接近你想要的例子。诀窍是使用submitButton。 wordcloud将有一个基于初始输入的默认图,但是当你更改文本并按下提交按钮时它会改变。

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)

答案 1 :(得分:3)

由于通常不建议使用submitButton()来支持功能更强大的actionButton()(有关功能文档,请参见here),因此,以下是上述答案的一种版本,该版本结合使用actionButton()eventReactive()ignoreNULL = FALSE的组合,以便在启动应用程序时显示图。

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)