您好我在R中有预测模型,我需要客户在Web界面中输入他们的数据,以便分类器可以预测类。我使用Shiny包但没有输出。
第一步是让客户以csv格式输入他的数据。然后在后端使用该模型来预测输入文件的类。然后在Web界面中为客户提供每行的类。
########## Server
bw <- read.csv("bw.csv", header = T)
colnames(bw)[1] <- "class"
bw$class[bw$class=="1"] <- "A"
bw$class[bw$class=="2"] <- "B"
bw$class[bw$class=="3"] <- "C"
bw$class[bw$class=="4"] <- "D"
bw$class[bw$class=="5"] <- "E"
bw$class <- as.factor(bw$class)
bw[ , 2:59]<- sapply(bw[ , 2:59], as.numeric)
control <- trainControl(method="repeatedcv", number=5, repeats=2)
# train the LDA model set.seed(7)
modelLda <- train(class~., data=bw, method="lda", trControl=control)
library(shiny)
shinyServer(function(input, output) { output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
head(read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)) })
# classification
clusters <- reactive({
predict(modelLda, input$file1(), input$clusters) })
# Show clusters:
output$table1 <- renderTable({
input$clusters })
})
########### UI
library(shiny)
shinyUI(fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', '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")
)
)
))
最后一个问题,分类器模型是否应该包含在server.r文件中,还是可以从另一个文件中调用?
感谢。
答案 0 :(得分:1)
这里有一些东西可以帮助你开始(它对我有用)。我简化了它,但你会知道如何构建它。希望能帮助到你!
library(shiny)
library(caret)
bw <- read.csv("bw_test.csv", header = T)
colnames(bw)[1] <- "class"
bw$class[bw$class=="1"] <- "A"
bw$class[bw$class=="2"] <- "B"
bw$class[bw$class=="3"] <- "C"
bw$class[bw$class=="4"] <- "D"
bw$class[bw$class=="5"] <- "E"
bw$class <- as.factor(bw$class)
bw[ , 2:58]<- sapply(bw[ , 2:58], as.numeric)
control <- trainControl(method="repeatedcv", number=5, repeats=2)
# train the LDA model set.seed(7)
modelLda <- train(class~., data=bw, method="lda", trControl=control)
shinyServer(function(input, output) {
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
# inFile <- input$file1
# if (is.null(inFile))
# return(NULL)
#head(read.csv(inFile$datapath, header=input$header, sep=input$sep,
# quote=input$quote)) })
# classification
dInput = reactive({
in.file = input$file1
if (is.null(in.file))
return(NULL)
read.csv(in.file$datapath, header=input$header, sep=input$sep, quote=input$quote)
})
clusters <- reactive({
df <- dInput()
tt <- as.data.frame(predict(modelLda))
tt
})
# Show clusters:
output$table1 <- renderTable({
toprint = clusters()
head(toprint)
})
})
编辑:现在它会在打印表格之前等待下载文件,但现在你可以自己动手了:)
shinyServer(function(input, output) {
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
# inFile <- input$file1
# if (is.null(inFile))
# return(NULL)
#head(read.csv(inFile$datapath, header=input$header, sep=input$sep,
# quote=input$quote)) })
# classification
dInput = reactive({
in.file = input$file1
if (is.null(in.file))
return(NULL)
bw <- read.csv(in.file$datapath, header=input$header, sep=input$sep, quote=input$quote)
colnames(bw)[1] <- "class"
bw$class[bw$class=="1"] <- "A"
bw$class[bw$class=="2"] <- "B"
bw$class[bw$class=="3"] <- "C"
bw$class[bw$class=="4"] <- "D"
bw$class[bw$class=="5"] <- "E"
bw$class <- as.factor(bw$class)
bw[ , 2:58]<- sapply(bw[ , 2:58], as.numeric)
bw
})
clusters <- reactive({
df <- dInput()
if (is.null(df))
return(NULL)
control <- trainControl(method="repeatedcv", number=5, repeats=2)
modelLda <- train(class~., data=df, method="lda", trControl=control)
tt <- as.data.frame(predict(modelLda))
tt
})
# Show clusters:
output$table1 <- renderTable({
toprint = clusters()
head(toprint)
})
})