Skip to content

Instantly share code, notes, and snippets.

@richarddmorey
Created July 21, 2021 20:27
Show Gist options
  • Save richarddmorey/b486b91acfddb5d0dd2d3fda193f9a37 to your computer and use it in GitHub Desktop.
Save richarddmorey/b486b91acfddb5d0dd2d3fda193f9a37 to your computer and use it in GitHub Desktop.
testing answer to stackexchange question
# Test answer to https://stackoverflow.com/questions/68473292/prevent-double-execution-of-shiny-reactive-with-two-dependency-pathways/68475515#68475515
library(shiny)
library(digest)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("M", "M:", ""),
uiOutput("S_UI"),
selectInput("A", "A:", c("A1","A2"))
),
mainPanel(
verbatimTextOutput("C")
)
)
)
server <- function(input, output, session) {
# Count calculations of C
count = 0
# This value just exists to tell us whether M has changed
# from its default of "". Doesn't need to be reactive
changedM = FALSE
S = reactive({
input$S
})
# Create "content" from A and S
C <- reactive({
req(S())
count <<- count + 1
Sys.sleep(0.5)
message("Count ", count)
paste(
"Count: ", count, "\n", digest::sha1( c(input$A, S() ))
)
})
output$S_UI <- renderUI({
val <- digest::sha1(c(input$M, input$A))
curr_val <- isolate(input$S)
changedM <<- changedM || input$M != ""
if(is.null(curr_val) || (changedM & input$M == "")){
val <- "testS"
} else if (input$M == "" & !changedM) {
val <- curr_val
} else {
freezeReactiveValue(input, "S")
}
return(textInput("S", "S:", val))
})
# "Content"
output$C <- renderText({
C()
})
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment