簡體   English   中英

閃亮-無需單擊即可出現observeEvent

[英]Shiny - observeEvent appears without click

我想用兩個按鈕開發一個應用程序:1.計算2.刷新

當我單擊按鈕“計算”時,出現另一個按鈕。 當我單擊第二個按鈕時,將出現一個句子:“ Le resultat est ...”。 該按鈕刷新以清潔網頁。 現在頁面已經干凈,只有兩個初始按鈕出現:“計算”和“刷新”。

如果我再次單擊“計算”按鈕,則會出現句子“ Le resultat est ...”,而無需單擊第二個按鈕。

問題:僅在單擊第二個按鈕之后,如何才能獲得句子“ Le resultat est ...”?

在我的代碼下面:

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)

預先感謝您的幫助 :)

J.

如果您希望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)

您可以使用shinyjs及其showhide功能:

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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM