I'm interested in developing a Shiny App that would act in the following manner:
- Update the chart following any changes to the number of user interface elements. This done through the
actionButton
/isolate
construct. - Display a warning text prompting user to click the Update button. Outstanding.
- Remove the warning text after the Update button was pressed. Outstanding.
Example
Approach
- Drawing from one of the examples available via rstudio the following example provides access to two functionalities that modify the histogram.
- Histogram is updated upon the Update button click.
- Upon interaction with the UI the
observeEvent
construct should update the warning message that is then passed torenderText
. - If there is no interaction with the UI the
renderText
can return empty string. actionButton
should revert warning message string value to emptyc("")
. Alternative approach making use frominsertUI
/renderUI
is acceptable.
Code
app.R
library(shiny)
ui <- fluidPage(titlePanel("Reactive UI"),
sidebarLayout(
sidebarPanel(
# Reactive text should appear upon any change here
sliderInput(
"bins",
"Number of bins:",
min = 1,
max = 50,
value = 30
),
checkboxInput(
inputId = "chckBxLg",
label = "Log scale",
value = FALSE
)
,
actionButton("btnUpdate", "Update")
),
mainPanel(textOutput("msgTxt"),
plotOutput("distPlot"))
))
# Define server logic required to draw a histogram
server <- function(input, output) {
# Create placeholder object for msg
msgUpdateText <- c("")
# Insert update message text upon change to UI elements
observeEvent(eventExpr = {
input$bins
input$chckBxLg
},
handlerExpr = {
# This message should only show after interaction with the UI
isolate(msgUpdateText <<-
c("You have clicked something, update the chart"))
})
# Render text
output$msgTxt <- renderText(msgUpdateText)
output$distPlot <- renderPlot({
input$btnUpdate
isolate({
x <- faithful[, 2]
if (input$chckBxLg) {
x <- log(x)
}
bins <-
seq(min(x), max(x), length.out = input$bins + 1)
# Also delete the text message
msgUpdateText <<- c("")
})
hist(x,
breaks = bins,
col = 'darkgray',
border = 'white')
})
}
shinyApp(ui = ui, server = server)
Problem
The message:
You have clicked something, update the chart
should only appear only after user interacts with the UI and disappear after the actionButton
is pressed, instead the message is visible permanently.
Side notes
The offered solution should be extensible across a number of UI, elements. The provided, not-working, example attempts to capture change to two UI elements:
observeEvent(eventExpr = {
input$bins # Element 1
input$chckBxLg # Element 2
},
handlerExpr = {
# This message should only show after interaction with the UI
isolate(msgUpdateText <<-
c("You have clicked something, update the chart"))
})
I'm striving for the code to accommodate a vast number of elements, on the lines
observeEvent(eventExpr = {
input$bins # Element 1
input$chckBxLg # Element 2
input$title # Element 3
input$n # Element n
... ...
},
handlerExpr = {
# User interacted with any of the UI elements listed above
# Update text message to be displayed in the app
})