基于服务器值/反应值的 R Shiny ConditionalPanel

R Shiny ConditionalPanel based on Server value / Reactive value

我遇到了一个无法解决的问题。我的闪亮应用程序中有一个数据集,其中包含可以这么说的订单说明(称为 SPO)。用户输入 SPO 编号,然后我们检索订单的所有必要说明,他们完成请求并通过 Shiny 提交。

对于其中一项说明,我们需要向提出请求的用户提出一些额外的问题。我正在尝试使用条件面板显示这些附加问题。

示例代码:

library(shiny)
library(tidyverse)

TestData <- tibble(SPO = c(101, 102),
                   HandRail = c(0, 1))

ui <- fluidPage(
  
    titlePanel("Order Acceptance"),
   
    sidebarLayout(
      sidebarPanel(
        numericInput("SPONumber",
                     "SPO",
                     value = NULL,
                     step = 1),
        
        conditionalPanel(
          condition = "output.HandRail",
          checkboxInput("MobileHandrail", "Mobile Handrail Allowed?", value = FALSE, width = NULL))
        ),
    
    mainPanel(
      h3(textOutput("OutputText", container = span))
    )
    )
)
# Define server logic
server <- function(input, output, session) {
  
  output$HandRail <- reactive({
    
    HandRailNeeded <- ifelse(input$SPONumber > 0 &
      input$SPONumber %in% TestData$SPO & 
      TestData$Handrail[TestData$SPO == input$SPONumber] == 1,
      TRUE,
      FALSE)
    
    return(HandRailNeeded)
  })
  

  outputOptions(output, "HandRail", suspendWhenHidden = FALSE)
  
  OutputText <- renderText({"Just to serve as mainpanel"})
  
}

# Run the application 
shinyApp(ui = ui, server = server)

因此,对于 SPO 101,我不想看到复选框,而对于 102,我希望看到。

我读过一些答案,例如

但出于某种原因,我无法解决问题。

非常感谢您的帮助!

鲍勃

output.HandRail 不为空时计算结果为真:

Any object of which the value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement.

[Source]

它也需要在 JS 中进行条件检查:output.HandRail == true:

library(shiny)
# library(tidyverse)
library(dplyr)

TestData <- tibble(SPO = c(101, 102),
                   HandRail = c(0, 1))

ui <- fluidPage(titlePanel("Order Acceptance"),
                sidebarLayout(
                  sidebarPanel(
                    numericInput("SPONumber",
                                 "SPO",
                                 value = NULL,
                                 step = 1),
                    
                    conditionalPanel(
                      condition = "output.HandRail == true",
                      checkboxInput(
                        "MobileHandrail",
                        "Mobile Handrail Allowed?",
                        value = FALSE,
                        width = NULL
                      ),
                      style = "display: none;"
                    )
                  ),
                  mainPanel(h3(
                    textOutput("OutputText", container = span)
                  ))
                ))

server <- function(input, output, session) {
  output$HandRail <- reactive({
    HandRailNeeded <- ifelse(
      input$SPONumber > 0 &
        input$SPONumber %in% TestData$SPO &
        TestData$HandRail[TestData$SPO == input$SPONumber] == 1,
      TRUE,
      FALSE
    )
    print(HandRailNeeded)
    return(HandRailNeeded)
  })
  
  outputOptions(output, "HandRail", suspendWhenHidden = FALSE)
  OutputText <- renderText({
    "Just to serve as mainpanel"
  })
  
}

shinyApp(ui = ui, server = server)

请查找相关答案