0
votes

Cannot coerce reactive into data.frame!! I have to take data from the user (numeric) through radio buttons and put it in a data frame for new prediction but it shows error when I try to form the data frame please help!!!

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel( 
  radioButtons(inputId="first", label="a",  choices = list("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4),selected = 1),
                radioButtons(inputId="second", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                radioButtons(inputId="third", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                radioButtons(inputId="fourth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                radioButtons(inputId="fifth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                radioButtons(inputId="sixth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                radioButtons(inputId="seventh", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                radioButtons(inputId="eighth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                radioButtons(inputId="ninth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                radioButtons(inputId="tenth", label="a", c("None" = 0,"Little" = 1,"Some" = 2,"Much" = 3,"Most" = 4)),
                radioButtons(inputId="eleventh", label="a", c("Yes" = 1,"No" = 0)),
                radioButtons(inputId="twelfth", label="a", c("Yes" = 1,"No" = 0)),
                radioButtons(inputId="thirteenth", label="a", c("Yes" = 1,"No" = 0)),
                radioButtons(inputId="fourteenth", label="a", c("Yes" = 1,"No" = 0)),
                radioButtons(inputId="fifteenth", label="a", c("Yes" = 1,"No" = 0)),
                radioButtons(inputId="sixteenth", label="a", c("Yes" = 1,"No" = 0)),
                submitButton("Submit")),
  mainPanel(
                textOutput("ans")
  ))
)

server <- function(input, output){ 
  library(caret)
  depression_model <- read.csv("C:/Users/Sheikh Afaan/Desktop/Project/web/depression_with_nd_p.csv" , header=TRUE , stringsAsFactors = F) 



  str(depression_model)
  head(depression_model )

  set.seed(3233)
  intrain <- createDataPartition(y = depression_model$Depression, p= 0.7, list = FALSE)
  training <- depression_model[intrain,]
  testing <- depression_model[-intrain,]

  dim(training)
  dim(testing)

  anyNA(depression_model)
  summary(depression_model)

  training[["Depression"]] = factor(training[["Depression"]])

  trctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3)
  set.seed(3233)

  svm_Linear <- train(Depression ~., data = training, method = "svmLinear",
                      trControl=trctrl,
                      preProcess = c("center", "scale"),
                      tuneLength = 10)

  svm_Linear
  summary(svm_Linear)

  test_pred <- predict(svm_Linear, newdata = testing)
  test_pred

  tab <- table(Predicted = test_pred, Actual = testing$Depression )
  tab
  1 - sum(diag(tab))/sum(tab)

  #It shows error here please help
  new_depression <- reactive({ data.frame("ASadness"=input$first,
                               "Loconcen"=input$second,
                               "Asleep"=input$third,
                               "Aappet"=input$fourth,
                               "Loenergy"=input$fifth,
                               "Foguilt"=input$sixth,
                               "Asbehav"=input$seventh,
                               "Sthough"=input$eighth,
                               "Ppains"=input$ninth,
                               "Eactivity"=input$tenth,
                               "Wloss"=input$eleventh,
                               "Ssupport"=input$twelfth,
                               "Etdsthin"=input$thirteenth,
                               "Dmaking"=input$fourteenth,
                               "Fhopilln"=input$fifteenth,
                               "Addiction"=input$sixteenth)
  })

predict(svm_Linear, newdata=new_depression)  

output$ans <- renderText({})

}

shinyApp(ui, server)  

How can I put input data into a data frame. All help is appreciated!!!

1

1 Answers

0
votes

new_depression is a reactive conductor. You need the value returned by this reactive conductor:

predict(svm_Linear, newdata=new_depression())

and not

predict(svm_Linear, newdata=new_depression)

But that will not work outside a reactive context. So you have to do something like:

predictions <- reactive({
  predict(svm_Linear, newdata=new_depression())
})

And then you get the output of predict(......) by doing predictions() (also in a reactive context).