如何为selectInput中的选项赋值

时间:2018-05-08 03:23:32

标签: r shiny

我目前正在开发一款需要处理用户输入值的Shiny App。在我的ui中,我创建了2个selectInput,其中一个请求性别,另一个请求年龄,这里是代码:

library(shiny)

ui <- fluidPage(
sidebarPanel(submitButton("Submit")),

mainPanel(tabsetPanel(type = "tabs",
tabPanel("Personal Information", selectInput("age", "What is your age?:",c("--" = "null","16-19" = "age1","20-24" = "age2","25-34" = "age3","35-44" = "age4")),
          selectInput("gender","What is your biological gender?:",c("--" = "null","male" = "gender1","female" = "gender2"))
),
tabPanel("Result", verbatimTextOutput("result"))
)))

然后,对于上面写的每个年龄组,我有两个值(每个性别一个),如下所示:

age1 <- c(1.00,1.00)
age2 <- c(11.11,0.75)
age3 <- c(3.39, 0.45)
age4 <- c(1.35, 0.09)  

所以现在在服务器上我写了这个:

server <- function(input, output) { 

age1 <- c(1.00,1.00)
age2 <- c(11.11,0.75)
age3 <- c(3.39, 0.45)
age4 <- c(1.35, 0.09)  

output$result <- renderPrint ({if(input$gender == "gender1") {

print(log10(input$age[1]))
}})}

我的意图是如果用户是男性并且选择特定年龄,结果将显示他的年龄组的值的log10(这是每个向量的第一个值),但显然这个代码没有&#39;工作。

我对光亮和r很新,所以这看起来有点愚蠢。希望有人可以帮助我:)。

1 个答案:

答案 0 :(得分:1)

您也可以尝试切换。另请尝试使用actionButton代替submitButton作为建议here

server <- function(input, output) { 

    data <- reactive({
          switch(input$age,
          "--" = c(NA,NA),
          "age1" =  c(1.00,1.00),
          "age2" = c(11.11,0.75),
          "age3" = c(3.39, 0.45),
          "age4" = c(1.35, 0.09)
    )
 })


 output$result <- renderPrint ({if(input$gender == "gender1") {
                  print(log10(data()[1]))
 }})}