0
votes

This is a ShinyApp that outputs a graph and a table based on user input. Once the user clicks "go", the values update.

I was undecided between using "eventReactive" for every variable, like pt_class, arm, freq, etc.. vs using "observeEvent" in the beginning to capture the clicking of "go" once. I opted for the latter but I having trouble understanding why this error occurs:

Warning: Error in freq: could not find function "freq"
  72: observeEventHandler [~/ShinyApp/app.R#108]
   1: runApp 

As you can see in the code below, I have defined "freq" already... Apologies in advance as the app relies on other python scripts, but I am happy to share them if they are necessary to diagnose the problem.

ui <- navbarPage(numericInput(inputId = "vl",
                             "Viral load threshold (copies/mL):", value = 1000, min=50, max=10000000),
                             radioButtons("one_or_duration", "Duration at or above viral load threshold:",
                                          c("A single measurement","Multiple measurements")),
                             radioButtons("pt_class", "Time of treatment:",
                             c("All study participants","Early treated participants","Chronic treated participants")),
                             br(),
                             radioButtons("nnrti", "Include participants on NNRTIs?",
                                          c("No, exclude participants on NNRTIs","Yes, include participants on NNRTIs")),
                             radioButtons("freq", "Expected frequency of post-treatment controllers:",
                                          c("Same frequency as observed by authors","Input expected frequency")),
                             actionButton(inputId="go",label="Go!"),

                             mainPanel(
                             plotOutput(outputId = "graph"), DT::dataTableOutput(outputId="table")))

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


  observeEvent(input$go,{

  one_or_duration <- 
              if(input$one_or_duration == "A single measurement"){
              "single"}
              else if(input$one_or_duration == "Multiple measurements"){
              "multiple"}

  nnrti <- 
    if(input$nnrti == "Yes, include participants on NNRTIs"){
      "yes"}
    else if(input$nnrti == "No, exclude participants on NNRTIs"){
      "no"}

  freq <- 
    if(input$freq == "Same frequency as observed by authors"){
      "same"}
    else if(input$freq == "Input expected frequency"){
      "diff"}

  pt_class <- 
    if(input$pt_class == "All study participants"){
      "all"}
    else if(input$pt_class == "Early treated participants"){
      "early"}
    else if(input$pt_class == "Chronic treated participants"){
      "chronic"}

  ptcs_plus_ncs <- 
    if (freq() == "same"){do_this
}


})}

shinyApp(ui = ui, server = server)
1

1 Answers

1
votes

In the current form I am not able to run your code, so I cannot look deeper into the problem.

At first glance, however, R throughs said error because you do not define freq as an reactive, but still refer to it as freq() which causes R to look for a function called freq. The same holds for a bunch of other variables such as pt_class(), one_or_duration(), nnrti(), arm() etc.

The part with observeEvent(input$go,{ is kind of problematic and it seems that you don't need the if-clauses. Instead use named vectors in the UI input values. For example instead of

radioButtons("one_or_duration", "Duration at or above viral load threshold:",
             c("A single measurement","Multiple measurements"))

write

radioButtons("one_or_duration", "Duration at or above viral load threshold:",
             c("A single measurement" = "single",
               "Multiple measurements" = "multiple"))

and then you would not need the part saying

one_or_duration <- 
              if(input$one_or_duration == "A single measurement"){
              "single"}
              else if(input$one_or_duration == "Multiple measurements"){
              "multiple"}

And later, instead of using one_or_duration() (which is not a reactive and cannot be called by adding brackets) just use input$one_or_duration. By the way, you do not need to make input variables reactive, because they already are.

This definitely doesn't solve all of your code's problems, but it might be a start.