【问题标题】:Show class probabilities of a prediction model on Shiny在 Shiny 上显示预测模型的类别概率
【发布时间】:2019-01-31 00:07:44
【问题描述】:

我使用 Caret 训练了一个随机森林,现在使用 Shiny App 我上传了一个 .csv 文件作为测试集,以在应用程序上查看上传的测试集的类。现在我需要在 Shiny 应用程序上绘制一个图来显示每个类别的概率。代码:

library(caret)
library(shiny)
library(randomForest)
data("iris")
train_control <- trainControl(method="cv", number=3,savePredictions = 
TRUE,classProbs = TRUE)
model <- train(Species~., data=iris, trControl=train_control, method="nb")
ui=fluidPage(
titlePanel("Prediction Result"),
sidebarLayout(
sidebarPanel(
fileInput('datafile', 'Choose CSV File',accept=c('text/csv','text/comma- 
separated-values,text/plain','.csv')),
     tags$hr(),
     checkboxInput('header', 'Header', TRUE),
     radioButtons('sep', 'Separator',
                  c(Comma=',',
                    Semicolon=';',
                    Tab='\t'),
                  ','),
    radioButtons('quote', 'Quote',
                  c(None='',
                    'Double Quote'='"',
                    'Single Quote'="'"),
                  '"')
 ),
  mainPanel(
     tableOutput("table1"),plotOutput("plot")
  )
  )
  )

  server=function(input, output) {
  dInput = reactive({
 in.file = input$datafile

 if (is.null(in.file))
     return(NULL)
  bw <- read.csv(in.file$datapath, header=input$header, sep=input$sep, 
  quote=input$quote)
   })
  clusters <- reactive({
  df <- dInput()
  if (is.null(df))
  return(NULL)
  tt <- as.data.frame(predict(model,df))
  tt
   })
  output$table1 <- renderTable({
  toprint = clusters()
 head(toprint)
 output$plot<-renderPlot({ plot(predict(model,df,type="raw"))
 })

 })
 }
shinyApp(ui=ui,server=server)

但是我得到以下错误:

 no applicable method for 'xtable' applied to an object of class "function"

我应该如何解决这个错误? 集合集合可以是以下.csv形式的数据框:

 structure(list(Sepal.Length = 4L, Sepal.Width = 4L, Petal.Length = 1L, 
Petal.Width = 0.2, Species = structure(1L, .Label = "setosa", class = 
"factor")), class = "data.frame", row.names = c(NA, 
-1L))

【问题讨论】:

  • output$table1 &lt;- renderTable({ output$plot&lt;- ... )} 真的是你想做的吗?

标签: r shiny shiny-server


【解决方案1】:

希望对你有帮助:

首先,您的代码在这里有些混乱:

 output$table1 <- renderTable({
  toprint = clusters()
 head(toprint)
 output$plot<-renderPlot({ plot(predict(model,df,type="raw"))
 })

 })

您将 output$plot 嵌套在 output$table1 中。正确的方法是:

  output$table1 <- renderTable({
    toprint = clusters()
    head(toprint)
  })
    output$plot<-renderPlot({ plot(predict(model,df,type="raw"))


  })

其次,因为你需要在生成df之前输入一些文件,所以当你运行代码时它是空的。

免得说您在一开始就将 df 指定为 iris,那么您的代码将起作用:

library(caret)
library(shiny)
library(randomForest)
data("iris")
df <- NULL
train_control <- trainControl(method="cv", number=3,savePredictions = 
                                TRUE,classProbs = TRUE)
model <- train(Species~., data=iris, trControl=train_control, method="nb")
ui=fluidPage(
  titlePanel("Prediction Result"),
  sidebarLayout(
    sidebarPanel(
      fileInput('datafile', 'Choose CSV File',accept=c('text/csv','text/comma- 
                                                       separated-values,text/plain','.csv')),
      tags$hr(),
      checkboxInput('header', 'Header', TRUE),
      radioButtons('sep', 'Separator',
                   c(Comma=',',
                     Semicolon=';',
                     Tab='\t'),
                   ','),
      radioButtons('quote', 'Quote',
                   c(None='',
                     'Double Quote'='"',
                     'Single Quote'="'"),
                   '"')
      ),
    mainPanel(
      tableOutput("table1"),plotOutput("plot")
    )
  )
)

server=function(input, output) {
  dInput = reactive({
    in.file = input$datafile

    if (is.null(in.file))
      return(NULL)
    bw <- read.csv(in.file$datapath, header=input$header, sep=input$sep, 
                   quote=input$quote)
  })
  clusters <- reactive({
    df <- dInput()
    if (is.null(df))
      return(NULL)
    tt <- as.data.frame(predict(model,df))
    tt
  })
  output$table1 <- renderTable({
    toprint = clusters()
    head(toprint)
  })
  output$plot<-renderPlot({
    if (is.null(df))
      return(NULL)
    plot(predict(model,df,type="prob"))


  })
}
shinyApp(ui=ui,server=server)

当然,你需要弄清楚你一开始展示的是什么:什么都没有?默认数据集?

最好的!

【讨论】:

  • 我尝试使用 iris 数据集使代码可重现,所以在我的真实编码中,我认为我不应该将 df 设置为任何东西(代码中的 df=iris)。 Shiny 应用程序的用户应该能够上传 .csv 文件作为模型的测试集,并查看所有类别概率的预测结果和条形图。在 iris 数据集上使用您的代码,在上传任何 .csv 文件之前,我会看到一个条形图。
  • 嗨!你说的对。您的代码的问题是,当您运行它时,这块 plot(predict(model,df,type="prob")) 会产生问题,因为 df 尚未定义,因为尚未上传任何文件。因此,您需要设置 NULL 或加载默认绘图而不是 predict(model,df,type="prob")
猜你喜欢
  • 2018-12-24
  • 2021-05-31
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多