Reputation: 544
I am trying to create a web interface where i want to make predictions from a pre-built model on the inputs provided by the user.
Below is the dummy data and model
# creation of dummy data and model
age=round(runif(100,15,100))
bmi=round(runif(100,15,45))
cholesterol=round(runif(100,100,200))
gender=sample(c('male','female'), 100, replace=TRUE, prob=c(0.45,0.55))
height=round(runif(100,140,200))
weight=round(runif(100,140,200))
outcome=sample(c('accepted','reject'),100,replace=T,prob=c(0.30,0.70))
df=data.frame(age,bmi,cholesterol,gender,height,weight,outcome)
model <- glm(outcome ~.,family=binomial(link='logit'),data=df)
Now i'll have tried creating a shiny app referencing through multiple links and similar issues people have faced. I am beginner and still haven't been able to resolve the issue. While there is no error in running the app however, The output i.e. the probability is not printed anywhere on the web page
UI Code
library(shiny)
ui = fluidPage(
# this is an input object
titlePanel("Underwriting Prediction Model"),
sidebarLayout(position = "right",
sidebarPanel(),
mainPanel(textOutput("Pred"))),
numericInput(inputId='age', label='Age', value = 18,min = NA, max = NA, step = NA,width = NULL),
checkboxGroupInput(inputId='gender', label='Gender', c('Male','Female'), selected = NULL, inline = FALSE,width = NULL),
numericInput(inputId='bmi', label='bmi', value = 18,min = NA, max = NA, step = NA,width = NULL),
numericInput(inputId='height', label='Height', value = 150,min = NA, max = NA, step = NA,width = NULL),
numericInput(inputId='Weight', label='Weight', value = 25, min = NA, max = NA, step = NA,width = NULL),
numericInput(inputId='cholesterol', label='Cholesterol', value = 25, min = NA, max = NA, step = NA,width = NULL),
textOutput(outputId = 'Pred')
)
Server
server = function (input,output) {
data<-reactive({
df_final=data.frame(age=input$age,gender=input$gender,bmi=input$bmi,height=input$height,weight=input$weight,cholesterol=input$cholesterol)})
pred=reactive({predict(model,data())})
output$Pred <- renderPrint(pred())
}
shinyApp(ui=ui,server=server)
When i provide the input values, i get no printed output for my predictions. I am calling the variable in render print and the main panel yet don't see any output. What is the issue here?
Upvotes: 1
Views: 1960
Reputation: 9809
There are several problems with your App. As @fenix_fx mentioned, you have 2 textOutput(outputId = 'Pred')
which collide. So it would not render the output if one were created.
The gender
input starts with a NULL
value, so you have to wait in your reactive until a value was selected, otherwise it will throw an error, that the data.frame cannot be build because of different rows. You can do that with req()
or validate(need(...))
. I put a req(input$gender)
in the data reactive.
Another problem was that you build the glm
model with the gender labels male and female, but your checkboxGroupInput
gives the options Male and Female. This will give you this error and the prediction cannot be made.
factor gender has new level Male
And last problem I think was that your input for weight
has a capital W, while the data reactive waits for an input called input$weight
with lower case w.
SIDENOTE: Your Ui seems to be arranged in a weird way. Normally you would put all inputs/outputs inside the sidebarPanel
or mainPanel
and not afterwards.
Resulting Code:
library(shiny)
ui = fluidPage(
# this is an input object
titlePanel("Underwriting Prediction Model"),
sidebarLayout(position = "left",
sidebarPanel(
numericInput(inputId='age', label='Age', value = 18,min = NA, max = NA, step = NA,width = NULL),
checkboxGroupInput(inputId='gender', label='Gender', c('male','female'), selected = NULL, inline = FALSE,width = NULL),
numericInput(inputId='bmi', label='bmi', value = 18,min = NA, max = NA, step = NA,width = NULL),
numericInput(inputId='height', label='Height', value = 150,min = NA, max = NA, step = NA,width = NULL),
numericInput(inputId='weight', label='Weight', value = 25, min = NA, max = NA, step = NA,width = NULL),
numericInput(inputId='cholesterol', label='Cholesterol', value = 25, min = NA, max = NA, step = NA,width = NULL)
),
mainPanel(textOutput("Pred")))
)
server = function (input,output) {
data <- reactive({
req(input$gender)
data.frame(age=input$age,
gender=input$gender,
bmi=input$bmi,
height=input$height,
weight=input$weight,
cholesterol=input$cholesterol)
})
pred <- reactive({
predict(model,data())
})
output$Pred <- renderPrint(pred())
}
shinyApp(ui=ui,server=server)
Upvotes: 1
Reputation: 394
You have duplicate output 'Pred', so shiny can't do anything (in mainPanel and after all inputs in UI). Delete last and do what you need next.
Upvotes: 0