0

So, I've been on google for hours with no answer.

I want to create a user-defined function inside the server side that takes inputs that I already know to wrap reactive({input$feature)} but the issue is how to incorporate reactive values as inputs too.

The reason why I want to do this is because I have a navbarPage with multiple tabs that shares elements such as same plots. So I want a user defined function that creates all the similar filtering and not have to create multiple of the same reactive expression with different input and reactive variable names which take up 2000+ lines of code.

server <- function(input, output) {

filtered_JointKSA <- reactiveVal(0)

create_filtered_data <- function(df, input_specialtya, filtered_JointKSA) {

    if (input_specialtya == 'manual') {

      data <- filter(data, SPECIALTY %in% input_specialtyb)

    }

    if (filtered_JointKSA != 0) {

      data <- filter(data, SPECIALTY %in% filtered_JointKSA)
    }

 reactive({return(data)})
}

  filtered_data <- create_filtered_data(df, 
                                         reactive({input$specialty1}),
                                         filtered_JointKSA())


  observeEvent(
    eventExpr = input$clickJointKSA, 
    handlerExpr = {

      A <- filtered_JointKSA(levels(fct_drop(filtered_data()$`Joint KSA Grouping`))[round(input$clickJointKSA$y)])
      A

    }
  )



This gets me an error:

"Error in match(x, table, nomatch = 0L) : 'match' requires vector arguments"

The error is gone if I comment out where I try to create filtered_data but none of my plots are created because filtered_data() is not found.

What is the correct approach for this?

Ideally, I would like my observeEvents to be inside user defined functions as well if that has a different method.

Skywind555
  • 31
  • 1
  • 7
  • It's hard to follow what's going on without a [reproducible example](https://stackoverflow.com/questions/48343080/how-to-convert-a-shiny-app-consisting-of-multiple-files-into-an-easily-shareable), but I think you might try wrapping your call to `create_filtered_data()` in a `reactive`. See answer below for a template, which perhaps you can adapt. – phalteman May 24 '19 at 18:40

1 Answers1

0

This example may provide some help, but it's hard to tell without a working example. The change is to wrap the call to your function in reactive({}) rather than the inputs to that function, so that the inputs are all responsive to user input and the function will update.

library(shiny)

ui <- fluidPage(
  numericInput("num", "Number", value = NULL),
  verbatimTextOutput("out")
)

server <- function(input, output){

  ## User-defined function, taking a reactive input
  rvals <- function(x){
    req(input$num)
    if(x > 5){x * 10} else {x*1}
  }

  # Call to the function, wrapped in a reactive
  n <- reactive({ rvals(input$num) })

  # Using output of the function, which is reactive and needs to be resolved with '()'
  output$out <- renderText({ n() })

}

shinyApp(ui, server)
phalteman
  • 3,442
  • 1
  • 29
  • 46