Navigating to an HTML anchor programmatically within a shiny application

Is it possible to programmatically send the window to #anchor_box when results are available at the end of an observeEvent() with a long running time, instead of relying on the user clicking on a link like

a("Go to Results", href = '#anchor_box')
?

Answer №1

If you want to achieve this functionality, utilizing JavaScript is the way to go. There are various methods available, but one approach involves acquiring the element and employing scrollIntoView(). This can be seen in action here: anchor jumping by using javascript.

A convenient method of integrating JavaScript within a shiny environment is through library(shinyjs).

To instruct R to shift focus to the desired element, consider implementing the following:

runjs('
      document.getElementById("anchor_box").scrollIntoView();
    ')

You can determine when to trigger this action by wrapping it within an observeEvent():

observeEvent(eventExpr = input$clck, handlerExpr = {...})

One drawback to keep in mind is that refreshing the page won't automatically "scroll up to the top".

Example for Replication:

library(shiny)
library(shinyjs)

ui <- fluidPage(

  a("Navigate to Results", href = '#anchor_box'),
  actionButton("clck", "Move Programmatically!"),

  plotOutput("plot", height = 1300),  
  div(id = "anchor_box", "HERE"),
  useShinyjs(),

)

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

  output$plot <- renderPlot({
    plot(1)
  })

  observeEvent(eventExpr = input$clck, handlerExpr = {
    runjs('
     document.getElementById("anchor_box").scrollIntoView();
    ')
  })

}

shinyApp(ui, server)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Material-UI DataGrid feature allows for the display of column sums, with the sum dynamically updating based on applied filters

I am struggling with calculating the sum of values in the Total Amount column of my Material-UI DataGrid. How can I achieve this and ensure that the sum is also updated when a filter is triggered? Any guidance on how to sum the entire Total Amount column ...

Instructions on removing rows by using buttons within a JavaScript-generated table

This snippet displays JS code to create a quiz index table and HTML code to display the index. function load(){ var data = [ { "id": "qc1111", "quizName": "Quiz1", "course": "111", "dueDate": "1/ ...

The issue with jQuery's .after() method is that it is inserting content as a string instead

Having trouble inserting a button element after another element selected using jQuery. Here's how I have my element selected: let btn = $("a[translate='server.ipmi.kvm.LAUNCH']")[0]; When I try to insert the button after it, I'm using ...

The Material-ui paper component fails to display on the screen

The material-ui paper component is implemented on my homepage and functioning correctly. However, when navigating to another page and returning to the homepage, the paper component disappears, leaving only text rendered. Can you help me identify the issue? ...

When the NODE_ENV is set to 'production', the global error handler middleware fails to log errors

To ensure proper code format, errors are logged and returned to the client using Postman in development mode, but are not sent back to the client in production mode. The following code represents the global error handler: module.exports = (err, req, res, n ...

Receiving a reply from the axios function

Whenever I try to call the lookUpItem function from ItemSearch.vue, I always get an undefined response. Code snippet from ItemSearch.vue: <script setup lang="ts"> import { lookUpItem } from '../systemApi' async fu ...

Tips for concealing images within a designated list and revealing them in a separate list

I have a question regarding image visibility. Is it possible to hide certain images from a "SHOW ALL" list, but have them appear in a different category list? Below is the code snippet: <section> <ul class="portfolio_filters"> < ...

There is an ample amount of blank space located at the bottom of the page

There seems to be an excess of white space at the bottom of my webpage, despite my efforts to adjust margins and padding. I've inspected the elements using browser developer tools but have been unable to pinpoint the issue. var chatResponseBox = do ...

Using RSelenium on the Rstudio server with Firefox

I'm encountering an issue while attempting to connect to Firefox using RSelenium for web scraping. Any assistance would be greatly appreciated. > rD <- rsDriver(browser = "firefox") checking Selenium Server versions: BEGIN: PREDOWNLOAD ...

.npmignore failing to exclude certain files from npm package

I'm facing an issue with a private module on Github that I am adding to my project using npm. Despite having a .npmignore file in the module, the files specified are not being ignored when I install or update it. Here is what my project's packag ...

React Native vector icons display enigmatic symbols

I recently installed react-native-vector, but I'm seeing strange symbols when using it. Can anyone provide guidance on how to properly utilize this library? Platform: Android import React from 'react'; import {View, Text, StyleSheet} from & ...

"What is the best way to retrieve the name of the object passed in a function in

After searching high and low, I still can't seem to find the answer to my simple question. Maybe it doesn't exist, but I won't give up just yet. So here's the scenario: I created a global prototype in Vue, essentially a global class, ...

Nested React Components in React Router Components

class AppRoutes extends Component { render () { return ( <Suspense fallback={<Spinner/>}> <Switch> <Route exact path="/" component={ HomePage } /> <Route exact path="/acti ...

Issue with Three.js GLTF loader peculiar behavior while attempting to incorporate three-dimensional text

I had an idea to import a prebuilt gltf scene created in Blender, then add some 3D text using the ttf loader and manipulate it. Each loader worked perfectly when used separately, but strange things started happening when I combined them into a single scrip ...

The search functionality in the table is experiencing a glitch where it does not work properly when trying to search with a

I created a simple mini-app with a search bar and a table displaying data. Users can enter keywords in the search bar to filter the data in the table using lodash debounce function for smoother performance. Everything works fine except for one issue - when ...

Arranging Angular Array-like Objects

I am in possession of an item: { "200737212": { "style": { "make": { "id": 200001510, "name": "Jeep", "niceName": "jeep" }, "model": { "id": "Jeep_Cherokee", "name": "Cherokee", "nice ...

Repeating X and Y Axis Labels on Highcharts

Highchart is new to me. I recently created a basic chart showing the count of males and females over the past five years. I have included a screenshot for reference. I am wondering if it's possible to remove duplicate labels from both axes? Below is ...

An issue has occurred: Angular2 is reporting that Observable_1.Observable.defer is not recognized as a

Recently, I made the transition of my Angular app from version 4 to 6. Here is a peek at my package details: Package.json file: { "version": "0.10.50", "license": "MIT", "angular-cli": {}, ... Upon running npm run start, an error popped up in th ...

Easy Registration Page using HTML, CSS, and JavaScript

In the process of creating a basic login form using HTML and CSS, I'm incorporating Javascript to handle empty field validations. To view my current progress, you can find my code on jsfiddle My goal is to implement validation for empty text fields ...

Tips for properly sending a JWT token

When working on my angular application, I encountered an issue while trying to send a jwt token as a header for request authorization. The error 500 was triggered due to the jwt token being sent in an incorrect format. Here is how I am currently sending it ...