Shiny - observeEvent appears without click
Shiny - observeEvent appears without click
I would like to develop an application with two buttons :
1. Calculate
2. Refresh
When I click on the button "Calculate", a second button appears. When I click on the second button, a sentence appears : "Le resultat est...". The button refresh clean the web page. Now the page is clean and only the two initial buttons appears : Calculate and Refresh.
If i click another time on the button Calculate, the sentence "Le resultat est..." appears without click on the second button.
Question : How can i do obtain the sentence "Le resultat est..." only after a click on the second button ?
Below my code :
library(shiny)
data_Modele <- deval_Shiny
ui <- fluidPage(
actionButton("runif", "Uniform"),
actionButton("reset", "Clear"),
uiOutput("plot")
)
server <- function(input, output){
v <- reactiveValues(data = NULL)
observeEvent(input$runif, {
v$data <- round(runif(1, min=1, max=nrow(deval_Shiny)),digits = 0)
output$Button<- renderUI({
actionButton("button", "click me")
})
})
observeEvent(input$reset, {
v$data <- NULL
})
observeEvent(input$button, {
output$Reponse <- renderText(paste0("Le resultat est :",v$data))
})
output$plot <- renderUI({
if (is.null(v$data)) return()
tagList(
uiOutput("Button"),
uiOutput("Reponse")
)
})
}
shinyApp(ui, server)
Thank you in advance for your help :)
J.
2 Answers
2
If you want your uiOutputs
to behave separately, I would suggest not to bind them together inside output$plot
. So if you don't need them to be together, I would add a variable show_response
to control whether you want to display the response or not.
uiOutputs
output$plot
show_response
library(shiny)
ui <- fluidPage(
actionButton("runif", "Uniform"),
actionButton("reset", "Clear"),
uiOutput("Button"),
uiOutput("Reponse")
)
server <- function(input, output){
v <- reactiveValues(data = NULL)
show_response <- reactiveValues(state = FALSE)
observeEvent(input$runif, {
v$data <- round(runif(1, min = 1, max = 100), digits = 0)
})
observeEvent(input$reset, {
v$data <- NULL
show_response$state <- FALSE
})
observeEvent(input$button, {
show_response$state <- TRUE
})
output$Button <- renderUI({
req(v$data)
actionButton("button", "click me")
})
output$Reponse <- renderText({
req(show_response$state)
paste0("Le resultat est :", v$data)
})
}
shinyApp(ui, server)
You can use shinyjs
and its show
and hide
functions:
shinyjs
show
hide
library(shiny)
library(shinyjs)
deval_Shiny <- mtcars
data_Modele <- deval_Shiny
ui <- fluidPage(
useShinyjs(),
actionButton("runif", "Uniform"),
actionButton("reset", "Clear"),br(),
actionButton("button", "click me"),
textOutput("Reponse")
)
server <- function(input, output){
observe({
hide("button")
hide("Reponse")
})
v <- reactiveValues(data = NULL)
observeEvent(input$runif,{
show("button")
v$data <- round(runif(1, min=1, max=nrow(deval_Shiny)),digits = 0)
})
observeEvent(input$reset, {
hide("button")
hide("Reponse")
})
output$Reponse <- renderText(paste0("Le resultat est :",v$data))
observeEvent(input$button, {
show("Reponse")
})
}
shinyApp(ui, server)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you very much
– Jeremy Bergeret
Jun 29 at 11:12