【问题标题】:How to clear the mainPanel if a selectInput choice has changed?如果 selectInput 选项已更改,如何清除 mainPanel?
【发布时间】:2022-08-19 14:54:31
【问题描述】:

我正在尝试创建一个应用程序,它将根据selectInput 向您显示结果,并且更改由actionButtons 控制。

当您启动应用程序时,您必须选择一个选项:数据 1 或数据 2。一旦您选择了您的选择(例如数据 1),您必须单击actionButton“提交数据类型”。接下来,转到第二个选项卡,选择一列,然后单击“提交”。 输出将是:一张表格、一张文字和一张图。

然后,如果您返回第一个选项卡并选择“数据 2”,您生成的所有内容都仍然存在(正如预期的那样,因为您没有单击任何按钮)。

但是,如果我更改我的第一个selectInput,我想删除mainPanel 中的所有内容,正如您在第一次启动应用程序时所看到的那样。 这个想法是,由于您已经更改了您的第一个选择,您将不得不再次执行相同的步骤(再次单击所有内容)。

我想使用actionButtons 来保存和控制更新,就像我在代码中所做的那样(因为我正在处理非常长的数据集,并且我不想依赖于加载我不喜欢的东西的速度直到我点击按钮)。尽管如此,如果我更改第一个selectInput 的选择,我想不出办法从mainPanel 中删除所有内容。

有人知道我如何实现这一目标吗?

提前致谢

代码:

library(shiny)
library(shinyWidgets)
library(shinyFeedback)
library(DT)
library(datasets)

ui <- fluidPage(
  sidebarPanel(
    tabsetPanel(id=\"histogram\",
                
                tabPanel(\"Selection\",
                         
                  useShinyFeedback(),
                  selectInput(inputId = \"type\", label = \"Select your data\", 
                              choices =  c(\"Data 1\" = \"data1\",
                                           \"Data 2\" = \"data2\")),
                  conditionalPanel(
                    condition = \"input.type == \'data2\'\",
                    div(style = \"position:absolute;right:2.5em;\",
                        actionButton(
                          inputId = \"button_more_info_data2\",
                          label = \"More info\",
                          icon = icon(\"info-circle\"))
                    )
                  ),
                  
                  actionButton(
                    inputId = \"button\",
                    label = \"Submit type of data\",
                    icon = icon(\"check\")
                  )
                ), 
              tabPanel(\"Pick the column\",
                       br(),
                       selectizeInput(inputId = \"list_columns\", label = \"Choose the column:\", choices=character(0)),
                       
                       actionButton(
                         inputId = \"button2\",
                         label = \"Submit\")
              ))
      
    
    
  ),
  mainPanel(
    dataTableOutput(\"table\"),
    textOutput(\"text\"),
    plotOutput(\"myplot\")
  )
)

server <- function(input, output, session) {
  
  observeEvent(input$type,{
    feedbackWarning(inputId = \"type\",
                    show = (\"data2\" %in% input$type),
                    text =\"This data is... Please, be careful...\"
    )
  })
  
  mydata <- reactive({
    if(input$type == \"data1\"){
      mtcars
    }else{
      iris
    }
    
  }) %>% bindEvent(input$button2)
  
  
  # This is to generate the choices (gene list) depending on the user\'s input.
  observeEvent(input$button, {
    updateSelectizeInput(
      session = session,
      inputId = \"list_columns\",
      choices = colnames(trees), options=list(maxOptions = length(colnames(trees))),
      server = TRUE
    )
  })
  
  output$table <- renderDataTable({
    req(input$button2)
    mydata()
    })
  
  output$text <- renderText({
    req(input$button2)
    input$list_columns
    })
  
  output$myplot <- renderPlot({
    req(input$button2, input$button)
    hist(trees[,input$list_columns])
  })
}

if (interactive())
  shinyApp(ui, server)
  • 一旦重新选择选择选项卡(或简单地实现一个重置按钮),重置mainPanel 输出是否有意义?另请检查bindCache
  • 是的,这就是我想要的,我想重置mainPanel 输出,但不是在重新选择选择选项卡时(因为您可以转到选项卡而不是出于某种原因更改selectInput)。可能吗?还是我需要一个重置按钮? (顺便说一句,感谢您提供更多信息,我不知道bindCache

标签: r shiny selectinput action-button


【解决方案1】:

这是一个使用重置按钮的示例 - 使用selectInput,您最终会得到circular reference

library(shiny)
library(shinyWidgets)
library(shinyFeedback)
library(DT)
library(datasets)

ui <- fluidPage(sidebarPanel(tabsetPanel(
  id = "histogram",
  tabPanel(
    "Selection",
    useShinyFeedback(),
    selectInput(
      inputId = "type",
      label = "Select your data",
      choices =  c("Data 1" = "data1",
                   "Data 2" = "data2")
    ),
    conditionalPanel(
      condition = "input.type == 'data2'",
      div(
        style = "position:absolute;right:2.5em;",
        actionButton(
          inputId = "button_more_info_data2",
          label = "More info",
          icon = icon("info-circle")
        )
      )
    ),
    actionButton(
      inputId = "button",
      label = "Submit type of data",
      icon = icon("check")
    ),
    actionButton(
      inputId = "reset",
      label = "Reset",
      icon = icon("xmark")
    )
  ),
  tabPanel(
    "Pick the column",
    br(),
    selectizeInput(
      inputId = "list_columns",
      label = "Choose the column:",
      choices = character(0)
    ),
    
    actionButton(inputId = "button2",
                 label = "Submit")
  )
)),
mainPanel(
  dataTableOutput("table"),
  textOutput("text"),
  plotOutput("myplot")
))

server <- function(input, output, session) {
  observeEvent(input$type, {
    feedbackWarning(
      inputId = "type",
      show = ("data2" %in% input$type),
      text = "This data is... Please, be careful..."
    )
  })
  
  mydata <- reactiveVal(NULL)
  
  observe({
    if (input$type == "data1") {
      mydata(mtcars)
    } else if (input$type == "data2") {
      mydata(iris)
    } else {
      mydata(data.frame())
    }
  }) %>% bindEvent(input$button2)
  
  observeEvent(input$reset, {
    mydata(data.frame())
  })
  
  # This is to generate the choices (gene list) depending on the user's input.
  observeEvent(input$button, {
    updateSelectizeInput(
      session = session,
      inputId = "list_columns",
      choices = colnames(trees),
      options = list(maxOptions = length(colnames(trees))),
      server = TRUE
    )
  })
  
  output$table <- renderDataTable({
    req(input$button2)
    mydata()
  })
  
  output$text <- renderText({
    req(input$button2)
    input$list_columns
  })
  
  output$myplot <- renderPlot({
    req(input$button2, input$button)
    hist(trees[, input$list_columns])
  })
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2013-04-16
    相关资源
    最近更新 更多