2017-08-01 3 views
1

reactiveの代わりにeventReactiveactionButtonの代わりにeventReactiveを使用すると、ページをリフレッシュするときの動作が異なることがわかります。Shiny App:eventReactiveと反応が一貫しないactionButtonの動作が「更新」

たとえば、このシンプルなShashダッシュボードは、期待通りに動作します。読み込み時にプロットが表示され、フィルタを変更して、更新ボタンをクリックするとプロットが更新されます。

# app1.R 

library(shiny) 
library(dplyr) 
library(ggplot2) 

species <- levels(iris$Species) 

ui <- fluidPage(
    sidebarLayout(
    sidebarPanel(
     selectInput("species", "Select Iris Species", 
      choices = species, selected=species, multiple = TRUE), 
     actionButton("refresh", "Refresh") 
    ), 
    mainPanel(plotOutput("scatterplot")) 
) 
) 

server <- function(input, output) { 

    selected_data <- reactive({ 
    input$refresh 
    isolate({ 
    iris %>% filter(Species %in% input$species) 
    }) 
    }) 

    output$scatterplot <- renderPlot({ 
    plot(selected_data()) 
    }) 
} 

shinyApp(ui = ui, server = server) 

私は

selected_data <- eventReactive(input$refresh, { 
    iris %>% filter(Species %in% input$species) 
}) 

私はselected_dataだけinput$refreshに依存していることを明示します。この方法でselected_dataためのコードを置き換えることができても、私のactionButtonの理解とeventReactive Iに基づいています。

しかし、アプリケーションは私が期待どおりに動作しません。読み込み時にプロットが表示されず、明示的に「更新」をクリックしてプロットを表示する必要があります。その後、すべてが意図どおりに機能します。ここ

アプリの第2のバージョンの完全なコードである:

# app2.R 

library(shiny) 
library(dplyr) 
library(ggplot2) 

species <- levels(iris$Species) 

ui <- fluidPage(
    sidebarLayout(
    sidebarPanel(
     selectInput("species", "Select Iris Species", 
      choices = species, selected=species, multiple = TRUE), 
     actionButton("refresh", "Refresh") 
    ), 
    mainPanel(plotOutput("scatterplot")) 
) 
) 

server <- function(input, output) { 

    selected_data <- eventReactive(input$refresh, { 

    iris %>% filter(Species %in% input$species) 

    }) 

    output$scatterplot <- renderPlot({ 
    plot(selected_data()) 
    }) 
} 

shinyApp(ui = ui, server = server) 

ドキュメントによれば、光沢のあるアプリケーションが実行されたときinput$refreshが交互にトリガし、0にNULLの値を変更しているはずselected_dataの評価およびプロットを示す。

私は理由を説明できますか?app1.Rアプリが読み込まれるときにプロットが表示され、app2.Rを手動で更新する必要がありますか?

app2.Rは、アプリの読み込み時にプロットを表示する方法がありますか?

答えて

1

ignoreNULLあり、それがデフォルトでTRUEに設定されている、あなたはFALSEにそれを変更した場合、それが引き金となりますeventReactive内の引数があります。詳細は?eventReactive

library(shiny) 
library(dplyr) 
library(ggplot2) 

species <- levels(iris$Species) 

ui <- fluidPage(
    sidebarLayout(
    sidebarPanel(
     selectInput("species", "Select Iris Species", 
        choices = species, selected=species, multiple = TRUE), 
     actionButton("refresh", "Refresh") 
    ), 
    mainPanel(plotOutput("scatterplot")) 
) 
) 

server <- function(input, output) { 

    selected_data <- eventReactive(input$refresh, { 
    iris %>% filter(Species %in% input$species) 
    },ignoreNULL = F) 

    output$scatterplot <- renderPlot({ 
    plot(selected_data()) 
    }) 
} 

shinyApp(ui = ui, server = server) 
を参照してください。
関連する問題