首页 > 解决方案 > 如何在闪亮的 r 中使用条件面板()

问题描述

我正在尝试创建一个闪亮的应用程序,它允许您选择操作计算的输入。如果用户选择“加法”,它将显示两个数字输入框(因此他们可以输入两个数字),如果用户选择“平方”,它将仅显示一个数字输入框来平方。

有了这个,我使用conditionalPanel,如果满足条件,它会通过我想要uiOutput()的s 获取。numericInput正方形也是一样。

现在当我运行这个应用程序时,条件面板不会出现。我哪里做错了?感谢您查看我的问题。

ui <- fluidPage( theme = shinytheme("slate"),
                 titlePanel("Basic Calculator"),
                 sidebarPanel(
                          selectInput("ops","Select what Operation use",choices = c("ADDITION","SQUARE")),
                   
                          helpText("Please input the appropriate number depending on the operations"),
                          conditionalPanel("input.ops=='ADDITION'", uiOutput("var2")),
                          conditionalPanel("input.ops=='SQUARE'", uiOutput("var1"))
                         
                          ),#sidebar panel
                  
                 
                   
)#fluidpage

server <- function(input, output) {
  
  output$basicmath <- renderText( ifelse(input$ops=="ADDITION",input$a+input$b,
                                  ifelse(input$ops=="SUBTRACTION",input$a-input$b,
                                  ifelse(input$ops=="SQUARE",input$a*input$a,
                                  ifelse(input$ops=="SQUARE ROOT",sqrt(input$a),
                                  ifelse(input$ops=="MULTIPLICATION",input$a*input$b,"not a valid operation"))))),
   
  output$var2 <- renderUI({
                            helpText("this will show to input two numerics to be added")
    
                          }) ,
  
  output$var1 <- renderUI({
                           helpText("this will show to input one numeric to square")
    
  })                             
      
)}


shinyApp(ui = ui, server = server)

标签: rshiny

解决方案


您遇到的关键问题是您将uiOutputs预期的计算输出放入其中。最好将它们分开,因为计算输出在具有必要的先决条件值(您的输入值)之前不会运行。此外,由于您没有为 指定输出位置basicmath,应用程序不知道在对 的调用中放置任何内容的位置renderText()。下面是使正确的 UI 元素出现的工作代码。

您缺少的另一件事renderUI是使用tagList(). 这有助于确保将所有元素打包在一起,而不仅仅是最后一个。

请注意,数学部分不起作用,但看起来这只是一个占位符。当您开始使用它时,请务必为每个输入使用唯一的 ID。input$a您有几个and的实例input$b,这可能不是一种可行的方法。

library(shiny)
library(shinythemes)

ui <- fluidPage( theme = shinytheme("slate"),
                 titlePanel("Basic Calculator"),
                 sidebarPanel(
                   selectInput("ops","Select what Operation use",choices = c("ADDITION","SQUARE")),
                   helpText("Please input the appropriate number depending on the operations"),
                   conditionalPanel("input.ops=='ADDITION'", uiOutput("var2")),
                   conditionalPanel("input.ops=='SQUARE'", uiOutput("var1"))
                 ),
                 mainPanel(
                   textOutput("basicmath")
                 )
)#fluidpage

server <- function(input, output) {

  output$var2 <- renderUI({
    tagList(
      helpText("this will show to input two numerics to be added"),
      splitLayout(
        numericInput("var2a", label = "Input one number", value = NULL),
        numericInput("var2b", label = "Input another number", value = NULL)
      )
    )
  })
  
  output$var1 <- renderUI({
    tagList(
      helpText("this will show to input one numeric to square"),
      numericInput("var1a", label = "Input a number", value = NULL)
    )
  })
  
  output$basicmath <- renderText( {ifelse(input$ops=="ADDITION",input$a+input$b,
                                         ifelse(input$ops=="SUBTRACTION",input$a-input$b,
                                                ifelse(input$ops=="SQUARE",input$a*input$a,
                                                       ifelse(input$ops=="SQUARE ROOT",sqrt(input$a),
                                                              ifelse(input$ops=="MULTIPLICATION",input$a*input$b,"not a valid operation")))))
  })
}

shinyApp(ui = ui, server = server)

推荐阅读