R Shiny - 导航到特定的 navbarPage 选项卡时刷新应用程序会话(shinyjs.reset)
R Shiny - Refresh app session (shinyjs.reset) when navigating to a specific navbarPage tab
我的应用程序通过一系列面板前进,用户可以通过单击操作按钮前进到下一个面板。我希望在导航到另一个选项卡时刷新整个应用程序。这意味着用户将从一系列面板的第一个开始,并且所有小部件输入都将被重置。
我已经为面板提供了 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" 时,他们将从第一个面板开始,并且不会为小部件定义任何输入。
这里的问题是 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)
我的应用程序通过一系列面板前进,用户可以通过单击操作按钮前进到下一个面板。我希望在导航到另一个选项卡时刷新整个应用程序。这意味着用户将从一系列面板的第一个开始,并且所有小部件输入都将被重置。
我已经为面板提供了 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" 时,他们将从第一个面板开始,并且不会为小部件定义任何输入。
这里的问题是 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)