osbwh
osbwh

Reputation: 79

unable to pass selectInput to output$table summary in r shiny

In my first month using Shiny. I want to create a simple app to be used by my school to show a summary of passed and failed students. Here's a reproducible example and I'll point to the error:

library(shiny)

Physiology <- c(49, 64, 74, 84)
Physiology2 <- ifelse(Physiology < 50, "Fail", "Pass")

Biochemistry <- c(49, 46, 74, 84)
Biochemistry2 <- ifelse(Biochemistry < 50, "Fail", "Pass")

Second <- data.frame(cbind(Physiology, Physiology2, Biochemistry, Biochemistry2))

Second$Physiology <- as.numeric(as.character(Second$Physiology))
Second$Biochemistry <- as.numeric(as.character(Second$Biochemistry))

UI:

ui <- fluidPage(
            selectInput("Subject", 
                        "Choose Subject", 
                        choices=list("Physiology2", "Biochemistry2")),
            tableOutput("table")
            )            

Server:

server <- shinyServer(function(input, output) {
      output$table <- renderTable(as.table(summary({input$Subject})))
})

shinyApp(ui, server)

I receive the following error

'dimnames' applied to non-array

The issue here is that

input$Subject

in the server isn't passed. When I manually do

Second$Physiology2

The table is produced normally.

Appreciate any help and thanks in advance!

Upvotes: 2

Views: 189

Answers (1)

user9746775
user9746775

Reputation: 34

This is because you pass a name taken from input, not data. So your code is equivalent to:

as.table(summary("Physiology2"))

All you have to do here is subset the data:

output$table <- renderTable(as.table(summary(Second[[input$Subject]])))

Upvotes: 3

Related Questions