Skip to content

Instantly share code, notes, and snippets.

@richarddmorey
Created Jul 21, 2021
Embed
What would you like to do?
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