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

Shiny Application actionButton click on page load

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

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

有没有办法让 actionButton() 或某种提交在页面加载时发生,以便带有 wordcloud 的选项卡可以更新?或者,也许我只需要将变量设置为全局变量之类的。我不确定。我在这上面花了很多时间,但碰壁了。任何帮助将不胜感激。

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

服务器代码:

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,以便立即显示绘图,而无需输入和提交新文本。谢谢!

这是一个应该接近您想要的示例。诀窍是使用 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)

由于通常不鼓励使用 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)

在第一次加载时制作操作按钮 运行 的解决方案很简单。只需添加一个 ifelse 语句。

原文:

eventReactive(input$submit, ...

新:

eventReactive(ifelse(input$submit == 0, 1, input$submit), ...

是的,就是这么简单!