2016-11-11 12 views
0

validate()機能の動作を示している。このexample app from the shiny docsを取る:表示検証エラーメッセージに一度だけ

## server.R 

`%then%` <- shiny:::`%OR%` 

shinyServer(function(input, output) { 

    data <- reactive({ 
    validate(
     need(input$data != "", "Please select a data set") %then% 
     need(input$data %in% c("mtcars", "faithful", "iris"), 
     "Unrecognized data set") %then% 
     need(input$data, "Input is an empty string") %then% 
     need(!is.null(input$data), 
     "Input is not an empty string, it is NULL") 
    ) 
    get(input$data, 'package:datasets') 
    }) 

    output$plot <- renderPlot({ 
    hist(data()[, 1], col = 'forestgreen', border = 'white') 
    }) 

    output$table <- renderTable({ 
    head(data()) 
    }) 

}) 

## ui.R 

shinyUI(fluidPage(

    tags$head(
    tags$style(HTML(" 
     .shiny-output-error-validation { 
     color: green; 
     } 
    ")) 
), 

    titlePanel("Validation App"), 

    sidebarLayout(
    sidebarPanel(
     selectInput("data", label = "Data set", 
     choices = c("", "mtcars", "faithful", "iris")) 
    ), 

    # Show a plot of the generated distribution 
    mainPanel(
     plotOutput("plot"), 
     tableOutput("table") 
    ) 
) 
)) 

同じので、アプリは一度の代わりに、二回validateエラーメッセージを表示させる方法はありますplotの場合はの検証が1回失敗し、tableの要素の場合は1回失敗しますか?

答えて

2

私はこの問題を解決するためのより良い方法があるはずと思いますが、これは私が持っていた唯一のアイデアです:

server.R

 ## server.R 

    `%then%` <- shiny:::`%OR%` 

    shinyServer(function(input, output) { 
      values <- reactiveValues(test=data.frame()) 
      data <- reactive({ 
        validate(
          need(input$data != "", "Please select a data set") %then% 
            need(input$data %in% c("mtcars", "faithful", "iris"), 
             "Unrecognized data set") %then% 
            need(input$data, "Input is an empty string") %then% 
            need(!is.null(input$data), 
             "Input is not an empty string, it is NULL") 
        ) 
        get(input$data, 'package:datasets') 
      }) 
      observeEvent(data(), { 
        if(!is.null(data())) { 
          values$test <- data() 
        } else { 
          values$test <- NULL 
        } 
      }) 

      output$plot <- renderPlot({ 
        hist(data()[, 1], col = 'forestgreen', border = 'white') 
      }) 

      output$table <- renderTable({ 
        head(values$test) 
      }) 

    }) 
関連する問題