0

I have a dataframe that is filtered and displayed. I want to update the filtered data. I can do this easily without the filtering but I get an error when using filter on the left side of the assignment.

res <- nearPoints(isolate({filter(rv$RawData, container == CurrPlate)}), isolate({input$PlateMap_click}), allRows = TRUE)

isolate({filter(rv$RawData,container == CurrPlate)$keepRows <- xor(filter(rv$RawData,container == CurrPlate)$keepRows, res$selected_)})

dataTableProxy("RawData")

selectRows(dataTableProxy("RawData"), which(rv$RawData$keepRows == FALSE))

The data in the filters are fine.

 Called from: observerFunc()
 Browse[1]> filter(rv$RawData,container == CurrPlate)$keepRows
  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
 [32] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
 [63] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
 [94] TRUE TRUE TRUE
 Browse[1]> xor(filter(rv$RawData,container == CurrPlate)$keepRows, res$selected_)
  [1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [27]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [53]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [79]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

But I cant figure out the right way to update the data.

 Browse[1]> filter(rv$RawData,container == CurrPlate)$keepRows <- xor(filter(rv$RawData,container == CurrPlate)$keepRows, res$selected_)
 Error in filter(rv$RawData, container == CurrPlate)$keepRows <- xor(filter(rv$RawData,  : 
   could not find function "filter<-"
 Browse[1]> rv$RawData %>% filter(container == CurrPlate)$keepRows <- xor(filter(rv$RawData,container == CurrPlate)$keepRows, res$selected_)
 Error in rv$RawData %>% filter(container == CurrPlate)$keepRows <- xor(filter(rv$RawData,  : 
   could not find function "%>%<-"
 Browse[1]> 
Adam Wheeler
  • 393
  • 1
  • 11
  • Possible duplicate of [Set certain values to NA with dplyr](https://stackoverflow.com/questions/27909000/set-certain-values-to-na-with-dplyr) – Aurèle May 01 '18 at 12:54

1 Answers1

0

You can do this (replace with a "complex" expression on the left-hand-side of a <- assignment symbol), only if a method to do so has been explicitly provided.

It is hinted at in the error message: could not find function "filter<-"

Such methods for subsetted data frames and vectors exist in base R:

dat <- mtcars

dat[dat$cyl == 4, ]$mpg <- 333

or better, the recommended (because you don't subset the whole data frame, just the vector of interest):

dat$mpg[dat$cyl == 4] <- 42

work because $<-, [<-, [<-.data.frame, $<-.data.frame exist as functions. (Recommended read: help("[") and help("[.data.frame")).


I don't know of a fully satisfying way in the context of the tidyverse. I would go with replace:

library(dplyr)

dat <-
  dat %>% 
  mutate(mpg = replace(mpg, cyl == 4, -7))

Finally, data.table has a concise syntax for this:

library(data.table)

setDT(dat)[cyl == 4, mpg := 37]
Aurèle
  • 12,545
  • 1
  • 31
  • 49
  • Thank you! I ended up with this: `res <- nearPoints(isolate({filter(rv$RawData, container == input$PlateCode)}), isolate({input$PlateMap_click}), allRows = TRUE)` and then... `isolate({rv$RawData$keepRows[rv$RawData$container == input$PlateCode] <- as.data.frame(xor(rv$RawData[rv$RawData$container == input$PlateCode,], res$selected_))$keepRows})` – Adam Wheeler May 01 '18 at 14:18