0

I'm writing some code to plot a control chart in shiny(I don't have to use the qichart library) and I'm struggling to find a solution about the error "Replacement has length zero", I already know this error indicates NULL or a length 0 vector. I tried step by step to solve it in the console and the error doesn't appear and I think it's strange. I'm using for loops because I have no practice with the "apply & co" function. I'm pasting part of the code for better comprehension. I've found out that the problem is in the for a loop when I build MR, how can I solve it? Thanks in advance.

                day <- input$date
                type <- input$type
                region <- input$region
                pnc <- input$range


                d2 <- 1.128
                request <- filter(region_dataset, region_dataset$data>=day & region_dataset$denominazione_regione==region)
                request <- request[,c(1,2,23,29,which(colnames(request)==(gsub(" ","_",input$type))))]
                pbar <- mean(request[,5])
                sigmapi <- as.data.frame(matrix(nrow = nrow(request),ncol = 1))
                zetai <- as.data.frame(matrix(nrow = nrow(request),ncol = 1))
                MR <- as.data.frame(matrix(nrow = nrow(request),ncol = 1))
                UCL <- as.data.frame(matrix(nrow = nrow(request),ncol = 1))
                LCL <- as.data.frame(matrix(nrow = nrow(request),ncol = 1))
                CL <- as.data.frame(matrix(nrow = nrow(request),ncol = 1))

                 for (i in 1:nrow(request)){
                  if(input$type=="p positivi"){
                    sigmapi[i,1] <- sqrt((pnc*(1-pnc))/as.numeric(request[i,4]))
                  }
                  else if(input$type=="p nuovi positivi"){
                    sigmapi[i,1] <- sqrt((pnc*(1-pnc))/as.numeric(request[i,3]))
                  }
                 }


               for (i in 1:nrow(request)){
                  zetai[i,1] <- as.numeric(request[i,5]-pnc)/sigmapi[i,1]
               }

                for(i in 2:nrow(request)){
                  MR[1,1] <- 0
                  MR[i,1] <-  as.numeric(abs(zetai[i,1]-zetai[i-1,1]))
                }
                MRbar <- (sum(MR)/as.numeric((nrow(request))-1))
                sigmaz <- MRbar/d2

                for(i in 1:nrow(request)){
                  UCL[i,1] <- pbar + 3*sigmaz*sigmapi[i,1]
                  LCL[i,1] <- pbar - 3*sigmaz*sigmapi[i,1]
                }
                CL <- rep.int(pbar,times = nrow(request))
                laney <- data.frame(request$data,"pnc"=request[5]) )} ```


S. Hesam
  • 5,266
  • 3
  • 37
  • 59
  • When it errors in shiny but not on the console, it might be due to the use of an input before the input is fully instantiated. With more complicated shiny apps, a block that depends on an input might trigger before there's a value, even if you define defaults. Look into `req` (and perhaps `validate`/`need` for nicer reporting) so that blocks that require non-null in an `input$...` don't trigger in this state. – r2evans Jun 11 '20 at 17:01
  • In the console i can't use any input because it's reactive (taking these values from the UI) so every formulas that involve one of the ```input$...``` is set up to a specific values and maybe it's because i'm setting non-reactive values that in the console the code work but in the app doesn't. – Stefano Celano Jun 11 '20 at 17:11
  • 2
    That's not what I'm saying. You said that you cannot reproduce the problem on the console, suggesting that you code works with "normal" input data. When the `shiny` environment gets going, in more complex apps it is feasible (even probable) that a reactive block that depends on some `input$...` will fire before that HTML entity is instantiated and/or the respective `input$...` variable is stable. `req` is meaningful only in a reactive construct, but if you aren't using it, then try it. For instance, `reactive({ req(input$somevar, input$var2); ... })`. – r2evans Jun 11 '20 at 17:14
  • Thanks for your patient, i'm newbie with R but now it is clearer what You meant, I've tried to use ```reactive({req(input$date,input$type,input$region,input$range)})``` inside my code but the problem persist. – Stefano Celano Jun 11 '20 at 17:24
  • Did that go in the reactive block(s) that depend on those inputs? – r2evans Jun 11 '20 at 17:38
  • I've wrote it inside my ```renderDygraph({})``` environment – Stefano Celano Jun 11 '20 at 17:40
  • I don't really know, Stefano, it's difficult to debug without more context. – r2evans Jun 11 '20 at 18:13
  • I know if there's something i can do to made the problem clearer tell me – Stefano Celano Jun 11 '20 at 18:23
  • Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and intended output *given that sample input*. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jun 11 '20 at 18:30

0 Answers0