如何在R中的闪亮中强制数据框中的反应

时间:2019-02-22 12:59:02

标签: r shiny

无法将反应性强制变为data.frame !!我必须通过单选按钮从用户(数字)中获取数据,并将其放入数据框中以进行新的预测,但是当我尝试形成数据框时会显示错误,请帮忙!!!

图书馆(闪亮)

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)  

如何将输入数据放入数据框。感谢所有帮助!

1 个答案:

答案 0 :(得分:0)

new_depression是电抗性导体。您需要此电抗导体返回的值:

predict(svm_Linear, newdata=new_depression())

不是

predict(svm_Linear, newdata=new_depression)

但这在反应性上下文之外是行不通的。因此,您必须执行以下操作:

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

然后通过执行predict(......)(同样在响应上下文中)获得predictions()的输出。