0

I'm trying to create a shiny based on a R code previously created.

The problem I'm facing is that the for loops are not working.

One example would be:

    
    centros.acp=reactive({colMeans(a.acp())})
    
    lcov.acp=reactive({solve(cov(a.acp()))})
    
    dm.acp=reactive({rep(0,length(a.acp()[,1]))})
    
    na.acp=reactive({a.acp()[1]})
    
    dm.acp()=reactive({
      value=dm.acp()
      for(i in na.acp()){

      value()[i]<-reactive({round(t(a.acp()[i,]-centros.acp())%*%lcov.acp()%*%(a.acp()[i,]-centros.acp()),3)

    })}})
    T2<-reactive({dm.acp()})
    my_list=reactive({list("T2" = T2(),"cc.sw.UCL.99" = cc.sw.UCL.99(),"cc.sw.UCL.95" = cc.sw.UCL.95(), "num.com"=num.com())})
   
   output$plot_t2=renderPlot({plot(1:n(),my_list()$T2, ylim = c(0,max(my_list()$T2,my_list()$cc.sw.UCL.99)+3),main = "",
         xlab = 'OBS',ylab = 'T2') })

I get the error

Error in dm.acp() = reactive({ : invalid (NULL) left side of assignment

The code I show is the only part that matters for the problem, everything else is working. I have tried different ways but always get the error or the graph plotted is based on the original dm.acp() that consists in group of 0. When I run the for loop, I want to substitute the 0 by values (in each iteration a new number is changed).

The original dm.acp() is

> dm.acp
 [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[50] 0 0 0 0 0 0 0 0 0 0 0

and after the for loop should be

[1]  6.210  3.663  4.246  4.279  3.766  3.583  2.957  4.796  4.578  3.225  4.232  2.865  6.976  3.454
[15]  3.411  3.530  3.610 22.747  9.936  2.659  3.080  6.412  9.183  2.781  3.253  5.585  3.183  5.171
[29]  3.898  5.868  2.498 12.901  2.772  2.795  1.644  1.393 21.078  2.489  3.198  2.412  4.213  4.251
[43]  8.144  3.201  3.295  4.378  4.746  9.481  4.281  4.402  5.329  2.637  2.627  4.507  5.521  3.545
[57]  2.623  5.835  2.438  3.230

I appreciate any help. Thank you !

Sevlar
  • 5
  • 4
  • It's easier to help you if you include a minimal [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 14 '20 at 17:33
  • @MrFlick I added data to the outputs, thank you ! – Sevlar Jul 14 '20 at 17:38

1 Answers1

0

Try replacing your for loop with lapply as given below:

lapply(1:na.acp(), function(i){
  value()[i]<-reactive({
    round(t(a.acp()[i,]-centros.acp())%*%lcov.acp()%*%(a.acp()[i,]-centros.acp()),3)
  })
})

Update: Please try this code.

dm.acp<-c(rep(0,60))
    
    n <- length(dm.acp)
    lapply(1:n, function(i) { 
      dm.acp[i] <<- i+2
    })

You should be able to implement this in your program. If you are going to use a function within lapply, please enclose it as lapply(1:(length(na.acp)),function(i){…})

Update2: Please see the code below

###  replace this code  ###
dm.acp=reactive({rep(0,length(a.acp()[,1]))})

na.acp=reactive({a.acp()[1]})

dm.acp()=reactive({
  value=dm.acp()
  for(i in na.acp()){
    
    value()[i]<-reactive({round(t(a.acp()[i,]-centros.acp())%*%lcov.acp()%*%(a.acp()[i,]-centros.acp()),3)
      
    })}})
T2<-reactive({dm.acp()})


###  with this code   ###
dm.acpv=reactive({rep(0,length(a.acp()[,1]))})

na.acp=reactive({a.acp()[1]})

T2 <- reactive({
  value=dm.acpv()
  
  lapply(1:(na.acp()), function(i){
    value[i] <<- round(t(a.acp()[i,]-centros.acp())%*%lcov.acp()%*%(a.acp()[i,]-centros.acp()),3)
  })
  value
})
## no need to define T2 again as I renamed dm.acp as T2
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Thank you for the help. It initially gave me this error ` Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) ` So i put the function inside a reactive, and changed ` T2<-reactive({value()}) ` but now gives-me the error ` could not find function "value" ` Any way to solve this problem? – Sevlar Jul 15 '20 at 12:29
  • As stated by @MrFlick, it would be best if you posted a reprex. However, looking at your code, `dm.acp()=reactive({value=dm.acp(),...})`, it appears that you are accessing something you are defining. Please change it to `dm.acpp<-reactive({value=dm.acp(),...})`, and use dm.acpp after this stage. That should work. You do not need parenthesis `()` when you are defining a reactive, and you cannot call itself within - Just give it a different name. – YBS Jul 15 '20 at 13:41
  • Thank you for the help. Now it gives me the error `invalid (NULL) left side of assignment` It is very complicated to show the whole data since as you can see there is a lot of variables. An easy way to test would be to do `dm.acp<-c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)` Then do `for(i in 1:length(dm.acp)){ dm.acp[i]=i }` in normal R code. I can't show you how to do in reactive since it is giving me errors. – Sevlar Jul 15 '20 at 17:43
  • How would I apply the updated code in the reactive way, the original one? Thank you a lot! – Sevlar Jul 16 '20 at 01:12