我是R的新手,我在Ui.R中有这个代码:
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Test 1"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("sex", "sex", c("Men", "Women")),
textOutput("number")
),
mainPanel(
)
)
))
这段代码在Server.R中:
server <- shinyServer(function(session, input, output) {
observeEvent(input$sex, {
if (input$sex == "Men"){s<-1}
number<-as.numeric(s)
if (input$sex == "Women"){s<-2}
number<-as.numeric(s)
if (input$sex == "Men" & input$sex == "Women"){s<-5}
number<-as.numeric(s)
if (is.null(input$sex)) {s<-10}
number<-as.numeric(s)
output$number<-renderText({number})
})
})
所以,我不明白为什么当我勾选两个复选框时“数字”没有取值5,如果我取消选中这两个框,为什么“数字”不取值10?
如果你能帮我理解,请。
非常感谢。
编辑:如何取消选中checkboxGrouInput“dynamic”?
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Test 1"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput(inputId = "sex", label = "sex",choices =
c("Men", "Women", "Children")),
textOutput("number")
),
mainPanel(
checkboxGroupInput(inputId = "dynamic", label = "", choices =
list("Answer 1"="01", "Answer 2"="02",
"Answer 3"="03", "Answer 4"="04",
"Answer 5"="05", "Answer 6"="06",
"Answer 7"="07", "Answer 8"="08",
"Answer 9"="09", "Answer 10"="10",
"Answer 11"="11", "Answer 12"="12"))
)
)
))
和Server.R:
server <- shinyServer(function(session, input, output) {
output$number<-renderText({
if (is.null(input$sex) ){return(10)
updateCheckboxGroupInput(session,"dynamic",selected=character(0))}
men <- "Men" %in% input$sex
women <- "Women" %in% input$sex
children <- "Children" %in% input$sex
if (men & women & children){
s <- 8
updateCheckboxGroupInput(session,"dynamic",
selected=list("01","02","03","04","05","06",
"07","08","09","10","11","12"))
} else if (men & women){
s <- 5
updateCheckboxGroupInput(session,"dynamic",
selected=list("01","02","03","04","05","06","07","08"))
} else if (men){
s <-1
updateCheckboxGroupInput(session,"dynamic",
selected=list("01","02","03","04"))
} else if (women){
s <-2
updateCheckboxGroupInput(session,"dynamic",
selected=list("05","06","07","08"))
} else if (children) {
s <- 3
updateCheckboxGroupInput(session,"dynamic",
selected=list("09","10","11","12"))
}
return(as.numeric(s))
})
})
非常感谢你。
答案 0 :(得分:5)
您的代码中需要考虑的一些事项:
1)如果选择男性和女性,则输入$ sex的长度= 2(chr [1:2]&#34;男性&#34;&#34;女性&#34;)。这条线不会工作。
(input$sex == "Men" & input$sex == "Women")
2)在app init输入$ sex为NULL,你不能在if语句中使用null,如
if (input$sex == "Men")
3)你不需要observeEvent。 renderText对输入$ sex有反应。
我对您的代码进行了一些更改
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Test 1"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput(inputId = "sex", label = "sex",choices = c("Men", "Women")),
textOutput("number")
),
mainPanel()
)
))
server <- shinyServer(function(session, input, output) {
output$number<-renderText({
if (is.null(input$sex) ){return(10)}
if (length(input$sex) ==1){
if (input$sex == "Men"){
s<-1
} else if (input$sex == "Women"){
s<-2
}
} else{
return(5)
}
return(as.numeric(s))
})
})
shiny::shinyApp(ui, server)
更新 - 如果您有更多选项
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Test 1"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput(inputId = "sex", label = "sex",choices = c("Men", "Women", "Children")),
textOutput("number")
),
mainPanel()
)
))
server <- shinyServer(function(session, input, output) {
output$number<-renderText({
if (is.null(input$sex) ){return(10)}
men <- "Men" %in% input$sex
women <- "Women" %in% input$sex
children <- "Children" %in% input$sex
if (men & women & children){
s <- 8
} else if (men & women){
s <- 5
} else if (men){
s <-1
} else if (women){
s <-2
} else if (children) {
s <- 3
}
return(as.numeric(s))
})
})
shiny::shinyApp(ui, server)