0

As stated in the title, I have two separate if statements in my R code. The first is functioning properly, but the second is being treated as if it is always true, even when it isn't. Here's the relevant code:

  narrowed <- reactive({
    nar <- data.frame(ref.range())
    exl <- which(nar$Result < input$minimum)
    if(length(exl) >= 1){
      nar <- nar[-exl,]
    }
    exh <- which(nar$Result > input$maximum)
    if(length(exh) >= 1){
      nar <- nar[-exh,]
    }
  })

When the Shiny App plots the data in the narrowed() dataframe, it is apparent that it is excluding exh from the data, whether exh has a length >0 or not. However, it is only excluding exl from the data when exl has a length >0. I know it's only evaluating the first if statement because if I put the exh-related lines first, it excludes exl all the time but only excludes exh when it has a length >0.

I tried splitting it into two separate reactive functions so it looked like this:

  narrow <- reactive({
    nar <- data.frame(ref.range())
    exl <- which(nar$Result < input$minimum)
    if(length(exl) >= 1){
      nar <- nar[-exl,]
    }
  })
  narrowed <- reactive({
    narr <- data.frame(narrow())
    exh <- which(narr$Result > input$maximum)
    if(length(exh) >= 1){
      nar <- nar[-exh,]
    }
  })

but that gave the error "evaluation nested too deeply: infinite recursion / options(expressions=)?" So dividing the work between two subsets is not the answer. I have written other R scripts with separate if statements like what I have here with no issue, so I don't know what the problem is. Any help would be greatly appreciated.

  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. As is it's hard to come up with a woking solution. But IMHO you should add a `return(nar)` to your reactive to actually return the filtered dataframe. Moreover, instead of using two `if` statements you could do `nar[nar$Result >= input$minimum & nar$Result <= input$maximum, , drop = FALSE]`to filter your dataset. – stefan Jul 29 '22 at 20:18
  • Do you mean `nrow(exh)` where you have `length(exh)`? The length of a dataframe is the number of columns, regardless of # of rows: `length(data.frame(var = numeric()))`. More here: https://stackoverflow.com/a/35306102/6851825 – Jon Spring Jul 29 '22 at 21:15
  • exh in this instance is a vector created by the which() function, so length(exh) is assessing how long the vector is. The default is an empty vector, which is where to issue comes in because R is trying to remove an positions in an empty vector from a dataframe. This results in removing the entire dataframe because R can't do that. When I run the code as a plain script, substituting input$minimum and input$maximum for the numbers that the user would enter, the code works exactly the way I think it should. It's only in this instance that it always treats the second if() statement as TRUE – Lord Zombitten Jul 29 '22 at 21:24
  • Thanks for the advice, Stefan. In the future, I'll provide a minimal reproducible example with my questions. Your code suggestion worked perfectly, though. – Lord Zombitten Jul 29 '22 at 21:39

1 Answers1

1

Thank so much, Stefan! The answer I needed was "...instead of using two if statements you could do nar[nar$Result >= input$minimum & nar$Result <= input$maximum, , drop = FALSE] to filter your dataset."