如何从用户上传的文件生成模型?

时间:2019-01-19 16:40:49

标签: r shiny linear-regression

我创建了一个文件上传部分,可以上传和读取CSV文件作为表格:

ui <- fluidPage(

titlePanel("Upload Transaction Data Set"),

 sidebarLayout(

    sidebarPanel(

  fileInput("file1", "Choose CSV File",
            multiple = FALSE,
            accept = c("text/csv",
                     "text/comma-separated-values,text/plain",
                     ".csv")),
  tags$hr(),

  checkboxInput("header", "Header", TRUE),

  radioButtons("sep", "Separator",
               choices = c(Tab = "\t"),
               selected = "\t"),

  radioButtons("quote", "Quote",
               choices = c(None = "",
                           "Double Quote" = '"',
                           "Single Quote" = "'"),
               selected = '"'),

  tags$hr(),

   radioButtons("disp", "Display",
               choices = c(Head = "head",
                           All = "all"),
               selected = "head")

),

mainPanel(

  tableOutput("contents")
       )
)

我还创建了线性回归模型,该模型从数据集中获取输入:

thedata <- readxl::read_xlsx("data/transactionDataAlteredXLSX.xlsx")

set.seed(2)
library(caTools)
split <- sample.split(thedata, SplitRatio=0.7)

train <- subset(thedata, split=TRUE)
Actual <- subset(thedata, split=FALSE)


# Create the model
Model <- lm(Class ~.,data=train)
#Prediction
Prediction <- predict(Model, Actual)

#Comparing predicted vs actual model
plot(Actual$Class,type = "l",lty= 1.8,col = "red")
lines(Prediction, type = "l", col = "blue")
plot(Prediction,type = "l",lty= 1.8,col = "blue")
#Finding Accuracy

shinyApp(ui, server)

如何使线性回归模型形成文件上传输出,而不是来自数据集“ thedata”?

谢谢。

编辑:

按照服务器代码中的建议将文件转换为数据帧,然后在回归代码上将“ thedata”更改为“ df”,但是现在我收到错误消息“ unique()仅适用于矢量数据帧”,任何想法?:

server <- function(input, output) {

output$contents <- renderTable({

req(input$file1)

df <- read.csv(input$file1$datapath,
         header = input$header,
         sep = input$sep,
         quote = input$quote)

if(input$disp == "head") {
  return(head(df))
}
else {
  return(df)
}

})

}

1 个答案:

答案 0 :(得分:0)

这是您随附的代码的简化版本。我选择的文件是标准mtcars数据集。您可以通过调用以下函数将其保存到计算机中:

write.csv(mtcars,file = "~/Desktop/Data.csv",row.names = FALSE)

我在这里所做的工作是在完整的训练数据集中读取的,执行简单的线性回归,并针对样本测试数据集输出预测的数据帧(我现在对此进行了硬编码)。

ui <- fluidPage(

  titlePanel("Upload Transaction Data Set"),

  sidebarLayout(

    sidebarPanel(

      fileInput("file1", "Choose CSV File",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv"))

    ),
    mainPanel(
      tableOutput("prediction")
    )
  )
)

server = function(input,output){
  df = reactive({
    req(input$file1)
    read.csv(file = input$file1$datapath)
  })

  #Perform Regression
  output$prediction = renderTable({
    req(df())
    model = lm(mpg ~ disp+hp,data = df())

    sample_df = data.frame(disp = c(100,200),hp = c(100,200))
    prediction = predict(model,sample_data)

    predict_df = data.frame(mpg = prediction)
    output_df = cbind(sample_df,predict_df)
    return(output_df)
  })
}

shinyApp(ui,server)