R语言 如何根据另一个单选按钮输入将单选按钮插入闪亮的 UI?



我正在尝试在 shiny 中创建一个基本的用户表单,我需要在此基础上执行一些计算。为了让我做到这一点,需要了解每个产品的材料和形状。有2种材料,每种材料有两种形状。

我想有一组用于材料的单选按钮......根据用户选择的材料,下一组单选按钮会出现该材料的相关形状。

用户可以选择两种材料 - 金或银。 形状的单选按钮将根据用户输入的材料显示。如果他们选择银色,形状应该是"圆形"或"方形"。如果他们选择金色,形状应该是"三角形"或"矩形"

提前感谢您的帮助。

我尝试使用insertUI函数和if语句,但无法根据需要插入按钮。

library(shiny)
library(shinydashboard)
header <- dashboardHeader(title = "My Calculator")
sidebar <- dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem("Main Menu", tabName = "main_menu", icon = icon("dashboard")))
body <- dashboardBody( 
tabItems(
tabItem(tabName = "main_menu",
h2("Please select a material and shape"), 
hr(), 
radioButtons(inputId = "material",
label = "Material: ",
choices = c("Silver" = "silver", 
"Gold" = "gold"))
))

ui <- dashboardPage(title = 'This is my Page title', header, sidebar, body, skin='blue')

server <- function(input, output, session) { 

}

形状的单选按钮将根据用户输入的材料显示。如果他们选择银色,形状应该是"圆形"或"方形"。如果他们选择金色,形状应该是"三角形"或"矩形">

您可以使用conditionalPanel().

注意:条件在 JavaScript 中,所以如果你正在编写不同的条件,请小心,因为它与 R 语法不同。

另请注意,这会创建两个单独的输入(每个银和金一个),因此您需要将其合并到您的输出中。

这将是您的新body

body <- dashboardBody( 
tabItems(
tabItem(tabName = "main_menu",
h2("Please select a material and shape"), 
hr(), 
radioButtons(inputId = "material",
label = "Material: ",
choices = c("Silver" = "silver", 
"Gold" = "gold")),
conditionalPanel(condition = "input.material == 'silver'", 
radioButtons("silver_shape",
label = "Shape:", 
choices = c("Round", "Square"))),
conditionalPanel(condition = "input.material == 'gold'", 
radioButtons("gold_shape",
label = "Shape:", 
choices = c("Triangle", "Rectangle")))
))
)

最新更新