【问题标题】:Shiny submitButton behavior at page load time页面加载时闪亮的 submitButton 行为
【发布时间】:2013-03-28 06:35:03
【问题描述】:

提交按钮很有用,但我还没有找到一种优雅的方式来抑制初始页面加载时的输出。

例如,Shiny 教程在加载时呈现输出。
http://rstudio.github.com/shiny/tutorial/#more-widgets

如何确保在按下提交按钮之前不调用任何反应函数?

这是上面链接示例的内联代码。

#ui.R
library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(

  # Application title.
  headerPanel("More Widgets"),

  # Sidebar with controls to select a dataset and specify the number
  # of observations to view. The helpText function is also used to 
  # include clarifying text. Most notably, the inclusion of a 
  # submitButton defers the rendering of output until the user 
  # explicitly clicks the button (rather than doing it immediately
  # when inputs change). This is useful if the computations required
  # to render output are inordinately time-consuming.
  sidebarPanel(
    selectInput("dataset", "Choose a dataset:", 
                choices = c("rock", "pressure", "cars")),

    numericInput("obs", "Number of observations to view:", 10),

    helpText("Note: while the data view will show only the specified",
             "number of observations, the summary will still be based",
             "on the full dataset."),

    submitButton("Update View")
  ),

  # Show a summary of the dataset and an HTML table with the requested
  # number of observations. Note the use of the h4 function to provide
  # an additional header above each output section.
  mainPanel(
    h4("Summary"),
    verbatimTextOutput("summary"),

    h4("Observations"),
    tableOutput("view")
  )
))



#server.R
library(shiny)
library(datasets)

# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {

  # Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })

  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })
})

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    一种方法是使用actionButton(来自shiny-incubator 包)和isolateHere's 解释如何将两者结合使用的文章。这本质上是一种比submitButton 更灵活的方法,submitButton 有点过于严厉,不够灵活。

    【讨论】:

      【解决方案2】:

      我在文本字段输入中遇到了同样的问题,我使用了一个非常肮脏的解决方法:

      我将初始字段值设置为“please_type_stuff_here”。在 server.R 中,我使用了一个 if 子句来决定是否有一些东西要返回给 ui:

      if (input$data=="please_type_stuff_here") {
      
      } else {
      # Return the requested dataset
        datasetInput <- reactive({
          switch(input$dataset,
                 "rock" = rock,
                 "pressure" = pressure,
                 "cars" = cars)
        })
      
        # Generate a summary of the dataset
        output$summary <- renderPrint({
          dataset <- datasetInput()
          summary(dataset)
        })
      
        # Show the first "n" observations
        output$view <- renderTable({
          head(datasetInput(), n = input$obs)
        })
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-25
        • 2018-04-30
        • 1970-01-01
        • 2017-07-15
        • 1970-01-01
        • 2020-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多