LEO-BEE
LEO-BEE

Reputation: 35

If else condition not responding in shiny , r

I will like to ask for help here as I am using Shiny in R for the first time. I am trying to display the values height and weight from the calculations and conversions done depending on the type of measurement system (imperial or metric) chosen. This is by using the if .. else statement in my script but it seems that the condition isn't working.In essence I want the bmi in "output$bmi" to be displayed based on the measurement system chosen.My code is as follows:

ui.r

library(shiny)

shinyUI(fluidPage(
    
  # Application title
  headerPanel("Healthy Weight Checker"),
    
  # Sidebar with 
  sidebarLayout(
    sidebarPanel(
       h4("Check your Body Mass Index (BMI) to see if you have a healthy weight."),
      textInput("text1", label = h4(" Your Name")),
      radioButtons("gender" , label = h4("Your gender"), list("Male","Female")), 
      numericInput('age', label = h4("Your Age"),0, min=0, max=120, step=1),
                    
                   
      # selectInput("stm", label= h4("System of Measurement"), c("Imperial (Pounds, Feet)" = "imperial", "Metric (Kilogrammes, Meters)" = "metric")),
               
      selectInput("sm", label=h3("Select Sytem of Measurement"), choices=c("Metric", "Imperial")), 
      fluidRow(          
        column(6, numericInput('height', 'Height',0, min = 0, max = 7, step= 0.01)),
        column(6,numericInput('weight', 'Weight',0, min = 0, max = 300, step= 0.01))
      ),
            
      submitButton("Calculate"),
      br(),
      br()
    ),    
               
    mainPanel(
      p(h4("Your personal information")),
      textOutput("text1"),
      textOutput("gender"),
      textOutput("age"),
      textOutput("sm"),
      textOutput("height"),
      textOutput("weight"),
      textOutput("bmi")
    )
  )            
))

server.r

shinyServer(function(input, output) {
   
  output$text1 <- renderText({ 
    paste("Name: ", input$text1)
  })
  output$gender <- renderText({ 
    paste("Gender: ", input$gender)
  })
  output$age <- renderText({ 
    paste("Age: ", input$age)
  })
  output$sm <- renderText({ 
    paste("Measurement system: ", input$sm)
  })
  output$height <- renderText({ 
    paste("Your height: ", input$height)
  })
  output$weight <- renderText({ 
    paste("Your weight: ", input$weight)
  })
   
    
  values <- reactiveValues()         
  observe({
    if(input$sm == 'metric'){
      values$obj <- as.numeric(input$weight/(input$height)^2)   
    } else {
      values$obj <- values$obj * 730
    }
  })
  
  output$bmi <- renderText({ # Print the result to the main panel
    obj <- values$obj
    print(obj)
  })
})

Upvotes: 0

Views: 98

Answers (1)

K. Rohde
K. Rohde

Reputation: 9676

Your code is fine, but your if statement is not working, because you spelled the value of input$sm wrong. It should be "Metric" with a capital "M".

Other than that, the else part has faulty logic, because values$obj might not exist. But I guess you can continue on your own now.

Upvotes: 1

Related Questions