r-我可以在UI的一部分使用单选按钮来创建标题吗



我想做的是根据用户选择的内容为面板生成一个标题。例如,如果用户选择用于"显示"的单选按钮;z";,我希望面板的标题说";z";。

我知道我可以使用很多conditionalPanel语句,比如conditionalPanel(condition="input.cochoice_crit==1",h3("First choice"((或其他什么,但使用我已经放入单选按钮选项列表的名称会更优雅。如果我在某个时候添加其他内容,更新会更容易。

所以这个代码是有效的,但显然不是动态的。有没有办法将[3]替换为单选按钮中选择的任何内容,以便h3((填充所选choice_crit中的适当名称?

library(shiny)
choice_crit <- c(1, 2, 3, 4)
names(choice_crit) <- c("z","t","U1D6D8U00B2","F")

# Define UI
ui <- fluidPage(

# Sidebar
sidebarLayout(

sidebarPanel(
h3(names(choice_crit[3])),
radioButtons(inputId = "crit_select",label = "Select the statistic:",choices = choice_crit),
),
# Main output
mainPanel(

)
)
)
# Define server logic
server <- function(input, output) {

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

这就是您想要的吗?您可以使用renderUIuiOutput来实现它


library(shiny)
choice_crit <- c(1, 2, 3, 4)
names(choice_crit) <- c("z","t","U1D6D8U00B2","F")

# Define UI
ui <- fluidPage(


# Sidebar
sidebarLayout(

sidebarPanel(
uiOutput("title"),
radioButtons(inputId = "crit_select",
label = "Select the statistic:", 
choices = choice_crit),
),

# Main output
mainPanel(

)
)
)
# Define server logic
server <- function(input, output) {
output$title <- renderUI({
h3(names(choice_crit)[as.numeric(input$crit_select)])
#h3(names(choice_crit)[as.character(choice_crit) == input$crit_select]) # Alternative
})

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

此外,您应该注意radioButtions:的文档

choices:要从中选择的值的列表(如果列表中的元素已命名,则会向用户显示该名称而不是值(。如果提供了此参数,则不得提供choiceNames和choiceValue,反之亦然值应该是字符串;其他类型(如逻辑和数字(将被强制为字符串

因此,input$crit_select最终成为character向量,取值为"1""4"

一种方法是将h3封装在uiOutput中,在UI:中

uiOutput("h3ui"),

在服务器中:

output$h3ui <- renderUI({
HTML(paste0("<h3>",input$crit_select,"</h3>"))
})

最新更新