2016-04-06 7 views
0

最初のselectInputウィジェットでデータセットを変更すると、誰にエラーが表示されるのですか? diamondsからmtcarsにデータセットを変更すると、エラーCould not find 'carat'input$binsになり、プロットでは1秒間しか表示されず、すべて正常に機能します。それはなぜ起こったのですか?光沢のある反応入力の警告を削除するには

library(shiny) 
library(ggplot2) 

data(diamonds) 
data(mtcars) 


ui <- fluidPage(
     column(3, 
      selectInput("data", "", choices = c('mtcars', 'diamonds')), 
      uiOutput('server_cols'), 
      uiOutput('server_bins') 
      ), 
     column(9, 
      plotOutput("plot") 
      ) 


) 

server <- function(input, output) { 

     data <- reactive({ 
      switch(input$data, 
        diamonds = diamonds, 
        mtcars = mtcars) 
     }) 

     output$server_cols <- renderUI({ 
      data <- data() 
      nam <- colnames(data) 
      selectInput('cols', "Choose numeric columns:", choices = nam[sapply(data, function(x) is.numeric(x))]) 
     }) 

     output$server_bins <- renderUI({ 
      if (!is.null(input$cols)) { 
        df <- data() 
        x <- eval(input$cols) 
        max_value <- max(df[,x]) 

        sliderInput('bins','Choose number of bins:', min = 0.1, 
           max = max_value, 
           value = max_value/2) 
      } 
     }) 

     output$plot <- renderPlot({ 
      if (!is.null(input$cols) & !is.null(input$bins)) { 
        basicData <- data() 
        var <- eval(input$cols) 

        ggplot(basicData, aes_string(var)) + 
           geom_histogram(binwidth = input$bins, color = 'white', fill = 'red') 
      }  

     }) 

} 

shinyApp(ui, server) 
+0

'carat'は' diamonds'データセットのカラム名です。 'mtcars'データセットを見てそこからカラム名を選ぶことができます。 –

+0

私は何が起こったのか理解しています。しかし、私はこの警告を避ける方法を知らない。 – Nicolabo

答えて

1

それぞれの出力オブジェクトは入力変数の変更に応答します。したがって、input$dataでデータセットを変更すると、プロットは再構築されますが、input$colsはまだ調整されていません。実際にはをoutput$plotの中に挿入して、input$dataを変更すると最大3回呼び出されることを確認してください。

解決策は、反応ロジックを再考し、要素が特定の変更にのみ応答して、何らかの応答「スレッド」を取得することです。

たとえば、input$dataは、output$server_colsをトリガーする必要があります。 output$server_binsは、input$cols(これは既にinput$dataが以前に変更されたことを意味するため)によってのみトリガーされます。最終的にoutput$plotは、input$binsの変更をリッスンするだけです(input$colsinput$dataの変更は、スレッドの最後にあるため、常にinput$binsの変更になります)。

私の提案はisolateです。

library(shiny) 
library(ggplot2) 

data(diamonds) 
data(mtcars) 


ui <- fluidPage(
    column(3, 
    selectInput("data", "", choices = c('mtcars', 'diamonds')), 
    uiOutput('server_cols'), 
    uiOutput('server_bins') 
    ), 
    column(9, 
    plotOutput("plot") 
    )  
) 

server <- function(input, output) { 

    data <- reactive({ 
    switch(input$data, diamonds = diamonds, mtcars = mtcars) 
    }) 

    output$server_cols <- renderUI({ 
    data <- data() 
    nam <- colnames(data) 
    selectInput('cols', "Choose numeric columns:", choices = nam[sapply(data, function(x) is.numeric(x))]) 
    }) 

    output$server_bins <- renderUI({ 
    if (!is.null(input$cols)) { 
     df <- isolate(data()) 
     x <- eval(input$cols) 
     max_value <- max(df[,x]) 

     sliderInput('bins','Choose number of bins:', min = 0.1, max = max_value, value = max_value/2) 
    } 
    }) 

    output$plot <- renderPlot({ 
    if (!is.null(isolate(input$cols)) & !is.null(input$bins)) { 
     basicData <- isolate(data()) 
     var <- eval(isolate(input$cols)) 

     ggplot(basicData, aes_string(var)) + 
     geom_histogram(binwidth = input$bins, color = 'white', fill = 'red') 
    }  
    })  
} 

shinyApp(ui, server) 

また、あなたが他の入力に応じて、入力要素を変更したい場合はupdateSelectInputupdateSliderInputに見たいと思うかもしれません。

+0

ありがとう!私は 'isolate'を試みましたが、実際にあなたの投稿はこの機能の仕組みを明らかにしました。ありがとう! – Nicolabo

関連する問題