4

I am trying to return a character vector created in a reactive expression in Shiny. However, when I run the app, I get the following:

Error: could not find function "check_case"

check_case is a reactive expression, not a function.

I can see a number of questions from people with similar issues, but the answers don't seem to fit this (for e.g., missing parentheses when calling reactive expression; calling something that hasn't been returned in the reactive expression).

I've tried immediately translating check_case() into a character vector in the output before using paste. I have also tried adding more arguments to paste so it includes sep = and collapse =, in case this is part of the issue, but this doesn't change the result I'm getting.

I currently have two theories:

  • There is an issue in the way I am using a reactive expression to return a character vector. Normally I use them to return dataframes so there might be something I'm missing here.
  • There is an issue in the way I am using paste to refer to a character vector.

Code:

library(shiny)
library(shinydashboard)


sidebar <- dashboardSidebar(
  selectInput(
  "case_select", label = "Select case",
  choices = c("Upper", "Lower")
  )
)

body <- dashboardBody(
  fluidRow(
      htmlOutput("text"))
)

ui <- dashboardPage(dashboardHeader(title = "Example"),
                    sidebar,
                    body
)

server <- function(input, output) {

  output$check_case <- reactive({
    if (input$case_select == "Upper") {
      case_list <- c("A", "B", "C")
      } else {
        case_list <- c("a", "b", "c")
      }
    return(case_list)
  })

  output$text <- renderUI({
    check_case <- check_case()
    HTML(paste(check_case, sep = "", collapse = ""))
  })
}
Jaccar
  • 1,720
  • 17
  • 46
  • 1
    There is no UI element named `check_case` as far as I can tell, so when you are outputting to a non-existent. – Chabo Dec 31 '18 at 16:23
  • Try getting rid of the `output` in `output$check_case`, since you want to create the reactive variable, not output, since that is being done by `output$text` – Chabo Dec 31 '18 at 16:25
  • Aaahh! You're right; it should have just been `check_case` rather than `output$check_case`. Thank you! – Jaccar Dec 31 '18 at 16:25
  • The expression that throws that error is `check_case()`. There is no `check_case` visible to the interpreter because it is a named list element and those are not visible outside the environment of `output`. – IRTFM Dec 31 '18 at 16:39

1 Answers1

3

The issue is regarding the fact you are trying to output a reactive variable while also creating it. It is also important to note that you need to output to an existing UI element when rendering.

This can simply be solved by creating the reactive varaible, and then subsequently rendering it with renderUI as you are doing with ouput$text

server <- function(input, output) {

  check_case <- reactive({
    if (input$case_select == "Upper") {
      case_list <- c("A", "B", "C")
      } else {
        case_list <- c("a", "b", "c")
      }
    return(case_list)
  })

  output$text <- renderUI({
    check_case <- check_case()
    HTML(paste(check_case, sep = "", collapse = ""))
  })
}

Just a simple error

Chabo
  • 2,842
  • 3
  • 17
  • 32