Search code examples
rshinyconfusion-matrix

Showing the results in the result tab of Shiny


How can I output the results of a function (say confusionMatrix from caret package) on the result tab of the mainPanel in Shiny?

Here's what I have in server.R:

  #Create Confusion Matrix of Predictions
  ref = matrix(c("P", "N", "P", "P", "P", "P","N"), ncol=1)
  pred = matrix(c("P", "N", "N", "P", "P", "P","P"), ncol=1)
  output$confusionMat <- renderPrint({
    confusionMatrix(ref,pred)
  })

And here's what I have in ui.R:

 mainPanel(width = 4,
      tabsetPanel(
        #tabPanel("Plot", plotOutput("plot")),
        tabPanel("Result", selectInput("featureEx", "Feature Exploration",
                                       c("ABC", "AB", "AC", "A"), multiple = TRUE),
                          helpText("Prediction Results Using Testing data"),
                          dataTableOutput("confusionMat"),
                          capture.output("confusionMat"),
                          plotOutput("fePlot")
                         ),

When I enter the function in RStudio here's the result I get:

> confusionMatrix(ref,pred)
Confusion Matrix and Statistics

          Reference
Prediction N P
         N 1 1
         P 1 4

               Accuracy : 0.7143          
                 95% CI : (0.2904, 0.9633)
    No Information Rate : 0.7143          
    P-Value [Acc > NIR] : 0.6792          

                  Kappa : 0.3             
 Mcnemar's Test P-Value : 1.0000          

            Sensitivity : 0.5000          
            Specificity : 0.8000          
         Pos Pred Value : 0.5000          
         Neg Pred Value : 0.8000          
             Prevalence : 0.2857          
         Detection Rate : 0.1429          
   Detection Prevalence : 0.2857          
      Balanced Accuracy : 0.6500          

       'Positive' Class : N               

So I would like to show the confusion table in a nicely formatted matrix in shiny and I was hoping to be able to do so using outputTable which doesn't show anything and also show the plain text as well in the result tab. Currently nothing is shown in the mainPanel. Any solution?


Solution

  • I used verbatimTextOutput("confusionmatrix") and it showed the results in the mainPanel.