2

I need to change certain numbers to NA in this reactive dataframe, for example all -1 to NA or all negative numbers, so they don't plot in the historgam

pointsInBounds <- reactive({
  if (is.null(input$map_bounds))
    return(Data[TRUE,])

  bounds <- input$map_bounds
  latRng <- range(bounds$north, bounds$south)
  lngRng <- range(bounds$east, bounds$west)

  subset(Organisms(),
         lat >= latRng[1] & lat <= latRng[2] &
           lon >= lngRng[1] & lon <= lngRng[2])
})
     ##histogram
output$hist <- renderPlot({plot1<-
  # If no organisms are in view, don't plot
  if (nrow(pointsInBounds()) == 0)
    return()


hist(...)
Allie
  • 105
  • 9
  • You should be able to use the same procedure as you normally would in R: `df[df < 0] <- 0` – roarkz Aug 14 '17 at 13:13
  • I receive this error code when using that `comparison (3) is possible only for atomic and list types` – Allie Aug 14 '17 at 13:25
  • Possible duplicate of [Formatting reactive data.frames in Shiny](https://stackoverflow.com/questions/20201070/formatting-reactive-data-frames-in-shiny) – John Paul Aug 14 '17 at 14:11

2 Answers2

0

Are you looking to apply this transformation to any negative numbers in the dataframe or specific columns?

Either way, the methods for doing this may be about the same as what you would do outside a reactive context. For example, using dplyr to change all negative numbers in the lat column of your Organisms reactive object:

Organisms() %>%
  mutate(lat = ifelse(lat < 0, NA, lat))

or ifelse(lat == -1, NA, lat)).

Of course, my specific solution makes no sense if you want to avoid dplyr or change negative numbers in all columns, but whatever the specific change is, you may be able to apply it to Organisms as you would a non-reactive dataframe, just by adding in the parenthesis.

  • It's not the `lat` and `lng` I want to change its all the other columns in `df`. – Allie Aug 14 '17 at 13:37
  • Ah, ok. So maybe something like: `mutate_at(vars(var1:var2, var3), ~ ifelse(. < 0, ., NA))`? Substituting in whatever columns in the df you want – lucini_p Aug 14 '17 at 13:57
0

I fix it by adding in

 `pointsInBounds<-reactive({ 
  DataP<-data.frame(pointsInBounds2())
  colnames(DataP)<-c(colnames(Data))
  DataP[DataP==-1]<-NA
  return(DataP)
  })`

after the first reactive dataframe

Allie
  • 105
  • 9