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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在R中闪亮的数据帧中强制反应相关的知识,希望对你有一定的参考价值。

无法强制反应到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)  

如何将输入数据放入数据框中。所有帮助表示赞赏!

答案

new_depression是一个反应导体。您需要此反应导体返回的值:

predict(svm_Linear, newdata=new_depression())

并不是

predict(svm_Linear, newdata=new_depression)

但这不会在反应性背景之外发挥作用。所以你必须做以下事情:

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

然后通过执行predict(......)(也在反应环境中)获得predictions()的输出。

以上是关于如何在R中闪亮的数据帧中强制反应的主要内容,如果未能解决你的问题,请参考以下文章

如何在ui中使用使用反应函数作为输入的结果? -r 闪亮

R 用 updateSelectInput 反应闪亮

删除后如何将旧的反应输入存储在R闪亮中

R中闪亮的反应选择输入

r闪亮:按下按钮时eventReactive没有反应

R 闪亮的反应单选按钮