我要尝试的目标:
在不同的单选按钮组上添加不同的标题。我通过创建两个具有相同ID和不同标签的radioButtons
小部件来做到这一点。
问题:
MWE:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
width = 3,
radioButtons("aspect","Structure",
choices = list("Option A" = "size",
"Option B" = "coh",
"Option C" = "bound")
),
hr(),
radioButtons("aspect","Composition",
choices = list("Option D" = "div")
)
),
mainPanel(
width = 9,
fluidRow(
h3(textOutput("aboutText"))
)
)
)
)
server <- function (input, output){
aspectDesc <- reactive({
switch(input$aspect,
size = "Alpha",
coh = " Beta",
bound = "Charlie",
div = "Delta")
})
output$aboutText <- renderText({paste("Text about ", aspectDesc())})
}
shinyApp(ui = ui, server = server)
我尝试过的内容
我完全不知所措。尽管这似乎是一个琐碎的问题。
任何帮助将不胜感激!
答案 0 :(得分:1)
经过一番努力,我找到了解决方案。
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
sidebarLayout(
sidebarPanel(
width = 3,
radioButtons("aspect1","Structure",
choices = list("Option A" = "size",
"Option B" = "coh",
"Option C" = "bound"),
selected = character(0)
),
hr(),
radioButtons("aspect2","Composition",
choices = list("Option D" = "div")
)
),
mainPanel(
width = 9,
fluidRow(
h3(textOutput("aboutText"))
)
)
)
)
server <- function (input, output, session){
aspectDesc <- reactiveVal("Delta")
onclick("aspect1", {
updateRadioButtons(session, "aspect2", choices = list("Option D" = "div"),
selected = character(0))
aspectDesc(switch(input$aspect1,
size = "Alpha",
coh = " Beta",
bound = "Charlie"))
})
onclick("aspect2",{
updateRadioButtons(session, "aspect1",
choices = list("Option A" = "size",
"Option B" = "coh",
"Option C" = "bound"),
selected = character(0))
aspectDesc("Delta")
})
output$aboutText <- renderText({paste("Text about ", aspectDesc())})
}
shinyApp(ui = ui, server = server)