2016-12-15 38 views
-1

最初のシンプルな光沢のあるアプリケーションを作成しようとしましたが、イライラするエラーメッセージが表示されました。 RStudioは、エラーの原因となっている行を私に教えてくれないので、 R光沢のあるエラー:交換するアイテムの数が交換の長さの倍数ではありません

number of items to replace is not a multiple of replacement length

は、残念ながら、私はこれをデバッグすることはできません:以下のコードは、私の光沢のあるアプリケーションの負荷が、mainPanelを実行する際にエラーメッセージが表示されます。どんな助けでも大歓迎です。

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

サーバーに必要な複数の変更、UIは大丈夫です、以下の作業をする必要があります:

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の入力ミスが発生しました。 IはR-コンソール上のxの値を印刷する際に助けてくれたコードの各行の間にprint(x)を挿入することによってこれを推定

x[1,9]=input$spread 

x[1,9]=input$Spread 

に変更。変数Spreadの値は、光沢のあるアプリを変更しても、xの値は変更されませんでした。したがって、それ自体は、誤字であるSpreadに何か問題があることを示唆していました。

これが役に立ちます。

EDIT問題は、あなたがout機能から何かを返されていないということですout() function

print(x) 
rm(xln, xsq) 
x 

の終わりにfollwoingます。

+0

ありがとうございました。入力ミスを修正しました。私はまだ出力テーブルにxの行列を得ることができません - なぜあなたは知っていますか? – user3725021

+0

関数 'select'を使用していますが、これについてはわかりません。それがエラーの原因です。どこからこの機能が得られますか? –

+0

これはdplyrパッケージからのものです(コードにrequire()行が追加されています)エラーが発生していないはずです – user3725021

関連する問題