[英]Shiny R - Drop down list constantly resets to first in list when using observeEvent and updateVarSelection
When using observe event and update var selection it resets every time to top in drop down list 使用观察事件并更新var选择时,每次都会重置到下拉列表的顶部
I have a very large data set, when one unit is selected it filters the dataset so the next drop down list only contains parameters related to that unit to plot. 我有一个非常大的数据集,当选择一个单位时,它将过滤数据集,因此下一个下拉列表仅包含与要绘制的该单位有关的参数。 This works except that the drop down list refreshes on all inputs not just the changing of the unit, I want it to refresh only when the unit is changed.
除了下拉列表不仅在单位更改上刷新所有输入之外,它还可以工作,我希望它仅在单位更改时刷新。 ie the drop down list becomes unit a temperature, unit a pressure, unit a level etc when unit a is selected.
即,当选择单位a时,下拉列表将变为单位温度,单位压力,单位水平等。 unit b temperature, unit b pressure etc.
b单位温度,b单位压力等
However, if I chose unit b the drop down list becomes unit b temperature, unit b temperature etc with the temperature being the first in the list and automatically shown on the chart, if I click on the drop down list and chose pressure, the chart will briefly show the pressure but then apparently the observe event is triggered, the list resets itself and defaults back to temperature 但是,如果我选择单位b,则下拉列表将变为单位b温度,单位b温度等,其中温度为列表中的第一位,并自动显示在图表上;如果我单击下拉列表并选择压力,则图表会短暂显示压力,但随后显然会触发观察事件,列表会重置自身并默认返回到温度
some portions of code summerized for clarity and not here word for word 为了清楚起见,对代码的某些部分进行了总结,此处不再逐字逐句地介绍
# 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)
Your code hase several issues: 您的代码有几个问题:
Trace
Trace
varSelectInput
requires data varSelectInput
需要数据 TracePlot
in ui, so use renderUI
TracePlot
在UI,所以用renderUI
observeEvent
inside renderPlot
as it's reactive already. renderPlot
使用observeEvent
,因为它已经是反应性的。 Instead, use input$Unit
directly or to update data based on input$Unit
, call a reactive variable created before. input$Unit
或基于input$Unit
更新数据,调用之前创建的反应变量。 updateVarSelectInput
is redundant if you use the reactive value in varSelectInput("Parameter", ...)
already. varSelectInput("Parameter", ...)
使用反应性值,则updateVarSelectInput
是冗余的。 If you want to update selected
, your probably better with observe()
outside of renderPlot
selected
,最好在renderPlot
之外使用observe()
Try this one as a start, I explained what I've changed 首先尝试一下,我解释了我已更改的内容
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.