1

I am really stumped on something. I’m struggling to get the code below to run. Usually the “i” in the for-loop will retain whatever the taxon name is (Opt1/Opt2/etc), but when I am generating the reactive expression, in all cases it updates to be the last value of “i” (i.e. Opt5). Is there a way to make it not reactive? There are few print statements in there for troubleshooting purposes, but I'm still struggling!

EC_settings_rv <- reactiveValues( mVariableOpt1="All", mVariableOpt2="With reads", mVariableOpt3="All", mVariableOpt4="All", mVariableOpt5="All")
TaxaDataEC<-c("Opt1", "Opt2","Opt3", "Opt4","Opt5")


for(i in  TaxaDataEC){
  var<-paste0("EC_settings_rv$mVariable",i)
  print(var)
  isolate(print(eval(parse(text=paste0("EC_settings_rv$mVariable",i)))))

  assign(paste0("mVariable",i,"_rv"),reactive({var}))

}   

isolate(print(mVariableOpt2_rv()))                        #I want it to give "EC_settings_rv$mVariableOpt2, but it gives "EC_settings_rv$mVariableOpt5"
isolate(print(eval(parse(text=mVariableOpt2_rv()))))      #I want it to give "With reads", but it gives "All"

Thanks for your help!

  • 2
    What are you trying to do? In what way do you intend these variables to be reactive if they are only holding static strings? – anotherfred Jan 09 '20 at 14:16
  • It is part of a much larger script where the static strings will change. I just put this short bit of code in the interests of brevity (e.g. "TaxaDataEC" will eventually contain 900+ items). What I am after is that for every item in TaxaDataEC (e.g. "Opt1"/"Opt2"/etc), I want to generate a reactive expression called e.g.mVariableOpt1/mVariableOpt2/etc, which when you run isolate(print(mVariableOpt1_rv()))/isolate(print(mVariableOpt2_rv()))/etc gives EC_settings_rv$mVariableOpt1/EC_settings_rv$mVariableOpt2/etc. However, with my current code, it always gives EC_settings_rv$mVariableOpt5 – Science.girl Jan 09 '20 at 14:53
  • The use of `eval`, `parse`, and `assign` here all seem very out of place. It's not clear to me what you are trying to do. It would help if you made a minimal self contained [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that can be used for testing so we can run it and see what's happening. I would strongly advise against creating a bunch of separate reactive values and instead store them in a reactiveValue list like you are doing with `EC_settings_rv` (though perhaps not in the most R-like way). – MrFlick Jan 09 '20 at 15:02
  • Sorry @MrFlick , I am new to this, and was under the impression it was reproducible. Not minimal certainly, but I wanted to leave the print statements in so you could see my issue. – Science.girl Jan 09 '20 at 15:19

0 Answers0