【发布时间】:2016-02-09 23:31:34
【问题描述】:
您好,我在 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 文件中还是可以从另一个文件中调用?
谢谢。
【问题讨论】:
-
您使用的是 output$contents,但随后您使用的是 tableOutput("table1")。试试 tableOutput("contents")
-
contents 将只显示上传的表格。我需要预测的类
-
您应该在 output$contents 语句的末尾打印您的集群,然后调用您的 output$table1
-
对不起,我对此有点陌生,请您说得更详细些。谢谢。
-
我认为没有输出,因为您的 output$table1 语句位于 output$contents 语句中。您应该使用第一条语句输出模型的结果,然后使用第二条语句输出为表格。有一个可重现的例子(一些数据)会有所帮助。
标签: r prediction shiny-server