r - Shiny app for prediction with rpart displaying error -


i tried build shiny app. objective let user input csv file in input box. user can input dependent variable name in text box. main panel display summary details of model fit , plot.

i have coded following 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')         )         ) )) 

the following 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)     })   }) 

when runapp called, showing error "error in !: invalid argument type". model details , plot not displayed in main panel intended. missing . have used https://drive.google.com/open?id=0b0byepxfdtgwmjzhugpnc3zzstg test. thanks

first of all, created reactive dataset data in there requirement dataset uploaded req(infile). prevent error propagation due missing inputs.

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

after created reactive model requirement inputed name of dependent variable valid variable. since input$text string, have create formula paste0 , as.formula functions able train model. pass reactive dataset via data() model. (these 2 reactives allow not repeat code unnecessarily)

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

finally, can pass model via model() render* functions.

note changed rendertext renderprint otherwise yield error. (rendertext cannot handle "cat" function)


you got error: "error in !: invalid argument type" because had missing widgets , there no input$header , few others. fixed adding them. (this simple upload interface taken here

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


edited on request in comment:

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 renderprint     model()   })    output$plot <- renderplot({     prp(model() )   })    # print predictions 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) 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -