0

I am trying to build a Shiny app that displays the output of a random forest in different tabs. Here, I only create a tab for the partial dependence plot to show the error I am receiving. Below is code for the global, server and ui files.

global.R

library(shiny)
setwd("C:/Users/rcaldwe4/Documents/Monthly Topic/Stackoverflow")

df <- data.frame(x1=sample(1:100,10000,replace=T),
                 x2=sample(1:100,10000,replace=T),
                 x3=sample(1:100,10000,replace=T),
                 y=as.factor(sample(0:1,10000,replace=T)))


train_x <- df[1:5000,-4]
train_y <- df[1:5000,4]
train1 <- data.frame(train_x,y=train_y)

test_x <- df[5001:10000,-4]
test_y <- df[5001:10000,4]

server.R

library(shiny)
library(randomForest)
library(AUC)
library(rpart)
library(lift)

#Define server logic required to output Random Forest
shinyServer(function(input,output){

  rfInput <- reactive({
    randomForest(x=train_x,y=train_y,importance=T,ntree=input$trees,keep.forest = T)
  })

  output$partDep <- renderPlot({
    partialPlot(x=rfInput(), pred.data=train1, x.var=input$select_var, which.class=1)
  })

})

ui.R

library(shiny)

shinyUI(
  fluidPage(
    titlePanel("Random Forest"),
    sidebarLayout(
      sidebarPanel(
        sliderInput("trees",
                    "Number of trees:",
                    min=0,
                    max=1000,
                    value=100,
                    step=50),
        selectInput("select_var", 
                    label = h4("Select variable for partial dependence plot"),
                    choices = names(train1)[1:3], 
                    selected = names(train1)[1])
      ),
      mainPanel(
        tabsetPanel(
          tabPanel("Dependence",
                   plotOutput("partDep"))
        )
      )
    )
  ))

The slider input for the number of trees to build is working fine. But, when I try to have the user select the variable for the partial dependence plot, I get this error:

Warning: Error in eval: object 'input' not found

I am not clear as to what the issue is with 'input'. I am wondering if it is an issue with using 'input' with the partialPlot function. Any suggestions would be great.

1 Answer 1

1

This is a problem with partialPlot. This is addressed in another question here. The only explanation is that some plot functions use some non-standard evaluation.

If you change your partDep section in server.R to the following it works:

output$partDep <- renderPlot({
   do.call("partialPlot", 
           list(x=rfInput(), pred.data=train1, x.var = input$select_var, which.class = 1))
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.