2

I have a datatable that I construct using function1 and user input. I then want to use a cell value from this datatable as input to function2 to build another datatable. I am getting a filter error and think the issue is related to how I am selecting cell values. I am building of an existing SO solution here.

Assume we have iris data and function1 uses the user input to filter on species. The result of function1 is a result of a reactive call. Now function2 takes this result as input and uses the selected "cell" value (Sepal.Length) to further filter and build another datatable.

Thanks the comment by @Silentdevildoll, table2 is being constructed correctly (I think) but I still see this error:

Warning: Error in : Problem with filter() input ..1. i Input ..1 is Sepal.Length == var. x Input ..1 must be of size 50 or 1, not size 0.

library(shiny)
library(DT)

ui <- fluidPage(
 
  mainPanel(
    fluidRow(
      column(4, textInput('textInput1', label=NULL)),
      column(1, actionButton('button1', label=NULL))
    ),
    fluidRow(
      column(8, DT::dataTableOutput("table1")),
      column(4, DT::dataTableOutput("table2"))
    )
  )
)

server <- function(input, output, session){
  
   data(iris)



function1 <- function(id){
    return(dplyr::filter(iris, Species==id))
  }
  
  function2 <- function(df, var){
    return(dplyr::filter(df, Sepal.Length==var))  
  }
  
  data <- reactiveValues()
  
  observeEvent(input$button1, { data$ID <- input$textInput1 })

  table1_rec <- reactive({ function1(data$ID) })
  
  output$table1 <- DT::renderDataTable({
    req(data$ID)
    DT::datatable(table1_rec(), selection = list(target = 'cell'))
                    
  })
  
  table2_rec <- reactive({ 
    selected = table1_rec()[input$table1_cells_selected]
    function2(table1_rec(), selected) 
  })
  
  output$table2 <- DT::renderDataTable({
    DT::datatable(table2_rec())
  })

}

shinyApp(ui, server)
Cola4ever
  • 189
  • 1
  • 1
  • 16
  • I'm not sure of a specific solution, but I'm thinking your issue lies in function2. The function has two variables, but it looks like in your reactive you are only sending one? And then I'm not completely sure about this, but I don't think dplyr::filter will work with just (df, var). I think it needs to be logical, maybe something like "filter(across(everything(), ~ !grepl(var, .)))" which I saw here: https://stackoverflow.com/questions/22850026/filter-rows-which-contain-a-certain-string Best of luck! – Silentdevildoll Oct 22 '21 at 15:49
  • thanks @Silentdevildoll - I added the second variable to the question (typo) - let me take a look at the logical to see if it makes a difference. – Cola4ever Oct 22 '21 at 15:55
  • thanks again @Silentdevildoll - I fixed function2 and have a new error " no applicable method for 'filter' applied to an object of class "c('reactiveExpr', 'reactive', 'function')" but if you lose the reactive then you have a different error "Error : Operation not allowed without an active reactive context" - am confused, because function1 has a dplyr filter and it worked - probably a syntax error somewhere I am not seeing. – Cola4ever Oct 22 '21 at 16:03
  • I noticed table2_rec<-reactive({}) puts "table1_rec" to the function, not "table1_rec()". Also, function2<- has "Sepal.Lengt" instead of "Sepal.Length". Maybe fixing those two will make it work? – Silentdevildoll Oct 22 '21 at 17:38
  • @Silentdevildoll - thank you for your comment! It makes a big difference as I now can see table2 - there is still a filter error (the error disappears when I select a cell) - see the question for the updates! Many thanks! – Cola4ever Oct 22 '21 at 18:01
  • 1
    I figured it out! needed to add req(input$table1_cells_selected) in the first line of table2_rec <- reactive({}) as the table was being initialized but nothing was selected - OMG your comments saved me hours of staring at the screen! – Cola4ever Oct 22 '21 at 18:37

0 Answers0