【问题标题】:web interface for R prediction model using Shiny使用 Shiny 的 R 预测模型的 Web 界面
【发布时间】: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


【解决方案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)
            })

        })

【讨论】:

  • 您好,感谢您的回复,但是在输入新的测试集之前会弹出预测的类。我不知道原因。
  • 你将不得不改变一些事情,但我只是想重组你以前服务器中的各种语句。我对 Caret 不是很熟悉,但我希望,您需要在此处更改一些内容 tt
  • 在为函数 'as.data.frame' 选择方法时评估参数 'x' 时出现以下错误: predict.train(modelLda, input$dInput()) 中的错误:尝试应用非函数
  • 如果这是你想要的,你可以接受答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2019-01-21
  • 1970-01-01
  • 2015-11-04
  • 2020-07-18
  • 1970-01-01
相关资源
最近更新 更多