Skip to content

Instantly share code, notes, and snippets.

@thisisnic
thisisnic / insanity_and_filters.R
Last active June 21, 2023 02:45
Dynamic filter generation
library(shinyjs)
library(shiny)
library(purrr)
library(stringr)
library(glue)
#' Remove element named "id" from a list
remove_id_element <- function(x){
x$id = NULL
x
@thisisnic
thisisnic / expensive_operation_second_isolate.R
Created May 3, 2019 10:51
Shiny app in which expensive operation could block up using the datatable but prevented somewhat by proxy variable
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
fluidRow(
textOutput("processStatus")
),
fluidRow(DTOutput("myTable"))
)
@thisisnic
thisisnic / expensive_operation_datatableproxy.R
Created May 3, 2019 10:19
Shiny app with expensive operation - still pauses with datatableproxy
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
DTOutput("myTable")
)
server <- function(input, output, session){
@thisisnic
thisisnic / expensive_operation_reactive.R
Last active May 3, 2019 10:20
Shiny app in which an expensive operation and frequent invalidation interferes with rendering of table
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
DTOutput("myTable")
)
server <- function(input, output, session){
@thisisnic
thisisnic / scrollDTHeader.js
Last active April 11, 2019 09:16
JS code to fix DataTable issue with floating header not scrolling horizontally.
/*
The div we've manually created, with a class of 'outerDivClassName'
which contains the scrollbar we want to watch. In the CSS, give
this class the property "overflow: auto;", so that a scrollbar appears
when its contents are too large to fit inside.
*/
const scrollBox = document.querySelector('.outerDivClassName');
function scrollHeaderFunc(){
@thisisnic
thisisnic / nested_module_dependencies.R
Created April 8, 2019 09:44
Example of how only the relevant reactives update in a child module when a value to be passed through from a parent is updated
# Nested modules - note that when something in the parent module is updated,
# only the elements of the child module which had reactive dependencies will
# update.
#
# * Examine what is updated when we click the "show row numbers" checkbox
# * Now examine when the data_count output is updated.
library(shiny)
library(DT)
@thisisnic
thisisnic / gist:e6e1b4bf07fd0e730c0c117862463b83
Created April 8, 2019 09:06
Simple Shiny nested module example
# Adapted from https://stackoverflow.com/questions/48592056/shiny-reactivity-not-working-in-submodules
library(shiny)
library(DT)
filtersUI <- function(id) {
ns <- NS(id)
selectizeInput(
ns("Species"),
@thisisnic
thisisnic / action_button_in_DT.R
Last active January 12, 2024 00:38
Example of how to include actionButtons in a DT, with one observer for all buttons
# Adapted from https://stackoverflow.com/questions/45739303/r-shiny-handle-action-buttons-in-data-table
library(shiny)
library(DT)
library(tibble)
#' Programmatically create a Shiny input
#'
#' @param FUN function to create the input
#' @param n number of inputs to be created
@thisisnic
thisisnic / back_button_confirm.js
Last active December 17, 2022 12:11
Confirm "back" browser button click in Shiny
history.pushState(null, null, null);
window.addEventListener('popstate', function () {
const endProgress = confirm('This will end your session, are you sure you want to go back?');
if (endProgress) {
window.onpopstate = null;
history.back();
} else {
@thisisnic
thisisnic / enterkey.js
Created January 16, 2019 18:28
JavaScript for "enter" key when focussed on a text field triggering activation of button
$(document).keyup(function(event) {
if ((event.keyCode == 13) && (document.activeElement.id == "login_password")) {
$("#login_button").click();
}
});