Reputation: 198
I have coded a shiny app. It have user input dataset and user input dependent variable .It predicts the dependent variable. Dependent variable is stored in input$text
In UI.R :
textOutput('contents2')
In server.R I have mentioned a conditional statement where if the dependent variable is factor , it will predict class levels, otherwise continuous values:
output$contents2 <- renderText({
if(class(input$text)=="factor"){
predict(modelc(), newdata=testdata(),type="class")}
if(class(input$text)=="numeric"){
predict(model(), newdata=testdata())
}
})
But its not displaying predicted values. I was wondering what might be missing. Thanks
Upvotes: 0
Views: 200
Reputation: 5471
Nothing gets printed because no value is returned by renderText
, you can easily fix it by wrapping predict(...)
into return
function.
However, there is another bug. Since input$text
is a character string its class is character
and your logical comparison doesn't do what you want. You can fix it, first by subsetting testdata()
with [[
operator which gives you a vector and then checking its class.
You also have to make sure that the name of inputed variable is indeed a valid variable - as usual with req
function (or validate
and need
)
Full example:
output$contents2 <- renderText({
req(input$text %in% names(testdata() ))
test <- class(testdata()[[input$text]])
if (test == "factor") {
return(predict(modelc(), newdata = testdata(), type = "class") )
}
if (test == "numeric") {
return(predict(model(), newdata = testdata()) )
}
})
Upvotes: 1