2013-04-05 27 views
11

提交按鈕很有用,但我還沒有找到一個優雅的方式來抑制初始頁面加載的輸出。頁面加載時閃亮的submitButton行爲

例如,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) 
    }) 
}) 

回答

10

的一種方式是使用actionButton(從shiny-incubator封裝)和isolateHere's這是一篇介紹如何將兩者結合使用的文章。這是一種比submitButton更靈活的方法,這有點過於笨拙,不夠靈活。

3

我有一個文本字段輸入同樣的問題,我用了一個很骯髒的解決辦法:

我設置的初始字段值,以類似「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) 
    }) 
}