【问题标题】:R Shiny - Refresh app session (shinyjs.reset) when navigating to a specific navbarPage tabR Shiny - 导航到特定 navbarPage 选项卡时刷新应用程序会话 (shinyjs.reset)
【发布时间】:2019-01-25 16:32:43
【问题描述】:

我的应用程序通过一系列面板进行,用户可以通过单击操作按钮进入下一个面板。我希望在导航到另一个选项卡时刷新整个应用程序。这意味着用户将从一系列面板中的第一个开始,并且任何小部件输入都将被重置。

我已经为面板提供了 ID,并尝试使用

刷新应用程序
    shinyjs.reset = function() {history.go(0)}

在一个

    observeEvent()

我还尝试重置条件面板选择中使用的输入以及相关的小部件输入。这些方法似乎都不能刷新应用程序。

library(shiny)
library(shinyjs)

#### Specify the number of pages to use 
NUM_PAGES <- 2

# Define the js method that resets the page 
jsResetCode <- "shinyjs.reset = function() {history.go(0)}"  


# UI
ui <- navbarPage(

  # Call shinyjs
  useShinyjs(),                                          
  extendShinyjs(text = jsResetCode),

  ### Panel 1 ### 
  tabPanel(id = "p1",
           title = "A",
           "Page 1"),

  ### Panel 2 ### 
  tabPanel(id = "p2",
           title = "B",
           div(id = "form",

               ### Sub-panel 1
               conditionalPanel(
                 condition = "input.nextBtn_1 == 0",
                 radioButtons(inputId ="Q1", ("Q1"), choices = c("A", "B"), 
                              selected = character(0)),

                 # Next page button 
                 div(actionButton("nextBtn_1", "Next1 >"))),

               ### Sub-panel 1
               conditionalPanel(
                 # Q1
                 condition = "input.nextBtn_1 == 1 && input.nextBtn_2 == 0",
                 radioButtons(inputId ="Q2", ("Q2"),
                              choices = c("A", "B" , "C"), 
                              selected = character(0)),
                 # Q2
                 radioButtons(inputId ="Q3", ("Q3"),
                              choices = c("A", "B"), 
                              selected = character(0)),

                 # Next page button 
                 div(actionButton("nextBtn_2", "Next2 >")))
           )))


# Server
server <- function(input, output) {

  # Observe event 
      observeEvent(input$p1, {js$reset()})

  # The function for adding the page value
  navPage <- function(input, direction) {
    input <- input + direction
   }

  # When nextBtn is pressed, one is added to page number
  observeEvent(input$nextBtn_1, navPage(input$nextBtn_1, 1))# navPage(1)
  observeEvent(input$nextBtn_2, navPage(input$nextBtn_2, 1))# navPage(1)

}

# run 
shinyApp(server = server, ui = ui)

我希望当用户单击选项卡“p1”时,应用程序会刷新。当用户然后导航回“p2”时,他们将从第一个面板开始,并且不会为小部件定义输入。

【问题讨论】:

    标签: r shiny shinyjs


    【解决方案1】:

    这里的问题是input$p1 不存在。要了解单击选项卡的时间以及单击的选项卡,您需要为选项卡容器提供id,并将该 ID 用作输入。此外,您提到您想要重置,但您使用的代码是非常硬重置 - 它刷新页面,实际上相当于按下刷新按钮。我不太明白你想要的逻辑是如何工作的(每次你点击选项卡 2,应用程序都会刷新,然后你又回到选项卡 1 - 这意味着你永远无法进入选项卡 2!),但无论如何这就是它看起来像:

    library(shiny)
    library(shinyjs)
    
    #### Specify the number of pages to use 
    NUM_PAGES <- 2
    
    # Define the js method that resets the page 
    jsResetCode <- "shinyjs.reset = function() {history.go(0)}"  
    
    
    # UI
    ui <- navbarPage(
      id = "mytabs",
    
      # Call shinyjs
      useShinyjs(),
      extendShinyjs(text = jsResetCode, functions = "reset"),
    
      ### Panel 1 ### 
      tabPanel(id = "p1",
               title = "A",
               "Page 1"),
    
      ### Panel 2 ### 
      tabPanel(id = "p2",
               title = "B",
               div(id = "form",
    
                   ### Sub-panel 1
                   conditionalPanel(
                     condition = "input.nextBtn_1 == 0",
                     radioButtons(inputId ="Q1", ("Q1"), choices = c("A", "B"), 
                                  selected = character(0)),
    
                     # Next page button 
                     div(actionButton("nextBtn_1", "Next1 >"))),
    
                   ### Sub-panel 1
                   conditionalPanel(
                     # Q1
                     condition = "input.nextBtn_1 == 1 && input.nextBtn_2 == 0",
                     radioButtons(inputId ="Q2", ("Q2"),
                                  choices = c("A", "B" , "C"), 
                                  selected = character(0)),
                     # Q2
                     radioButtons(inputId ="Q3", ("Q3"),
                                  choices = c("A", "B"), 
                                  selected = character(0)),
    
                     # Next page button 
                     div(actionButton("nextBtn_2", "Next2 >")))
               )))
    
    
    # Server
    server <- function(input, output) {
    
      # Observe event 
      observeEvent(input$mytabs, {
        if (input$mytabs == "A") {
          print("tab 1 clicked")
        }
        if (input$mytabs == "B") {
          print("refreshing page")
          js$reset()
        }
      })
    
      # The function for adding the page value
      navPage <- function(input, direction) {
        input <- input + direction
      }
    
      # When nextBtn is pressed, one is added to page number
      observeEvent(input$nextBtn_1, navPage(input$nextBtn_1, 1))# navPage(1)
      observeEvent(input$nextBtn_2, navPage(input$nextBtn_2, 1))# navPage(1)
    
    }
    
    # run 
    shinyApp(server = server, ui = ui)
    

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 2018-08-03
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      相关资源
      最近更新 更多