2013-04-05 18 views
11

Przycisk submit jest przydatny, ale nie znalazłem eleganckiego sposobu na zniesienie danych wyjściowych podczas ładowania strony początkowej.Błyskawiczne zachowanie submitButton w czasie ładowania strony

Na przykład samouczek Shiny renderuje dane wyjściowe podczas ładowania.
http://rstudio.github.com/shiny/tutorial/#more-widgets

Jak upewnić się, że żadne funkcje reaktywne nie zostaną wywołane, dopóki nie zostanie naciśnięty przycisk przesyłania?

Oto kod wbudowany w przykładzie powyżej.

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

Odpowiedz

10

Jednym ze sposobów jest użycie actionButton (z pakietu shiny-incubator) i isolate. Here's Zapis, który wyjaśnia, w jaki sposób korzystać z tych dwóch elementów. Jest to z natury bardziej elastyczne podejście niż submitButton, które jest zbyt trudne w obsłudze i mało elastyczne.

3

miałem ten sam problem dla wejścia pola tekstowego i użyłem bardzo brudne prace wokół:

ustawić wartość początkową polową na coś takiego jak „please_type_stuff_here”. W serwerze.R użyłem klauzuli if, aby zdecydować, czy jest coś, co należy zwrócić do interfejsu użytkownika lub nie:

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) 
    }) 
} 
Powiązane problemy