r - 如何在闪亮的 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)
解决方案
您遇到的关键问题是您将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)
推荐阅读
- perl - 如何在不禁用严格的'refs'的情况下重命名perl __ANON__ sub?
- firefox - 如何在 Firefox 最新版本 81.0 中打开 Scratchpad
- php - how do i show an image in laravel with infyom crud generator
- html - 如何结合 Angular ngStyle 指令使用“repeat()”css 函数?
- python - Python XML 按属性/子项排序
- unity3d - Unity3d-如何检查我的角色是否在空中 2 秒?
- java - 意图/开始活动开始错误的活动?
- python - 如何在基于类的视图 django 中验证会话
- flutter - 使用 Riverpod Flutter 创建登录功能
- r - 在 Rstudio 的一个文件夹中获取多个文件