'Why Is my R Shiny app not displaying properly?

I am clearly missing something here, but I am pretty new to Shiny apps (I have only every made a couple of them before), and I'm still learning the ropes of them.

This app (which will run on its own) works for the input side (a slider and a text input), but the output (which is supposed to be a table) will not display.

Here is the code:

# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

ui <- fluidPage(

    # Application title
    titlePanel("CHD Risk Calculator"),

    sidebarLayout(
        sidebarPanel(
            sliderInput("BMI",
                        "Your BMI (kg/m^2 OR (703*lbs)/in^2):",
                        min = 10,
                        max = 70,
                        value = 24),
            textInput("Age",
                      "Your Age:")
        ),

        mainPanel(
           tableOutput("")
        )
    )
)

server <- function(input, output) {

    inputdata <- reactive({
        data <- data.frame(
            MyBMI = as.integer(input$BMI),
            MyAge = as.integer(input$age))
        data
    })
    
    output$result <- renderTable({
        data = inputdata()
        chdrisk = -6.293 + (0.0292*data$BMI) + (0.07409*data$age)
        resultTable = data.frame(
            Result = "Your risk of Coronary Heart Disease (CHD) is",
            Risk = chdrisk)
        resultTable
        
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

What am I missing here?

Thank you!



Solution 1:[1]

You have a few things going on here

  1. Your tableOutput() has been given outputID=""; change this to "result"
  2. Your inputs for the slider and the text are called BMI and Age, but in the reactive, you refer to them as BMI and age
  3. The data frame in the reactive has two columns, MyBMI and MyAge, but later, you refer to them like this: data$BMI and data$age

Here is a corrected version

# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

ui <- fluidPage(
  
  # Application title
  titlePanel("CHD Risk Calculator"),
  
  sidebarLayout(
    sidebarPanel(
      sliderInput("BMI",
                  "Your BMI (kg/m^2 OR (703*lbs)/in^2):",
                  min = 10,
                  max = 70,
                  value = 24),
      textInput("Age",
                "Your Age:")
    ),
    
    mainPanel(
      tableOutput("result")
    )
  )
)

server <- function(input, output) {
  
  inputdata <- reactive({
    data <- data.frame(
      MyBMI = as.integer(input$BMI),
      MyAge = as.integer(input$Age))
    data
  })
  
  output$result <- renderTable({
    data = inputdata()
    chdrisk = -6.293 + (0.0292*data$MyBMI) + (0.07409*data$MyAge)
    resultTable = data.frame(
      Result = "Your risk of Coronary Heart Disease (CHD) is",
      Risk = chdrisk)
    resultTable
    
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 langtang