2016-12-15 62 views
-1

我嘗試構建我的第一個簡單的閃亮應用程序時遇到了令人沮喪的錯誤消息。在運行下面的代碼,我有光澤應用程序加載,但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) 

回答

1

在服務器所需的多種變化,用戶界面是好的,下面應該工作:

server=function(input, output){ 

    values <- reactiveValues() 
    values$df <- NULL 

    newEntry <- observe({ # use observe pattern 

    x=as.data.frame(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 # wrong case earlier 
    x[1,10]=as.numeric(input$Timeouts_Off) # need to be converted to numeric 
    x[1,11]=as.numeric(input$Timeouts_Def) # need to be converted to numeric 

    #xsq=select(x,mar:timd, -dwn.1, -dwn.2, -dwn.3, -dwn.4)^2 # deplyr::select was not working 
    xsq <- x[-3:-6] 
    colnames(xsq)=paste(colnames(xsq), "sq", sep = "_") 
    #xln=log(select(x,timeRemaining:timd, -closingLine, -dwn.1, -dwn.2, -dwn.3, -dwn.4)) # need to handle log properly 
    xln=x[-c(3:6,9)] 
    indices= which(xln!=0) 
    if (length(indices)>0) { 
     xln[indices]=log(xln[indices]) 
    } 
    colnames(xln)=paste(colnames(xln), "ln", sep = "_") 
    x=cbind(x, xsq, xln) 
    rm(xln, xsq) 
    isolate(values$df <- x) 
    }) 

    output$testTable <- renderDataTable({values$df}) 
} 
+0

非常感謝! – user3725021

0

您對變量Spread進行了拼寫錯誤。改變

x[1,9]=input$spread 

x[1,9]=input$Spread 

我通過在每一行代碼,這有助於我打印R-控制檯上x的值之間插入print(x)推導出這一點。即使當我在閃亮的應用程序中更改時,變量Spread的值也不會更改x的值。因此,這本身就表明Spread存在一些問題,這是錯字。

希望這會有所幫助。

編輯添加在out() function

print(x) 
rm(xln, xsq) 
x 

問題到底follwoing是,你沒有返回從out函數的任何信息。

+0

謝謝。修復了錯字。我仍然無法將x矩陣放入輸出表中 - 你知道爲什麼不能嗎? – user3725021

+0

您正在使用函數'select',我不知道。這是錯誤的根源。你從哪裏得到這個功能? –

+0

它來自dplyr包(我現在在代碼中添加了一個require()行,它不應該再導致錯誤 – user3725021

相關問題