Error when trying to execute something reactive on shiny R

+1 vote

I'm trying to build a shiny app. I get this error:

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.)

when I try to execute something reactive I get another error:

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

code:

ui.R

 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.R
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

  })

})
Dec 6, 2018 in Data Analytics by Ali
• 11,360 points
2,444 views

1 answer to this question.

0 votes

There seem to be some issues in your server.R, try using this:

shinyServer(function(input, output) {

  prediction <- eventReactive(input$predict, {
    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),]
    predict(object = ct, newdata = loanfilev3[886508], type = "prob"))
  })

  output$ct <- renderPrint({
    as.data.frame(prediction())[2,]*100
  })
})
answered Dec 6, 2018 by Maverick
• 10,840 points

Related Questions In Data Analytics

+5 votes
0 answers
0 votes
1 answer

"Error in if" while trying to execute simple code in R

This caused non-logical data or missing values passed ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
622 views
0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer

Error saying "could not find function "shinyUI"" in shiny R

Its a small spelling mistake that you've ...READ MORE

answered Nov 28, 2018 in Data Analytics by Maverick
• 10,840 points
2,626 views
+1 vote
1 answer

Error saying "could not find function dashboardPage" in shiny R

Include this line in the code: Library(shinydashboard) READ MORE

answered Nov 29, 2018 in Data Analytics by Maverick
• 10,840 points
3,657 views
+1 vote
1 answer

"Error in eval(ei, envir) : object 'RDX2' not found" when trying to source the code in R

This is a very common issue that ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,840 points
5,109 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP