R闪亮-导航到特定的navbarPage标签时刷新应用会话(shinyjs.reset)

时间:2019-01-25 16:32:43

标签: r shiny shinyjs

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

我为面板提供了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”时,他们将在第一个面板上启动,并且不会为小部件定义任何输入。

1 个答案:

答案 0 :(得分:0)

这里的问题是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)