R 在“结果”选项卡中显示结果

R 在“结果”选项卡中显示结果,r,shiny,confusion-matrix,R,Shiny,Confusion Matrix,如何在主面板的“结果”选项卡上输出函数的结果(如插入符号包中的confusionMatrix) 以下是我在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$confusionMa

如何在主面板的“结果”选项卡上输出函数的结果(如插入符号包中的confusionMatrix)

以下是我在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)
  })
当我在RStudio中输入函数时,得到的结果如下:

> 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               

因此,我想在一个格式良好的矩阵中用shiny显示混淆表,我希望能够使用outputTable这样做,outputTable不显示任何内容,并且在result选项卡中也显示纯文本。当前主面板中未显示任何内容。任何解决方案?

捕获。基本软件包的输出,再加上
性能分析软件包的
textplot
,您可能会对这种情况感兴趣


capture.output
允许您以文本格式分别从
confusionMatrix
的输出中提取每个元素。请注意,您可以根据需要重新排列
capture.output
的输出,但在这个特定示例中,我不打算修改输出。然后,您必须将
capture.output
的输出传递到
textplot
,以便能够将输出渲染为闪亮的绘图

在上面提供的示例中,我将使用
renderPlot
而不是
renderPrint
,并使用上述两个函数,如下所示:

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  

我使用了
逐字输出(“confusionmatrix”)
,它在主面板中显示了结果。

我可以提出建议并要求您提供一个前进方向,以便我们可以轻松运行代码并查看结果吗?我将提供一个解决方案,但它不会重复,因为您没有提供完整的代码
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  
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)