暂停闪亮的会话并返回到前端UI

时间:2018-11-21 18:30:27

标签: r shiny

当用户单击提交按钮时,是否有任何解决方案可返回到应用程序的当前状态。如果不满足一个条件,则应用显示信息并返回到初始UI。

  

observeEvent(input $ sub,{

if (input$test == TRUE) {
  shinyalert("You made error", type = "error")
  stop () #need back initial state before click button stop() stops app completely
}

}

1 个答案:

答案 0 :(得分:2)

您可以使用library(shinyjs)重置会话。以下是您提供的observeEvent之后的示例:

library(shiny)
library(shinyalert)
library(shinyjs)

jsResetCode <- "shinyjs.reset = function() {history.go(0)}"

ui <- fluidPage(
  useShinyalert(),
  useShinyjs(),
  extendShinyjs(text = jsResetCode),

   titlePanel("Old Faithful Geyser Data"),

   sidebarLayout(
      sidebarPanel(
        actionButton("sub","sub"),
        checkboxInput("test", "test"),
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),

      mainPanel(
         plotOutput("distPlot")
      )
   )
)

server <- function(input, output, session) {

  observeEvent(input$sub, {
    if(input$test == TRUE){
      shinyalert("You made error", type = "error")
    }
  })

  observeEvent(input$shinyalert, {
    if(input$shinyalert == TRUE){
      js$reset()
    }
  })

   output$distPlot <- renderPlot({
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

shinyApp(ui = ui, server = server)