【问题标题】:Shiny app for prediction with rpart displaying error用于预测的闪亮应用程序,rpart 显示错误
【发布时间】:2016-12-31 04:33:12
【问题描述】:

我尝试构建一个闪亮的应用程序。目的是让用户在输入框中输入 csv 文件。然后用户可以在文本框中输入因变量名称。主面板将显示模型拟合和绘图的摘要详细信息。

我将以下代码编码为 ui.R

library(shiny)
shinyUI(fluidPage(

titlePanel(h1("ANALYSIS",align = "center")),
sidebarLayout(
    sidebarPanel(

        fileInput('file1', 'Select CSV File',
                  accept=c('text/csv', 
                           'text/comma-separated-values,text/plain', 
                           '.csv')),
        textInput("text", label = strong("Dependent Variable"), 
                  value = "Enter Dependent Variable...")
    ),
    mainPanel(br(),

              textOutput('text1'),
              plotOutput('plot')
        )
        )
))

以下作为 server.R :

 library(shiny)
 library(rpart)
 library(rpart.plot)

 shinyServer(function(input, output) {

    output$text1 <- renderText({
    inFile <- input$file1
    if (is.null(inFile))
        return(NULL)
    data <-read.csv(inFile$datapath, header=input$header, sep=input$sep,  quote=input$quote)
    depVar <- input$text
    modrpart <- rpart(depVar ~. ,data=data)
    modrpart
    #depVar


})

    output$plot <- renderPlot({
        inFile <- input$file1
        if (is.null(inFile))
            return(NULL)
        data <-read.csv(inFile$datapath, header=input$header, sep=input$sep,  quote=input$quote)
        depVar <- input$text
        modrpart <- rpart(depVar ~. ,data=data)
        prp(modrpart)
    })


})

当调用 RunApp 时,它显示错误“错误!:无效参数类型”。 模型详细信息和绘图未按预期显示在主面板中。我错过了一些东西。我使用https://drive.google.com/open?id=0B0ByEpXfdtGWMjZHUGpNc3ZZSTg 进行测试。谢谢

【问题讨论】:

  • 你能把.csv上传到某个地方吗
  • 谢谢@PorkChop。有一个输入框。您可以在 sidebarPanel 的输入框中选择任何可用的小 csv 文件。

标签: r shiny


【解决方案1】:

首先,我创建了一个响应式数据集data,其中要求上传数据集req(inFile)。它将防止由于缺少输入而导致错误传播。

data <- reactive({
    inFile <- input$file1
    req(inFile)
    data <- read.csv(inFile$datapath, header=input$header, sep=input$sep,  quote=input$quote)
    data
  })

之后,我为您的模型创建了另一个反应式,要求输入的因变量名称是有效变量。由于input$text 是一个字符串,您必须使用paste0as.formula 函数创建一个公式才能训练模型。您还可以通过 data() 将您的反应数据集传递给模型。 (这两个反应式可以让你不必重复代码)

model <- reactive({
    req(input$text %in% names(data() ))
    depVar <- input$text
    mod <- as.formula(paste0(depVar, " ~ ."))
    modrpart <- rpart(mod, data = data() )
    modrpart
  })

最后,您可以通过model() 将模型传递给render* 函数。

请注意,我将renderText 更改为renderPrint,否则会产生错误。 (renderText 无法处理“猫”功能)


您收到此错误:“错误!:无效参数类型”,因为您缺少小部件并且没有 input$header 和其他一些小部件。我通过添加它们来修复它。 (这个简单的上传界面取自here

read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)


EDITED 对评论中的请求:

library(shiny)
library(rpart)
library(rpart.plot)

server <- shinyServer(function(input, output) {

  data <- reactive({
    inFile <- input$file1
    req(inFile)
    data <- read.csv(inFile$datapath, header=input$header, sep=input$sep,  quote=input$quote)
    data
  })

  # reactive test data
  testdata <- reactive({
    inFile <- input$file2
    req(inFile)
    read.csv(inFile$datapath)
  })

  model <- reactive({
    req(input$text %in% names(data() ))
    depVar <- input$text
    mod <- as.formula(paste0(depVar, " ~ ."))
    modrpart <- rpart(mod, data = data() )
    modrpart
  })

  output$text1 <- renderPrint({ # changed to renderPrint
    model()
  })

  output$plot <- renderPlot({
    prp(model() )
  })

  # print predictions to the console
  observe({ 
    pred <- predict(object = model(), newdata = testdata() )
    print(head(pred, 10))
  })

})


ui <- shinyUI(fluidPage(

  titlePanel(h1("ANALYSIS",align = "center")),
  sidebarLayout(
    sidebarPanel(

      fileInput('file1', 'Upload Train Data',
                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'="'"),
                   '"'),
      br(),
      textInput("text", label = strong("Dependent Variable"), 
                value = "Enter Dependent Variable..."),

      # new fileInput
      br(),
      fileInput('file2', 'Upload Test Data', 
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv'))

    ),
    mainPanel(
      br(),
      textOutput('text1'),
      plotOutput('plot')
    )
  )
))

shinyApp(ui, server)

【讨论】:

  • 非常感谢@UnnamedUser。我对此进行了一些尝试。我添加了另一个文件输入框,用户可以在其中选择测试数据。我创建了另一个反应数据集“testdata”。现在我尝试使用模型拟合对象进行预测。这是给出错误。我想上传测试数据或原始数据来获得预测。由于原始数据已经存储在反应数据集中。我想它会产生错误。你能展示一下这个过程吗?
猜你喜欢
  • 1970-01-01
  • 2016-01-19
  • 1970-01-01
  • 2018-05-24
  • 2017-08-20
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多