Comments (4)
@andbamp This is the behavior that happens when you use observers inside of the page. This is because shiny doesn't destroy observers when the server code is called for the n-th time but registers them again (please check my blogpost that describes the issue: https://appsilon.com/how-to-safely-remove-a-dynamic-shiny-module/).
Unfortunately in shiny.router, the main functionality is based on rerendering the page server code. We've got a few ideas how to implement this properly but this is of a low priority now, but I'll let you know when it's done.
In the meantime, you may try to force destroying observers (following instructions from the above blogpost, my solution in #69 might also help) everytime the page is switched. Please let me know if you need any help with it.
from shiny.router.
@andbamp I decided to give my idea a try, and the issue should be fixed now. Please install the package from https://github.com/Appsilon/shiny.router/tree/krystian.stop-rerendering-whole-page branch and let me know if everything works correctly now.
from shiny.router.
I didn't realise shiny.router's functionality was based on rerunning the server code. This fix not only solves the issue of console output being printed multiple times (and cases such as dialogs instantiated by shinyjs appearing multiple times), but it also negates the need to set ignoreInit
to TRUE
to accommodate page switching. More importantly, it should also help with performance on certain applications with expensive computations within reactives.
I tested the new branch in a few scenarios and it seems to work well, but one side-effect I noticed is that the UI elements defined in shiny_ui
are always rendered last. A modification of the previous example showcases the issue:
library(shiny)
library(shiny.router)
# This generates menu in user interface with links.
menu <- (
tags$ul(
tags$li(a(class = "item", href = route_link("home"), "Home page")),
tags$li(a(class = "item", href = route_link("side"), "Side page"))
)
)
# This creates UI for each page.
page <- function(title, content, button_name) {
div(
menu,
titlePanel(title),
p(content),
actionButton("switch_page", "Click to switch page!"),
actionButton(button_name, "Go!")
)
}
# Both sample pages.
home_page <- page("Home page", uiOutput("current_page"), "test1")
side_page <- page("Side page", uiOutput("current_page"), "test2")
home_server <- function(input, output, session) {
observeEvent(input$test1, {
print("Foo")
})
}
side_server <- function(input, output, session) {
observeEvent(input$test2, {
print("Bar")
})
}
# Creates router. We provide routing path, a UI as
# well as a server-side callback for each page.
router <- make_router(
route("home", ui = home_page, server = home_server),
route("side", ui = side_page, server = side_server)
)
# Create output for our router in main UI of Shiny app.
ui <- shinyUI(fluidPage(
actionButton("before_router", "Before router UI"),
router_ui(),
actionButton("after_router", "After router UI")
))
# Plug router into Shiny server.
server <- shinyServer(function(input, output, session) {
router(input, output, session)
output$current_page <- renderText({
page <- get_page(session)
sprintf("Welcome on %s page!", page)
})
observeEvent(input$switch_page, {
if (is_page("home")) {
change_page("side")
} else if (is_page("side")) {
change_page("home")
}
})
})
# Run server in a standard way.
shinyApp(ui, server)
Thanks for addressing the issue so fast @krystian8207! Being able to use shiny.router without modifications to the main application code (be it ignoreInit
or assigning observers to variables in order to destroy them) would make the package incredibly versatile.
from shiny.router.
@andbamp Thank you for noticing that. The current branch version should fix this behavior as well.
from shiny.router.
Related Issues (20)
- New documentation page for shiny.router
- New CI for shiny router
- New labels for shiny.router
- [Bug]: shiny.router does not work when deploying a shiny app in docker HOT 1
- [Feature]: Only load the html for the route HOT 2
- [Bug]: 404 page does not work when a user opens a non-valid link without going to a valid one first
- [Feature]: Examples need for ui.R and server.R app design
- Building a markdown-based survey platform in {shiny}
- semantic tabset not working with router HOT 4
- Feature request: support dynamic population of `make_router()`
- Using shiny.router with separate ui and server files HOT 1
- Using shiny.router to receive POST requests on specific routes HOT 1
- shiny.router not working on RSConnect server HOT 2
- Internal linking?
- multiple_radio position inline not working HOT 1
- semantic_DT: selected rows are not highlghted HOT 1
- Callback behavior changed with 0.2.2
- Question: How to use shiny.router with moduleServer? HOT 6
- [Bug]: Error: 'router_ui' is not an exported object from 'namespace:shiny.router' HOT 2
- disable_bootstrap_on_bookmark() errors out with development version of Shiny HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shiny.router.