Hello, I have no idea how to modify my codes to avoid repeat computation.
What my understood about reactValues is that it stores values for using downstream. There has a Demo.
What I want is that SelectInput
value changes then the result changes. But the app will run observe()
from beginning. And the message(cor(x, y))
is a code to debug.
Thanks for your time.
# UI
ui <- fluidPage(
title = "test",
fluidRow(
column(4, offset = 2,
uiOutput('selects')
),
column(1, offset = 0,
uiOutput("txt")
)
)
)
# server
server <- function(input, output, session) {
rct <- reactiveValues()
observe({
x <- rnorm(1000)
y <- rnorm(1000)
message(cor(x, y))
rct$l <- list(X = 1,
Y = "a",
Z = "A")
if (!is.null(input$slt))
rct$v <- switch(input$slt,
X = rct$l$X,
Y = rct$l$Y,
Z = rct$l$Z)
})
output$selects <- renderUI({
selectInput("slt",
label = "values",
choices = c("X",
"Y",
"Z"),
width = "250px"
)
})
output$txt <- renderUI({
textOutput("txt_content")
})
output$txt_content <- renderText({
rct$v
})
}
# Run
shinyApp(ui = ui, server = server)