[英]Shiny R - Drop down list constantly resets to first in list when using observeEvent and updateVarSelection
使用观察事件并更新var选择时,每次都会重置到下拉列表的顶部
我有一个非常大的数据集,当选择一个单位时,它将过滤数据集,因此下一个下拉列表仅包含与要绘制的该单位有关的参数。 除了下拉列表不仅在单位更改上刷新所有输入之外,它还可以工作,我希望它仅在单位更改时刷新。 即,当选择单位a时,下拉列表将变为单位温度,单位压力,单位水平等。 b单位温度,b单位压力等
但是,如果我选择单位b,则下拉列表将变为单位b温度,单位b温度等,其中温度为列表中的第一位,并自动显示在图表上;如果我单击下拉列表并选择压力,则图表会短暂显示压力,但随后显然会触发观察事件,列表会重置自身并默认返回到温度
为了清楚起见,对代码的某些部分进行了总结,此处不再逐字逐句地介绍
# Define UI for application that plots User Input and filtered data
ui <- navbarPage( "Viewer",
tabPanel("Chart" , plotOutput(outputId = "newPlot"),
box(selectInput("Unit", "Chose Unit",c(a,b,c),selected = NULL)),
box(varSelectInput("Parameter", "Select the Parameter
to Plot:",TracePlot, selected = NULL )),
))
# Define server logic
server <- function(input, output,session) { output$newPlot <-
renderPlot({
TracePlot<-filters on unit selected (input$Unit)
observeEvent(input$Unit, updateVarSelectInput(session,"Parameter",
"Select the Parameter to Plot:", TracePlot,selected = NULL) )
ggplot(Trace, aes_string(y=input$Parameter, x=input$Batch_Or_Step)) +
geom_point()
})
}
# Run the application
shinyApp(ui = ui, server = server)
您的代码有几个问题:
Trace
varSelectInput
需要数据 TracePlot
在UI,所以用renderUI
renderPlot
使用observeEvent
,因为它已经是反应性的。 而是直接使用input$Unit
或基于input$Unit
更新数据,调用之前创建的反应变量。 varSelectInput("Parameter", ...)
使用反应性值,则updateVarSelectInput
是冗余的。 如果要更新selected
,最好在renderPlot
之外使用observe()
首先尝试一下,我解释了我已更改的内容
library(shiny)
library(ggplot2)
# Define UI for application that plots User Input and filtered data
ui <- navbarPage(
"Viewer",
tabPanel(
"Chart" ,
plotOutput(outputId = "newPlot"),
box(selectInput("Unit", "Chose Unit", c("a","b","c"), selected = NULL)),
# uiOutput to render selectInput/varSelectInput in server session using
# reactive Values
box(uiOutput("selectParameter"))
)
)
# Define server logic
server <- function(input, output,session) {
# create reactive
TracePlot <- reactiveValues()
# update reactive with your filter
observe({
# Do your filtering here and save to TracePlot$filter
TracePlot$filter <- input$Unit
})
# From ui to server with renderUI - change to varSelectInput again if data
# provided
output$selectParameter <- renderUI({
req(TracePlot$filter)
selectInput(
"Parameter",
"Select the Parameter to Plot:",
input$Unit,
selected = NULL
)
})
# cannot plot without data and Trace
output$newPlot <- renderPlot({
req(TracePlot$filter)
#ggplot(Trace, aes_string(y=input$Parameter, x=input$Batch_Or_Step)) +
#geom_point()
})
}
# Run the application
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.