1
我是新來的R和我有這樣的代碼在Ui.R:[R閃亮:checkboxGroupInput價值
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「動態」?
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))
})
})
非常感謝你非常。
哦,太好了!非常感謝Ron的解釋。我現在明白好多了。還有一個問題:我必須在checkboxGroupInput:children中添加一個框。我如何區分選擇:男性 - 女性/男性 - 兒童/女性 - 兒童的長度是相同的?再次感謝你。 –
查看更新的代碼。有更多優雅的方法可以做到這一點,但是這個方法遵循代碼中的想法。 –
非常感謝Ron的幫助。你是國王。但是,一旦我完成了我的代碼,當checkboxGroupInput「sex」的框沒有被選中時,我仍然有一個關於取消選擇checkboxGroupInput「dynamic」的小問題。我試過了「selected = NULL,FALSE和character(0),但沒有一個能夠工作,你知道爲什麼嗎?請看我編輯的代碼,再次感謝Ron。 –