-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added post about conditionaPanel use in shiny modules
- Loading branch information
Showing
9 changed files
with
1,804 additions
and
953 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
_freeze/posts/conditional_shiny_widgets/index/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"hash": "8e83ad6de44ca20bc5a94a71afa6e768", | ||
"result": { | ||
"markdown": "---\ntitle: \"Conditional panels within shiny modules\"\nauthor: \"Thomas Sandmann\"\ndate: \"2024-04-07\"\nfreeze: true\ncategories: [R, shiny, TIL]\neditor:\n markdown:\n wrap: 72\nformat:\n html:\n toc: true\n toc-depth: 4\n code-tools:\n source: true\n toggle: false\n caption: none\neditor_options: \n chunk_output_type: console\n---\n\n\n## tl;dr\n\nThe \n[shiny R package](https://cran.r-project.org/package=shiny)\nincludes the\n[conditionalPanel](https://rstudio.github.io/shiny/reference/conditionalPanel.html)\nfunction to show / hide a panel depending on a javascript expression. Today I\nlearned how to use a `conditionalPanel` within a\n[shiny module](https://mastering-shiny.org/scaling-modules.html).\n\n## A shiny module with several conditional selectors\n\nThe following example [^1] expands the example shown on the \n[conditionalPanel help page](https://rstudio.github.io/shiny/reference/conditionalPanel.html). \n\n[^1]: Also available in \n[this gist](https://gist.github.com/tomsing1/884f32a3f40abd9a937dab0c54d23fda)\n\nIt encapsulates the ui and server elements in separate functions, that together\nconstitute the `mod_histogram` module. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(shiny)\n\nmod_histogram_ui <- function(id){\n ns <- NS(id) # <1>\n fluidPage(\n sidebarPanel(\n selectInput(ns(\"plotType\"), \"Plot Type\",\n c(Scatter = \"scatter\", Histogram = \"hist\")\n ),\n # Only show this panel if the plot type is a histogram\n conditionalPanel(\n condition = \"input.plotType == 'hist'\",\n ns = ns, # <2>\n selectInput(\n ns(\"breaks\"), \"Breaks\",\n c(\"Sturges\", \"Scott\", \"Freedman-Diaconis\", \"[Custom]\" = \"custom\")\n ),\n # Only show this panel if Custom is selected\n conditionalPanel(\n condition = \"input.breaks == 'custom'\",\n ns = ns,\n sliderInput(ns(\"breakCount\"), \"Break Count\", min = 1,\n max = 100, value = 10)\n )\n )\n ),\n mainPanel(\n plotOutput(ns(\"plot\"))\n )\n )\n}\n\nmod_histogram_server <- function(id, df, labels, interactive = FALSE){\n moduleServer( id, function(input, output, session){\n x <- rnorm(1000)\n y <- rnorm(1000)\n\n output$plot <- renderPlot({\n if (input$plotType == \"scatter\") {\n plot(x, y)\n } else {\n breaks <- input$breaks\n if (breaks == \"custom\") {\n breaks <- input$breakCount\n }\n hist(x, breaks = breaks)\n }\n })\n })\n}\n\nhistogramApp <- function() {\n ui <- mod_histogram_ui(\"histogram_1\")\n server <- function(input, output, session) {\n mod_histogram_server(\"histogram_1\")\n }\n\n shinyApp(ui, server)\n}\n```\n:::\n\n\n1. The `ns` object defined in the UI part of the module takes care of\n managing the module's namespace.\n \n2. The `ns` namespace is passed as an argument to each `conditionalPanel` call.\n It automatically modifies the `condition` javascript expression, so we do \n _not_ need to include the module's id ourselves (e.g. via `paste` or \n `sprintf`, as is sometimes advised on Stack Overflow).\n\nTo see a minial shiny app, call the `histogramApp()` function:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nhistogramApp()\n```\n:::\n\n\n\n## Reproducibility\n\n<details>\n<summary>\nSession Information\n</summary>\n\n\n::: {.cell}\n\n```{.r .cell-code}\nsessioninfo::session_info(\"attached\")\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n─ Session info ───────────────────────────────────────────────────────────────\n setting value\n version R version 4.3.2 (2023-10-31)\n os Debian GNU/Linux 12 (bookworm)\n system x86_64, linux-gnu\n ui X11\n language (EN)\n collate en_US.UTF-8\n ctype en_US.UTF-8\n tz America/Los_Angeles\n date 2024-04-07\n pandoc 3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)\n\n─ Packages ───────────────────────────────────────────────────────────────────\n ! package * version date (UTC) lib source\n P shiny * 1.7.5 2023-08-12 [?] CRAN (R 4.3.1)\n\n [1] /home/sandmann/repositories/blog/renv/library/R-4.3/x86_64-pc-linux-gnu\n [2] /home/sandmann/.cache/R/renv/sandbox/R-4.3/x86_64-pc-linux-gnu/9a444a72\n\n P ── Loaded and on-disk path mismatch.\n\n──────────────────────────────────────────────────────────────────────────────\n```\n:::\n:::\n\n\n</details>\n ", | ||
"supporting": [], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": {}, | ||
"engineDependencies": {}, | ||
"preserve": {}, | ||
"postProcess": true | ||
} | ||
} |
Oops, something went wrong.