2013-04-05 20 views
11

Nút gửi rất hữu ích, nhưng tôi chưa tìm thấy cách thanh lịch để chặn đầu ra khi tải trang ban đầu.Hành vi gửi bóng ở thời gian tải trang

Ví dụ: hướng dẫn Sáng bóng hiển thị kết xuất khi tải. http://rstudio.github.com/shiny/tutorial/#more-widgets

Làm cách nào để đảm bảo rằng không có chức năng phản ứng nào được gọi cho đến khi nhấn nút gửi?

Đây là mã nội tuyến cho ví dụ được liên kết ở trên.

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

Trả lời

10

Một cách là sử dụng actionButton (từ shiny-incubator gói) và isolate. Here's một ghi chú giải thích cách sử dụng cả hai. Đây là phương pháp tiếp cận linh hoạt hơn so với submitButton, hơi quá nặng và không đủ linh hoạt.

3

tôi đã cùng một vấn đề cho một đầu vào trường văn bản và tôi đã sử dụng một tác phẩm RẤT bẩn xung quanh:

tôi thiết lập giá trị lĩnh vực ban đầu để một cái gì đó như "please_type_stuff_here". Trong máy chủ.R tôi đã sử dụng mệnh đề if để quyết định xem có cái gì cần trả về ui hay không:

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) 
    }) 
} 
Các vấn đề liên quan