Display KaTeX within a modal in R Shiny without the need for an additional click inside the modal

I've been having trouble getting the KaTeX formulas to render properly inside my popups. The Javascript function I'm using is able to do this, but only after a click is made within the popup that's generated.

Unfortunately, I have no background in js and haven't been able to find the right argument to render everything without requiring a click somewhere.

Here is the RME for reference:

renderKaTeX <- '
$(document).ready(function() {
        $("body").on("click", function() {
          var mathElements = document.getElementsByClassName("math");
          for(var i = 0; i < mathElements.length; i++) {
            renderMathInElement(
              mathElements[i], {
                delimiters: [{left: "$", right: "$", display: false}]
              });
          }
        });
 });
'

library(shiny)
library(shinyMobile)

shinyApp(
  ui = f7Page(
    tags$head(
      tags$link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b45394d4e57415c5d54565b49561c5556">[email protected]</a>/dist/katex.min.css", integrity="sha384-657CzJX/dcQUoT8vEr75SlgnmUjFZWEINeh2MtN+bYrpS9,NMblcaNXnIVOkIxA4, cryptography:"anonymous"),
      tags$script(defer = "", src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="366d77636d647a637473747873695660417177476748475460bc2352fa29f03b9"> katex.min.js",integrity="sha384-nFMiirEVlGjkfsLrwQnjYYtoIdI+VliD45aPNWCFnhgIkHrJjSGIEAyzYWvASuQh",crossorigin="anonymous"),
      tags$script(defer = "",src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="62555c45555752474651464f05504c51545140454706595193"> auto-render.min.js",integrity="sha384-JVVqOEsCSilsj3KRwcBWOhRisxLOsCoZHDAncgglIAUFaruuComphumPRHckHzDk contrib:",crossorigin="anonymous"),
      tags$script(HTML(renderKaTeX))
    ),
    title = "Popup",
    f7SingleLayout(
      navbar = f7Navbar(title = ""),
      f7Button("btn", "Open Popup")
    )
  ),
  server = function(input, output, session) {
    observeEvent(input$btn, {
      f7Popup(
        id = "popup1",
        title = "test",
        tags$div(class = "math",
                 tags$p("please render me without needing a click: $X$")         
        )
      )
    })
  }
)

I believe it may just be a missing argument, but I've been struggling with this issue for days now.

Thank you for any assistance you can provide

Answer №1

If you're looking to incorporate mathematical notation in your Shiny app, consider using the katex package:

library(katex)
library(shiny)
library(shinyMobile)

shinyApp(
  ui = f7Page(
    tags$head(
      tags$link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f69d9782938eb6c6d8c7c6d8c7">[email protected]</a>/dist/katex.min.css", integrity="sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ",crossorigin="anonymous"),
      tags$script(defer = "", src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c070d1809142c5c425d5c425d">[email protected]</a>/dist/katex.min.js",integrity="sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij",crossorigin="anonymous"),
      tags$script(defer = "",src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83e8e2f7e6fbc3b3adb2b3adb2">[email protected]</a>/dist/contrib/auto-render.min.js",integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI",crossorigin="anonymous")
    ),
    title = "Simple Popup Example",
    f7SingleLayout(
      navbar = f7Navbar(title = ""),
      f7Button("btn", "Open Popup")
    )
  ),
  server = function(input, output, session) {
    observeEvent(input$btn, {
      f7Popup(
        id = "popup1",
        title = "Mathematical Expression",
        tags$div(
          class = "math",
          tags$p(HTML(
            katex_html("\\frac{1}{2} + \\frac{3}{4} = ?", displayMode = FALSE, preview = FALSE)
          ))         
        )
      )
    })
  }
)

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

Find any consecutive lowercase or uppercase letter and include one more

I have a task in Javascript that I need help with. The goal is to insert a special character between a lowercase and uppercase letter when they are matched together. For example: myHouse => my_House aRandomString => a_Random_String And so on... T ...

Merging columns in a dataframe using R

I'm a beginner in R and I need help combining columns (x-axis) of a dataframe to get the sum in a single column. Tourism Weekdays Weekends Profit 100 20 Visitors 250 75 How can I merge Weekdays and Weeken ...

Having trouble with the parent folder functionality in JavaScript?

I am facing a challenge with my website's structure as I have an old setup that needs to be updated. http://localhost/enc/pdfs/ : This directory contains some html files that are uploaded via ajax to be displayed on a tabbed div using: var Tabs ...

IE9 encounters an error when using jQuery's .remove() function

While testing my website in IE9 using Firebug Lite, I encountered an issue. When attempting to run a basic command to remove a div, I received an error saying "TypeError: Object expected". The command I used was: $("#drag-hoverbox_you").remove(); Interes ...

I'm feeling a bit lost on where to go next with this JavaScript

I've been working on a project in Python where I need to retrieve a file, store it in an array, and display a random string from the array on HTML. However, I have no experience with JavaScript and am unsure how to proceed. I suspect there may be an i ...

Is there a way to prevent the context menu from appearing when tapping on an image on a mobile phone?

When attempting to move an image on the screen with a touch event, a popup appears in Chrome when pressing and holding the image. This popup usually includes options to save the image. Is there a way to disable this popup so I can freely move the image w ...

I am looking to continuously update a div element on my website with data sourced from another site. My goal is to enable the div element to refresh automatically without

I am looking to continuously update the getPrice($url) function every 1 second without the need for manual page refresh. <?php ini_set('display_errors', '1'); Currently, the getPrice($url) function only refreshes when I manual ...

Errors Arise when Using jQuery UI Dialog with Greasemonkey

Encountering a peculiar error whenever attempting to create a dialog using Greasemonkey... I suspect it may be related to the constraints of XPCNativeWrapper, though I can't be certain. Thus far, none of the fundamental jQuery functions I've uti ...

What is the process for adding a Contact Us page to a website?

I recently created a website using html, css and Javascript. I would like to include a 'get in touch' page where visitors can send me messages directly on the site. What steps should I take to make this happen? Is it necessary to set up a databas ...

Sending Grunt Jade configurations to a specific file

I'm currently using grunt-contrib-jade and I'm trying to utilize the pkg.name and pkg.version variables to generate my css file name. Unfortunately, I haven't been able to make this work on my own and would appreciate any assistance. Below i ...

Is it possible to implement Angular Universal on Github Pages?

Is it doable to Deploy Angular Universal on Github Pages? I've come across some solutions such as angular-cli-ghpages, but from what I understand, these options don't pre-render content for SEO purposes. ...

Learn how to call class methods using jQuery events by utilizing the on() method

I'm attempting to create a reusable "component" using both HTML5 and accompanying JavaScript code that can be utilized multiple times. First, let's examine the HTML5 component code: <div class="listEditor" id="myStringList"> <div id="li ...

Ways to incorporate an increment indicator into a JavaScript slider

I am currently using the Angular-Slider created by Prajwalkman, which you can find at THIS LINK. Nevertheless, I am in need of adding indicators with specific values. To illustrate this, I have prepared a proof of concept that can be viewed below. Ideally ...

ng-show and ng-hide toggling for the active row

I have a single table where I am implementing row hide and show functionality using Angular for editing and saving purposes. So far, everything works as expected. $scope.init=function(){ $scope.editable=true; } Now, when I click on the edit button ...

Retrieving information for a specific table row and column

Currently, I am working on fetching data from an API. I have been attempting to retrieve data using an API and have referred to this website: https://www.geeksforgeeks.org/how-to-use-the-javascript-fetch-api-to-get-data/. The data is successfully console. ...

TypeScript compiler encountering issue with locating immutable.js Map iterator within for of loop

I am currently facing a challenge with using immutable.js alongside TypeScript. The issue lies in convincing the TypeScript compiler that a Map has an iterator, even though the code runs smoothly in ES6. I am perplexed as to why it does not function correc ...

I've come across certain challenges when setting values for Vue data objects

I'm having trouble with a Vue assignment. Here is my code: new Vue({ el: "#alarmEchartBar", data: { regusterUrl: Ohttp + "historicalAlarmRecord/chart", regDistrictUrl: Ohttp + "district", regStreetUrl: Ohttp + "street/", regCameraUrl: ...

Discovering a group of slow-moving vehicles in GPS tracking information

I've been experimenting with filtering a GPS tracking dataset based on speed and time of day. The species I'm studying becomes inactive around dusk, resting on the ocean's surface until nightfall when it resumes activity. I want to remove da ...

"Arranging elements in a stack using Bootstrap's collapse

Currently, I am utilizing the collapse feature of Bootstrap 4.0.0 to show and hide a bootstrap "row". Within this row are four columns - "col-md-3" - containing buttons. The layout of this row is exactly as I desire until I include the "collapse" class, at ...

Converting an HTML table into an Excel spreadsheet

In the process of developing an application that populates a table based on a JSON dataset, I am seeking a way to store the filtered data into an Excel file or even a CSV. The structure includes two script files - app.js and mainController.js (organized fo ...