Reputation: 38225
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?
Upvotes: 2
Views: 1587
Reputation: 38225
I used verbatimTextOutput("confusionmatrix")
and it showed the results in the mainPanel.
Upvotes: 1
Reputation: 574
capture.output
, from the base package, coupled with textplot
from the PerformanceAnalytics
package may interest you in this case.
capture.output
lets you extract every single element from the output of confusionMatrix
separately in text format. Note that you may re-arrange the output of capture.output
however you want but in this particular example I'm not going to modify the output. Then you will have to pass the output of capture.output
to textplot
in order to be able to render the output as a plot in Shiny.
In the example you provided above, instead of renderPrint
I would use renderPlot
and utilize the two functions mentioned above as follows:
require(PerformanceAnalytics)
output$confusionMat <- renderPlot({ #Replaced renderPrint with renderPlot
textplot( #wrap textplot around capture.output
capture.output( #capture output of confusionMatrix in text format
confusionMatrix(ref,pred) #your original code here
) #close capture.output
) #close textplot
}) #close renderPlot
But if you REALLY want to use renderDataTable
you will need to make sure that you pass a dataframe to renderDataTable
.
Here's a fully reporducible example with a different data set. If you run the code below, the results will look ugly and that's because I did not take the time to re-arrange the output of capture.output
to my liking. If you want to use renderDataTable
you will need to spend some time to change the layout of the dataframe being passed to renderDataTable
:
require(shiny)
require(caret)
require(PerformanceAnalytics)
server <- function(input,output,session){
output$confusionMat <- renderDataTable({
data.frame(
capture.output(
confusionMatrix(iris$Species, sample(iris$Species))
)
)
})
}
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
dataTableOutput('confusionMat')
)
)
))
shinyApp(ui = ui, server = server)
Upvotes: 2