Как заставить реактив в dataframe в блестящем в R - PullRequest
0 голосов
/ 22 февраля 2019

Невозможно принудительно реагировать на 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 голосов
/ 22 февраля 2019

new_depression - реактивный проводник.Вам нужно значение, возвращаемое этим реактивным проводником:

predict(svm_Linear, newdata=new_depression())

, а не

predict(svm_Linear, newdata=new_depression)

Но это не будет работать вне реактивного контекста.Поэтому вы должны сделать что-то вроде:

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

И затем вы получите вывод predict(......), выполнив predictions() (также в реактивном контексте).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...