How to assign value to choices in selectInput

I am currently developing a Shiny App that needs to process the value of the user input. In my ui I created 2 selectInput with one asking for gender and another for age and here's the code:

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"))
)))

Then for each age group written above I have two values (one for each gender) , like this :

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

So now in the sever I wrote this:

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]))
}})}

My intention was if the user is a male and choose a particular age, the result will show the log10 of the value of his age group (which is the first value of each vector), but apparently this code doesn't work.

I'm quite new to shiny and r so this might seem a little dumb. Hope someone can help me :)

rshiny

Answers

answered 2 months ago A. Suliman #1

You can also try switch. Also try to use actionButton instead of submitButton as recommended 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]))
 }})}

comments powered by Disqus