0

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 Answer 1

0

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

3
  • Thankyou very much Sir it worked, now but how do i output the answer , and using this code: output$ans <- renderText({reactive({predictions()})}) it shows error: Error in cat: argument 1 (type 'closure') cannot be handled by 'cat' Feb 23, 2019 at 4:34
  • @student output$ans <- renderText({predictions()}). renderXXX is a reactive context. Feb 23, 2019 at 7:30
  • output$ans <- renderText({predictions()}) not working!!! I think problem is here : predictions <- reactive({ predict(svm_Linear, newdata=new_depression()) }) Its showing variables in new_depression() were specified with different types from the fit Feb 23, 2019 at 14:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.