Set/Get conditional_panel Attribute for Shiny Conditional Panels
Source: R/filter_data.R
conditional_panel.RdThese 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.
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_panelattributes will be assigned to a div with this class to allow for easy resetting in the server side. Seedisplay_filters()for proper implementation.
Value
set_conditional_panel returns the input vector
xwith a newconditional_panelattribute set todisplay_logic.get_conditional_panel returns the value of the
conditional_panelattribute, orNULLif 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"
#>