Error in active reactive context - How to fix?

ساخت وبلاگ

آخرین مطالب

امکانات وب

Hey I am trying to build a shiny app for the purpose of calculating per cent chance of defaulting and I thought I fixed all my issues until I hit

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

but whenever I try to build something reactive I get

Error in RET@get_where(newdata = newdata, mincriterion = mincriterion) : 
  object 'loanfilev3' not found

I've looked over stackoverflow and tutorials and none seem to really help

Here is my UI and Server code for the first error, if someone could please highlight my issue that would be greatly appreciated.

UI:

library(shiny)

shinyUI(fluidPage(


  titlePanel("Tabsets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select the random distribution type ----
      numericInput("loan_amnt",
                   "Loan Amount:",
                   value = 5000,
                   min = 0,
                   max = NA),

      numericInput("int_rate",
                   "Interest Rate:",
                   value = 10.5,
                   min = 0,
                   max = NA),

      selectInput("term",
                  "Loan Term:",
                  c("36 months" = " 36 months", 
                    "60 months" = " 60 months")),

      numericInput("installment",
                   "Installment:",
                   value = 100,
                   min = 0,
                   max = NA),
      textInput("grade", "Grade:", "B"),

      textInput("emp_length", "Employment Length:", "5 years"),

      numericInput("annual_inc",
                   "Annual Income:",
                   value = 40000,
                   min = 0,
                   max = NA),

      numericInput("dti",
                   "Debt to Income Ratio:",
                   value = 5.4,
                   min = NA,
                   max = NA),

      textInput("sub_grade", "SubGrade:", "B2"),

      textInput("verification_status", "Verification Status:", "Verified"),

      textInput("home_ownership", "Home Ownership:", "RENT"),

      radioButtons("pymnt_plan", "Payment Plan:",
                   c("Yes" = "y",
                     "No" = "n"))
    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Tabset w/ plot, summary, and table ----
      tabsetPanel(type = "tabs",
                  tabPanel("Decision Tree", verbatimTextOutput("ct")),
                  tabPanel("Generlized Linear Model", verbatimTextOutput("dl")),
                  tabPanel("K-Nearest Neighbour", verbatimTextOutput("kn"))
      )
    )
  )
)
)

Server:

library(shiny)
library(pscl)
library(ROCR)
library(plyr)
library(dplyr)
library(ggplot2)
library(pROC)
library(caret)
library(e1071)
library(RMySQL)
library(reshape2)

USER <- 'inft216'
PASSWORD <- 'rosemary'
HOST <- 'bruce3.dc.bond.edu.au'
DBNAME <- 'inft216'

db <- dbConnect(MySQL(), user = USER, password = PASSWORD, host = HOST, dbname = DBNAME)
loanfile <- dbGetQuery(db, statement = "select * from lendingClub;")
dbDisconnect(db)

library(party)

colnames(loanfile) = tolower(colnames(loanfile))

bad_indicators = c("Charged Off",
                   "Default",
                   "Does not meet the credit policy. Status:Charged Off",
                   "Default Receiver", 
                   "Late (16-30 days)",
                   "Late (31-120 days)")

loanfile$default = ifelse(loanfile$loan_status %in% bad_indicators, 1,
                          ifelse(loanfile$loan_status=="", NA, 0))
loanfile$loan_status = as.factor(loanfile$default)
loanfilev2 = dplyr::select(.data = loanfile,loan_status,loan_amnt,int_rate,term,installment,grade,emp_length,annual_inc,dti,sub_grade,verification_status,home_ownership,pymnt_plan) 
loanfilev2$grade = as.factor(loanfilev2$grade)
loanfilev2$sub_grade <- as.factor(loanfilev2$sub_grade)
loanfilev2$term <- as.factor(loanfilev2$term)
loanfilev2$emp_length <- as.factor(loanfilev2$emp_length)
loanfilev2$verification_status <- as.factor(loanfilev2$verification_status)
loanfilev2$home_ownership <- as.factor(loanfilev2$home_ownership)
loanfilev2$pymnt_plan <- as.factor(loanfilev2$pymnt_plan)
loanfilev2$loan_status <- as.factor(loanfilev2$loan_status)
loanfilev2$grade <- as.numeric(loanfilev2$grade)
loanfilev2$sub_grade <- as.numeric(loanfilev2$sub_grade)
loanfilev2$term <- as.numeric(loanfilev2$term)
loanfilev2$emp_length <- as.numeric(loanfilev2$emp_length)
loanfilev2$verification_status <- as.numeric(loanfilev2$verification_status)
loanfilev2 <- loanfilev2[complete.cases(loanfilev2),]
set.seed(69)
train_index <- sample(seq_len(nrow(loanfilev2)), size = 5000)
TrainData<- loanfilev2[train_index, ]

ct = ctree(loan_status ~ ., data = TrainData)
dl <- glm(formula = loan_status ~ .,data = loanfilev2, family = binomial)
kn <- train(form = loan_status ~.,data = TrainData, method = 'knn')

shinyServer(function(input, output) {


  loan_status <- c(0)
  loan_amnt <- input$loan_amnt
  int_rate <- input$int_rate
  term <- input$term
  installment <- input$installment
  grade <- input$grade
  emp_length <- input$emp_length
  annual_inc <- input$annual_inc
  dti <- input$dti
  sub_grade <- input$sub_grade
  verification_status <- input$verification_status
  home_ownership <- input$home_ownership
  pymnt_plan <- input$pymnt_plan
  temp2 <- cbind(loan_status, loan_amnt, int_rate, term, installment, grade, emp_length, annual_inc, dti, sub_grade, verification_status, home_ownership, pymnt_plan)
  loanfilev3 = dplyr::select(.data = loanfile,loan_status,loan_amnt,int_rate,term,installment,grade,emp_length,annual_inc,dti,sub_grade,verification_status,home_ownership,pymnt_plan) 
  loanfilev3 = rbind(loanfilev3, temp2, deparse.level = 0)
  loanfilev3$grade = as.factor(loanfilev3$grade)
  loanfilev3$sub_grade <- as.factor(loanfilev3$sub_grade)
  loanfilev3$term <- as.factor(loanfilev3$term)
  loanfilev3$emp_length <- as.factor(loanfilev3$emp_length)
  loanfilev3$verification_status <- as.factor(loanfilev3$verification_status)
  loanfilev3$home_ownership <- as.factor(loanfilev3$home_ownership)
  loanfilev3$pymnt_plan <- as.factor(loanfilev3$pymnt_plan)
  loanfilev3$loan_status <- as.factor(loanfilev3$loan_status)
  loanfilev3$grade <- as.numeric(loanfilev3$grade)
  loanfilev3$sub_grade <- as.numeric(loanfilev3$sub_grade)
  loanfilev3$term <- as.numeric(loanfilev2$term)
  loanfilev3$emp_length <- as.numeric(loanfilev3$emp_length)
  loanfilev3$verification_status <- as.numeric(loanfilev3$verification_status)
  loanfilev3 <- loanfilev3[complete.cases(loanfilev3),]
    prediction1 = c(predict(object = ct, newdata = loanfilev3[886508], type = "prob"))
    output$ct <- renderPrint({
    as.data.frame(prediction1)[2,]*100

  })
    loan_status <- c(0)
    loan_amnt <- input$loan_amnt
    int_rate <- input$int_rate
    term <- input$term
    installment <- input$installment
    grade <- input$grade
    emp_length <- input$emp_length
    annual_inc <- input$annual_inc
    dti <- input$dti
    sub_grade <- input$sub_grade
    verification_status <- input$verification_status
    home_ownership <- input$home_ownership
    pymnt_plan <- input$pymnt_plan
    temp2 <- cbind(loan_status, loan_amnt, int_rate, term, installment, grade, emp_length, annual_inc, dti, sub_grade, verification_status, home_ownership, pymnt_plan)
    loanfilev3 = dplyr::select(.data = loanfile,loan_status,loan_amnt,int_rate,term,installment,grade,emp_length,annual_inc,dti,sub_grade,verification_status,home_ownership,pymnt_plan) 
    loanfilev3 = rbind(loanfilev3, temp2, deparse.level = 0)
    loanfilev3$grade = as.factor(loanfilev3$grade)
    loanfilev3$sub_grade <- as.factor(loanfilev3$sub_grade)
    loanfilev3$term <- as.factor(loanfilev3$term)
    loanfilev3$emp_length <- as.factor(loanfilev3$emp_length)
    loanfilev3$verification_status <- as.factor(loanfilev3$verification_status)
    loanfilev3$home_ownership <- as.factor(loanfilev3$home_ownership)
    loanfilev3$pymnt_plan <- as.factor(loanfilev3$pymnt_plan)
    loanfilev3$loan_status <- as.factor(loanfilev3$loan_status)
    loanfilev3$grade <- as.numeric(loanfilev3$grade)
    loanfilev3$sub_grade <- as.numeric(loanfilev3$sub_grade)
    loanfilev3$term <- as.numeric(loanfilev2$term)
    loanfilev3$emp_length <- as.numeric(loanfilev3$emp_length)
    loanfilev3$verification_status <- as.numeric(loanfilev3$verification_status)
    loanfilev3 <- loanfilev3[complete.cases(loanfilev3),]
    prediction2= c(predict(dl, newdata= loanfilev3[886508,], type = "response"))
    output$dl <- renderPrint({
      as.data.frame(prediction2)[2,]*100

    })

    loan_status <- c(0)
    loan_amnt <- input$loan_amnt
    int_rate <- input$int_rate
    term <- input$term
    installment <- input$installment
    grade <- input$grade
    emp_length <- input$emp_length
    annual_inc <- input$annual_inc
    dti <- input$dti
    sub_grade <- input$sub_grade
    verification_status <- input$verification_status
    home_ownership <- input$home_ownership
    pymnt_plan <- input$pymnt_plan
    temp2 <- cbind(loan_status, loan_amnt, int_rate, term, installment, grade, emp_length, annual_inc, dti, sub_grade, verification_status, home_ownership, pymnt_plan)
    loanfilev3 = dplyr::select(.data = loanfile,loan_status,loan_amnt,int_rate,term,installment,grade,emp_length,annual_inc,dti,sub_grade,verification_status,home_ownership,pymnt_plan) 
    loanfilev3 = rbind(loanfilev3, temp2, deparse.level = 0)
    loanfilev3$grade = as.factor(loanfilev3$grade)
    loanfilev3$sub_grade <- as.factor(loanfilev3$sub_grade)
    loanfilev3$term <- as.factor(loanfilev3$term)
    loanfilev3$emp_length <- as.factor(loanfilev3$emp_length)
    loanfilev3$verification_status <- as.factor(loanfilev3$verification_status)
    loanfilev3$home_ownership <- as.factor(loanfilev3$home_ownership)
    loanfilev3$pymnt_plan <- as.factor(loanfilev3$pymnt_plan)
    loanfilev3$loan_status <- as.factor(loanfilev3$loan_status)
    loanfilev3$grade <- as.numeric(loanfilev3$grade)
    loanfilev3$sub_grade <- as.numeric(loanfilev3$sub_grade)
    loanfilev3$term <- as.numeric(loanfilev2$term)
    loanfilev3$emp_length <- as.numeric(loanfilev3$emp_length)
    loanfilev3$verification_status <- as.numeric(loanfilev3$verification_status)
    loanfilev3 <- loanfilev3[complete.cases(loanfilev3),]
    prediction3 = c(predict(kn, newdata=loanfilev3[886508,], type="prob"))
    output$kn <- renderPrint({
      as.data.frame(prediction3)[2,]*100

    })
})
...
نویسنده : استخدام کار بازدید : 2 تاريخ : چهارشنبه 14 آذر 1397 ساعت: 19:05

close
تبلیغات در اینترنت