r - 在 Shiny 的结果选项卡中显示结果

标签 r shiny confusion-matrix

如何在 Shiny 的 mainPanel 的结果选项卡上输出函数的结果(比如来自 caret 包的 fusionMatrix)?

这是我在 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)
  })

这是我在 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")
                         ),

当我在 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来做到这一点,它不显示任何内容,并且还在结果选项卡中显示纯文本。目前主面板中没有显示任何内容。有什么解决办法吗?

最佳答案

在这种情况下,您可能会对来自基础包的

capture.output 以及来自 PerformanceAnalytics 包的 textplot 感兴趣。

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

在上面提供的示例中,我将使用 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  

但是,如果您确实想使用 renderDataTable,则需要确保将数据帧传递给 renderDataTable

这是一个使用不同数据集的完全可重现的示例。如果运行下面的代码,结果看起来会很难看,这是因为我没有花时间根据自己的喜好重新排列 capture.output 的输出。如果您想使用renderDataTable,您将需要花一些时间来更改传递给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)

关于r - 在 Shiny 的结果选项卡中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28845948/

相关文章:

r - TraMineR 的并行计算

r - R 数据集中的 TextInput 过滤器 Shiny

mysql - 如何获取 R Shiny 响应式(Reactive)下拉框中所选选项的值?

python-3.x - 线性回归中的混淆矩阵

python - Azure ML 中混淆矩阵中每个单元格的项目数的浮点值

python-3.x - 如何对数组列表中的所有数组求和?

c++ - 如何获取R的辅助函数的C/C++源代码?

r - 从 sae.dnn (deepnet) 转换为 mx.mlp (mxnet) 错误

r - 多级建模的协方差结构

r - 在 Shiny 应用程序部署中隐藏 key