我嘗試構建我的第一個簡單的閃亮應用程序時遇到了令人沮喪的錯誤消息。在運行下面的代碼,我有光澤應用程序加載,但mainPanel中顯示錯誤消息:R閃亮的錯誤:要替換的項目數量不是替換長度的倍數
number of items to replace is not a multiple of replacement length
可惜我不能調試這一點,因爲RStudio並沒有告訴我哪一行導致錯誤。任何幫助將非常感激。
require(shiny)
require(dplyr)
ui = pageWithSidebar(
headerPanel("NFL"),
sidebarPanel(
sliderInput("Margin", "Current margin", min=-50, max=50, value=0, step=1),
numericInput("Spread", "Spread", value=0, width="30%"),
radioButtons("Quarter", "Current period", choices=c("1st", "2nd", "3rd", "4th", "OT"),
selected = "1st", inline = TRUE,width = NULL),
textInput("TimeRemaining", "Time remaining (mm:ss)", value="15:00", width="30%"),
radioButtons("Down", "Down", choices=c("1st", "2nd", "3rd", "4th", "N/A"), inline = TRUE),
numericInput("YTG", "Yards to go", value=10, width="30%"),
numericInput("YFOG", "Yards from own goal", value=50, width="30%"),
radioButtons("Timeouts_Off", "Timeouts: Offense", choices=c("1", "2", "3")),
radioButtons("Timeouts_Def", "Timeouts: Defence", choices=c("1", "2", "3"))
),
mainPanel(
dataTableOutput('testTable')
)
)
server=function(input, output){
out <- reactive({
x=matrix(0, nrow=1, ncol=11)
colnames(x)=c("mar", "timeRemaining", "dwn.1", "dwn.2", "dwn.3", "dwn.4", "ytg","yfog", "closingLine", "timo", "timd")
qtr=switch(input$Quarter, "1st"=1, "2nd"=2,"3rd"=3,"4th"=4, "OT"=5)
mins=as.numeric(substr(input$TimeRemaining,1,2))
secs=as.numeric(substr(input$TimeRemaining,4,5))
timeLeft=100-10/6*((4-qtr)*15+(mins+secs/60))
x[1,1]=input$Margin
x[1,2]=timeLeft
x[1,3]=switch(input$Down, "1st"=1, "2nd"=0,"3rd"=0,"4th"=0)
x[1,4]=switch(input$Down, "1st"=0, "2nd"=1,"3rd"=0,"4th"=0)
x[1,5]=switch(input$Down, "1st"=0, "2nd"=0,"3rd"=1,"4th"=0)
x[1,6]=switch(input$Down, "1st"=0, "2nd"=0,"3rd"=0,"4th"=1)
x[1,7]=input$YTG
x[1,8]=input$YFOG
x[1,9]=input$Spread
x[1,10]=as.numeric(input$Timeouts_Off)
x[1,11]=as.numeric(input$Timeouts_Def)
x=data.frame(x)
xsq=select(x,mar:timd, -dwn.1, -dwn.2, -dwn.3, -dwn.4)^2
colnames(xsq)=paste(colnames(xsq), "sq", sep = "_")
xln=log(select(x,timeRemaining:timd, -closingLine, -dwn.1, -dwn.2, -dwn.3, -dwn.4)+1)
colnames(xln)=paste(colnames(xln), "ln", sep = "_")
x=cbind(x, xsq, xln)
print(x)
rm(xln, xsq)
})
output$testTable <- renderDataTable(out())
}
shinyApp(ui=ui, server=server)
非常感謝! – user3725021