Model 根据用户输入进行预测-闪亮

Model 根据用户输入进行预测-闪亮,model,shiny,Model,Shiny,我正在尝试创建一个web界面,在这个界面中,我希望根据用户提供的输入从预构建的模型中进行预测 下面是虚拟数据和模型 # 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

我正在尝试创建一个web界面,在这个界面中,我希望根据用户提供的输入从预构建的模型中进行预测

下面是虚拟数据和模型

# 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)
服务器

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)
server=功能(输入、输出){

数据你有重复的输出'Pred',所以shiny不能做任何事情(在主面板和UI中的所有输入之后)。最后删除,然后做你需要做的事情。

你的应用有几个问题。正如@fenix_fx提到的,你有2个
文本输出(outputId='Pred')
冲突。因此,如果创建了一个,它将不会呈现输出

gender
输入以一个
NULL
值开始,因此您必须在reactive中等待,直到选择了一个值,否则它将抛出一个错误,因为不同的行无法生成data.frame。您可以使用
req()
validate(需要(…)
。我放置了一个
req(输入$gender)
在数据框中

另一个问题是,您使用性别标签malefemale构建了
glm
模型,但是您的
复选框GroupInput
提供了malefemale选项。这将导致此错误,无法进行预测

性别因素具有新的层次性

我认为最后一个问题是,
weight
的输入有一个大写字母W,而数据反应式等待一个名为
input$weight
的输入,小写字母W

旁注:您的Ui似乎以一种奇怪的方式排列。通常您会将所有输入/输出放在
侧栏面板
主面板
中,而不是之后

结果代码:

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)
库(闪亮)
ui=fluidPage(
#这是一个输入对象
titlePanel(“承保预测模型”),
侧边栏布局(position=“left”,
侧栏面板(
数值输入(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),
数值输入(输入ID='bmi',标签='bmi',值=18,最小值=NA,最大值=NA,步长=NA,宽度=NULL),
数值输入(inputId='height',label='height',value=150,min=NA,max=NA,step=NA,width=NULL),
数值输入(输入ID='weight',标签='weight',值=25,最小=NA,最大=NA,步长=NA,宽度=NULL),
数值输入(inputId='胆固醇',label='胆固醇',value=25,min=NA,max=NA,step=NA,width=NULL)
),
主面板(文本输出(“Pred”))
)
服务器=功能(输入、输出){

数据感谢@SeGa,代码运行正常。注意到错误和您对UI安排的评论。我仍在摸索中。我将阅读更多。最后一件事-在我从您的代码输出中-输出在概率之前有一个“1”。它看起来像
10.49
尝试使用
renderText
而不是
renderPrint
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)