繁体   English   中英

闪亮的R-下拉列表在使用observeEvent和updateVarSelection时始终重置为列表的第一位

[英]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)

您的代码有几个问题:

  1. 重现性:发布前,请尝试在刷新的R会话中运行您的代码段
    • 您在单位选择器中的替代品丢失了
    • 您在用户界面的末尾使用了逗号
    • 您没有定义变量Trace
    • 缺少任何示例数据可直接帮助您使用代码。 这是必需的,因为varSelectInput需要数据
  2. 不能使用TracePlot在UI,所以用renderUI
  3. 反应性:您无需在renderPlot使用observeEvent ,因为它已经是反应性的。 而是直接使用input$Unit或基于input$Unit更新数据,调用之前创建的反应变量。
  4. 如果已经在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM