0
votes

I have a shiny app for which I want users to be able to upload their own data and run a gbm model. The uploaded database will be a dataframe of all the predictors plus the response variable. This will require the user to input the response variable as a reactive input, or select it from a list of column names. Here is what I have tried so far:

library(shiny)
library(gbm)


ui <- fluidPage(
  fluidRow(
    column(3,textInput("response","Enter the response variable",value="Petal.Width")),
    column(9,verbatimTextOutput("summary"))
  )
)
server <- function(input, output, session) {


  output$summary <- renderPrint({
 model<-gbm(input$response~., data=iris,
               distribution="gaussian",
               bag.fraction=.5,
               n.trees=1000,
               interaction.depth=4,
               shrinkage=0.01,
               n.minobsinnode=10,
               verbose=FALSE)
    summary(model)

  })



}


shinyApp(ui=ui,server=server)

But I get a variable lengths differ error:

Warning: Error in model.frame.default: variable lengths differ (found for 'Sepal.Length')
Stack trace (innermost first):
    90: model.frame.default
    89: model.frame
    88: eval
    87: eval
    86: gbm
    85: renderPrint [D:/Dropbox/PhD/Nitrate Mapping Project/USGS Project/SOPs and Test Data/1SE SOP/gbm_reac_response.R#15]
    84: func
    83: eval
    82: eval
    81: withVisible
    80: evalVis
    79: utils::capture.output
    78: paste
    77: origRenderFunc
    76: output$summary
     1: runApp
1

1 Answers

1
votes

You could use a reactive() object to convert your user input into a formula object for passing to gbm(). Something like this would work....

library(shiny)
library(gbm)


ui <- fluidPage(
    fluidRow(
        column(3,textInput("response","Enter the response variable",
                           value="Petal.Width")),
        column(9,verbatimTextOutput("summary"))
    )
)
server <- function(input, output, session) {

    f <- reactive({ as.formula(paste(input$response, "~ .")) })

    output$summary <- renderPrint({
        model<-gbm(f(), data=iris,
                   distribution="gaussian",
                   bag.fraction=.5,
                   n.trees=1000,
                   interaction.depth=4,
                   shrinkage=0.01,
                   n.minobsinnode=10,
                   verbose=FALSE)
        summary(model)

    })

}


shinyApp(ui=ui,server=server)