在 Shiny 上显示 class 个预测模型的概率
Show class probabilities of a prediction model on Shiny
我使用 Caret 训练了一个随机森林,现在使用 Shiny App 我上传了一个 .csv 文件作为测试集,以在 App 上查看上传测试集的 class。现在我需要在 Shiny 应用程序上绘制一个图来显示每个 class 概率。代码:
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 <- 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)
当然,一开始你要搞清楚你要表现什么:什么都没有?默认数据集?
最好!
我使用 Caret 训练了一个随机森林,现在使用 Shiny App 我上传了一个 .csv 文件作为测试集,以在 App 上查看上传测试集的 class。现在我需要在 Shiny 应用程序上绘制一个图来显示每个 class 概率。代码:
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 <- 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)
当然,一开始你要搞清楚你要表现什么:什么都没有?默认数据集?
最好!