我使用Shiny和R动态显示基于用户输入Shiny的输入和基于现有数据的模型的预测值。但是,在预测的输出部分,我收到以下错误: “无效的第一个论点。”任何帮助将不胜感激,因为这是我第一次真正涉足R和Shiny。以下是我的R代码:
#shiny ui build
library(shiny)
shinyUI(fluidPage(
titlePanel(
tags$b(h1("Predict"))
),
sidebarLayout(
sidebarPanel(
tags$style("body{background-image: url('Picture.jfif'); background-size: cover; background-repeat: no-repeat"),
tags$style(".well{background-color:#D0D0D0}"),
submitButton(text="Compute!"),
h2("Answer"),
textOutput("Answer"),
"Units",
br(),
h6("Powered by:"),
img(src="RStudio-Ball.png",height=35,width=35,alight="Left")
),
mainPanel(
tags$style(".well{background-color:#D0D0D0}"),
h2("Enter information"),
selectInput("Var.F","Select the input Var F",choices=levels(ExistingData$Var.F)),
dateInput("Var.D","Enter input Var D",value=NULL),
selectInput("Var.P","Select the input Var P",choices=levels(ExistingData$Var.P)),
selectInput("Var.O","Select the input Var O",choices=levels(ExistingData$Var.O)),
selectInput("Var.G","Select the input Var G",choices=c("A","B")),
selectInput("Var.C","List Var C",choices=c("CA","DI","S"),multiple=TRUE),
numericInput("Var.H","Enter H Value",value=5),
numericInput("Var.W","Enter W Value",value=10),
br()
)
)
)
)
#shiny server
library(shiny)
#############
library(httr)
set_config(config(ssl_verifypeer = 0L))
library(DomoR)
set.seed(1000)
DomoR::init('x123','x12345')
#Get new data
ExistingData <- fetch('x') #This is a call to Domo to get the data set "ExistingData"
#Set Factors
ExistingData$Var.F <- as.factor(ExistingData$Var.F)
ExistingData$Var.P <- as.factor(ExistingData$Var.P)
ExistingData$Var.O <- as.factor(ExistingData$Var.O)
ExistingData$Var.G <- as.factor(ExistingData$Var.G)
ExistingData$Var.DI <- as.factor(ExistingData$Var.DI)
ExistingData$Var.S <- as.factor(ExistingData$Var.S)
ExistingData$Var.CA <- as.factor(ExistingData$Var.CA)
#Compute age
ExistingData$Var.T <- (ExistingData$T2 - ExistingData$T1)
ExistingData$Var.T <- as.numeric(ExistingData$Var.T)
#Random Forest
library(randomForest)
RF.x <- as.data.frame(ExistingData[,c("Var.A","Var.F","Var.P","Var.O","Var.G","Var.DI","Var.W","Var.H","Var.B","Var.S","Var.CA")])
set.seed(1000)
RF.1 <- randomForest(y=ExistingData$Answer,x=RF.x,importance=TRUE,ntree=5)
#Shiny specific language
shinyServer(function(input,output){
Var.T <- reactive({Sys.Date()-input$D})
Var.F <- reactive({input$Var.F})
Var.P <- reactive({input$Var.P})
Var.O <- reactive({input$Var.O})
Var.G <- reactive(as.factor(ifelse(input$Var.G=="A",1,0)))
Var.DI <- reactive({as.factor(max(ifelse(grepl("DI",input$Var.C),1,0)))})
Var.W <- reactive({input$Var.W})
Var.H <- reactive({input$Var.H})
Var.B <- reactive({(Var.H - Var.W)})
Var.S <- reactive({as.factor(max(ifelse(grepl("S",input$Var.C),1,0)))})
Var.CA <- reactive({as.factor(max(ifelse(grepl("CA",input$Var.CA),1,0)))})
i.df <- reactive({
data.frame(Var.A=as.numeric(Var.A()),
Var.F=as.factor(Var.F()),
Var.P=as.factor(Var.P()),
Var.O=as.factor(Var.O()),
Var.G=as.factor(Var.G()),
Var.DI=as.factor(Var.DI()),
Var.W=as.numeric(Var.W()),
Var.H=as.numeric(Var.H()),
Var.B=as.numeric(Var.B()),
Var.S=as.factor(Var.S()),
Var.CA=as.factor(Var.CA())
)
})
output$Answer <- reactive({predict(RF.1,i.df())[1]})
})
编辑:我在输出$ Answer中修复了i.df,这样它就是i.df()。我现在收到的错误是“新数据中预测变量的类型与训练数据的类型不匹配。”