0

I need to loop over the contents of a column in a newly-created reactive (made by subsetting a data frame based on user input), and use it in a function.

for example:

target <- reactive({df$targetid[active$region == input$region]}) 

for (i in 1:length(target())){



target_info[[i]] <<- reactive({output <- someAPIcallingFunction(target=target(),
                                                     ma=7,
                                                     score_restrict = TRUE,
                                                     metric_restrict = "metric1")})
}

I'm not sure the square brackets are in the right place, but any help on the concept of looping over the output of a reactive would be REALLY appreciated! EDITED below for a working example.

  subset1<- reactive({
    BI_[input$brand == df$brand]
  })



  hello<- reactive({
    for (i in 1:nrow(subset1()))
      {
      print(subset1()[i,1]) }

AB

Duke Showbiz
  • 252
  • 1
  • 11
  • You need to use `local`, as I do here: https://stackoverflow.com/a/44506201/1100107 – Stéphane Laurent Jun 13 '17 at 18:45
  • Oh wait, you don't use `i` *inside* the `reactive`, so I'm not sure `local` is needed. It would be easier to help with a full (but minimal) working example. – Stéphane Laurent Jun 13 '17 at 18:47
  • Wow, and I just realize that your loop has a reactive length... I missed 2/3 of the question. Very interesting case. Please provide an example :) – Stéphane Laurent Jun 13 '17 at 18:49
  • It doesn't necessarily have to have a reactive length, but I couldn't think of another way to determine the length of the loop? I can provide a fuller example if needed! :-) – Duke Showbiz Jun 13 '17 at 18:52
  • 1
    Honestly, you should provide an example. We don't see what you want to achieve like this. What you do in the reactives (`output <- ...`) is not clear at all. – Stéphane Laurent Jun 13 '17 at 18:56
  • 1
    When do you want this loop to run? I think you are missing an `observe` or `observeEvent` of some sort for the update. A proper [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be very helpful. Describe the final behavior you want and focus less on how you tried to accomplish it. – MrFlick Jun 13 '17 at 19:22
  • Thanks all, something like the below: `subset1<- reactive({ BI_[input$brand == df$brand'] }) hello<- reactive({ for (i in 1:nrow(subset1())) { print(subset1()[i,1]) }`. Where the df is just a fake dataset of two columns, and I'm subsetting based on input == df col. So I'd like the loop to run when there is a change in the input$brand. The real version wouldn't print subset, it would populate an API call function's parameters – Duke Showbiz Jun 13 '17 at 19:40
  • I'm confused why the loop occurs outside of the reactive function. You should build everything inside the reactive space and assign the output to target_info. – Ryan Morton Jun 13 '17 at 21:38
  • if i even understand what youre trying to do, you dont need two reactives, you need an observer. you dont need a returned value you just want the side effects which says to me you need an observer, not a reactive. – Phi Jun 13 '17 at 22:44

1 Answers1

0

Found a solution to the issue (well a workaround):

I used lapply with the contents of the reactive as the datasource, BUT had to turn the convert the reactive into a list first.

 variableA<- reactive({as.list(areactive())})


     testing<- reactive({lapply(variableA(), 
              function(k) somefunction{parameter1=input1,parameter2=k}})

Hope this saves you some time!

AB

Duke Showbiz
  • 252
  • 1
  • 11