reactive
の代わりにeventReactive
とactionButton
の代わりに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
は、アプリの読み込み時にプロットを表示する方法がありますか?