Skip to contents

These functions let you attach or retrieve a JavaScript condition string as a conditional_panel attribute on a vector (e.g. a column in a data frame). This attribute can then be fed directly into shiny::conditionalPanel() via its condition argument.

Usage

set_conditional_panel(x, condition, id = NULL)

get_conditional_panel(x)

Arguments

x

A vector (typically a column from a dataframe) to which the "show_only" attribute should be assigned or from which it should be extracted.

condition

A JavaScript expression that will be evaluated repeatedly to determine whether the panel should be displayed.

id

Character. A div class that should be associated to this condition. in the UI, all conditional_panel attributes will be assigned to a div with this class to allow for easy resetting in the server side. See display_filters() for proper implementation.

Value

  • set_conditional_panel returns the input vector x with a new conditional_panel attribute set to display_logic.

  • get_conditional_panel returns the value of the conditional_panel attribute, or NULL if it has not been set.

Examples

library(shiny)
library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union

# Sample data frame
df <- tibble(
  grp = c(rep("grp1", 4), rep("grp2", 3)),
  grp_1_income = c("15k - 30K", "35k - 50K", "15k - 30K", "35k - 50K", rep(NA, 3)),
  grp_2_region = c(rep(NA, 4), c("west", "east", "west"))
)

# Attach a JS condition: show only when input$region is "North" or "South"
df <- df |>
  mutate(
    grp_1_income = set_conditional_panel(
      grp_1_income,
      "input.grp == 'grp1'"
    ),
    grp_2_region = set_conditional_panel(
      grp_2_region,
      "input.grp == 'grp2'"
    )
  )

get_conditional_panel(df$grp_1_income) # Returns: "input.grp == 'grp1'"
#> $condition
#> [1] "input.grp == 'grp1'"
#> 
#> $id
#> [1] "div-7234e519-bc19-422b-bf6b-560d748e5e83"
#> 
get_conditional_panel(df$grp_2_region) # Returns: "input.grp == 'grp2'"
#> $condition
#> [1] "input.grp == 'grp2'"
#> 
#> $id
#> [1] "div-d2641ce7-93fe-4df6-af89-6e2a29eb72cf"
#>