Search code examples
rshinygolem

making conditionalPanel work with input parameter in a R shiny Golem package


i usually see the conditionalPanel work with an event happening, like a button press or a checkbox checked etc. but here, i want it to work with a parameter passed when i run the app. i am trying to show the conditional panel only if the "input" parameter is not null, but the panel appears all the time. here's the code i tried:

app_ui.R

#' The application User-Interface
#'
#' @param request Internal parameter for `{shiny}`.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_ui <- function(request) {
  tagList(
    # Leave this function for adding external resources
    golem_add_external_resources(),
    # List the first level UI elements here
    fluidPage(
      h1("testapp"),
      conditionalPanel(
        condition = "!is.null(r.input)",
        p('there is an input ')

      )
    )
  )
}

app_server.R

#' The application server-side
#'
#' @param input,output,session Internal parameters for {shiny}.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_server <- function( input, output, session ) {
  # List the first level callModules here
  r <- reactiveValues(query = reactiveValues())

  observe({
    input <- golem::get_golem_options("input")
    r$input <- input

  })
}

run_app.R

#' Run the Shiny Application
#'
#' @param input input
#'
#' @export
#' @importFrom shiny shinyApp
#' @importFrom golem with_golem_options
run_app <- function(
  input = NULL
) {
  with_golem_options(
    app = shinyApp(
      ui = app_ui,
      server = app_server
    ),
    golem_opts = list(input = input)
  )
}

Solution

  • The beauty of get_golem_options("input") is that it can be uses inside your UI too :)

    So I think you can do something simpler, like :

    app_ui <- function(request) {
      tagList(
        # Leave this function for adding external resources
        golem_add_external_resources(),
        # List the first level UI elements here
        fluidPage(
          h1("testapp"),
          if (!is.null(golem::get_golem_options("input")){
              your_UI_here(...)
          }
        )
      )
    }