What is the process for creating a pop-up bubble that appears when the cursor hovers over an image displayed within a table using R Shiny?

I am currently working on code that is almost achieving my desired functionality. I want to make it so that hovering over each question mark in the table will trigger a pop-up bubble displaying the help text, rather than having the text appear at the bottom of the screen and needing the user to click "close". Once the cursor moves away from the question mark, the pop-up bubble should disappear, as depicted in the image.

Does anyone have any suggestions on how I can accomplish this?

I am attempting to improve my understanding of JavaScript and CSS by studying at W3Schools, but progress has been slow.

https://i.sstatic.net/I58PG.png

Here is the code I am currently working with:

library(magrittr)
library(htmlwidgets)
library(rhandsontable)
library(shiny)

DF = data.frame(
  Col_1 = c("This is row 1","This is row 2"), 
  Col_Help = c(
    "https://as1.ftcdn.net/v2/jpg/03/35/13/14/1000_F_335131435_DrHIQjlOKlu3GCXtpFkIG1v0cGgM9vJC.jpg",
    "https://as1.ftcdn.net/v2/jpg/03/35/13/14/1000_F_335131435_DrHIQjlOKlu3GCXtpFkIG1v0cGgM9vJC.jpg"
  ),
  text = c("Row 1 does xxx","Row 2 does yyy"),
  stringsAsFactors = FALSE
)

ui <- fluidPage(br(),rHandsontableOutput('my_table'))

server <- function(input, output, session) {
  output$my_table <- renderRHandsontable({
    rhandsontable::rhandsontable(
      DF,
      allowedTags = "<em><b><strong><a><big>"
      ) %>%
        hot_cols(colWidths = c(200, 80)) %>%
        hot_col(1, renderer = htmlwidgets::JS("safeHtmlRenderer")) %>%
        hot_col(2, renderer = "
          function(instance, td, row, col, prop, value, cellProperties) {
            var escaped = Handsontable.helper.stringify(value),
              img;
        
            if (escaped.indexOf('http') === 0) {
              img = document.createElement('IMG');
              img.src = value; img.style.width = 'auto'; img.style.height = '20px';
        
              Handsontable.dom.addEvent(img, 'mousedown', function (e){
                var exists = document.getElementById('test')
                if (exists === null){
                  var textBlock = instance.params.data[[row]][[2]];
                  var popup = document.createElement('div');
                  popup.className = 'popup';
                  popup.id = 'test';
                  var cancel = document.createElement('div');
                  cancel.className = 'cancel';
                  cancel.innerHTML = '<center><b>close</b></center>';
                  cancel.onclick = function(e) {
                    popup.parentNode.removeChild(popup)
                  }
                  var message = document.createElement('span');
                  message.innerHTML = '<center>' + textBlock + '</center>';
                  popup.appendChild(message);
                  popup.appendChild(cancel);
                  document.body.appendChild(popup);
                }
              });
        
              Handsontable.dom.empty(td);
              td.appendChild(img);
              
            }
            else {
              // render as text
              Handsontable.renderers.TextRenderer.apply(this, arguments);
            }
            return td;
          }") %>% 
      hot_cols(colWidths = ifelse(names(DF) != "text", 100, 0.1))
  })
}

shinyApp(ui, server)

Answer №1

Is there a way to enable the display of img tags with customizable title attributes?

library(magrittr)
library(htmlwidgets)
library(rhandsontable)
library(shiny)

DF = data.frame(
  Col_1 = c("This is row 1","This is row 2"), 
  Col_Help = c(
    as.character(img(src = "https://images.plot.ly/language-icons/api-home/python-logo.png", title = "My first help text", style = "width: 50px;")),
    as.character(img(src = "https://images.plot.ly/language-icons/api-home/r-logo.png", title = "My second help text", style = "width: 50px;"))
  ),
  text = c("Row 1 does xxx","Row 2 does yyy"),
  stringsAsFactors = FALSE
)

ui <- fluidPage(br(),rHandsontableOutput('my_table'))

server <- function(input, output, session) {
  output$my_table <- renderRHandsontable({
    rhandsontable::rhandsontable(
      DF,
      allowedTags = "<em><b><strong><a><big><img>"
    ) %>%
      hot_cols(colWidths = c(200, 80)) %>%
      hot_col(1:2, renderer = htmlwidgets::JS("safeHtmlRenderer")) %>%
      hot_cols(colWidths = ifelse(names(DF) != "text", 100, 0.1))
  })
}

shinyApp(ui, server)

https://i.sstatic.net/45wxH.png

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

Navigating to the following webpage with Selenium using Python

url_main = "https://comic.naver.com/webtoon/detail.nhn?titleId=131385&no=292" This particular website features a comment pagination at the bottom of the page. Upon inspecting the page, I noticed that the buttons were structured like this: &l ...

Timeout error of 10000ms occurred while using await with Promise.all in Mocha unit tests

Document: index.ts // Default Exported Classes getItemsA() { return Promise.resolve({ // Simulating API call. Mocking for now. success: true, result: [{ itemA: [] }] }); } getItemsB() { return Promise.resolve({ // Simulating API cal ...

Having trouble extracting information from JSON object array following an AJAX post?

My website transfers data through JSON objects using Angular's $http post. If you'd like to see the console logs and responses, visit my website: Initially, I used x-form-urlencoded encoding successfully and decided to switch to application/jso ...

Exploring alternatives to setTimeOut() for precise timing of Javascript events, especially when incorporating third party events

Here is the HTML element stack that I am working with: <div class="btn-group btnSortAssType" data-toggle="buttons"> <label class="btn ink-reaction btn-primary active"> <input type="checkbox" value="m">Model </label> ...

What is the best way to make this eerie javascript script communicate with my webpage or another jquery file?

I've been struggling with a javascript code that enables infinite scrolling in Tumblr. It seems outdated compared to jQuery, which I'm more familiar with. I've tried integrating this script with my jQuery functions and identifying DOM eleme ...

Tips to eliminate rows from a data frame if the values in a single column do not display consecutive increments

I am working with a data frame in R and looking to eliminate rows that do not show consecutive increases in column B. In other words, the value in each row must be greater than the previous one but less than the next one. Sorting the data frame based on c ...

Utilize drag and drop functionality to interact with an HTML object element

I am struggling with a div that contains a PDF object and draggable text: <html> <head> <script> function allowDrop(ev) { ev.preventDefault(); } function drop(ev) { alert("DROP"); } </script> </head> <body> <di ...

Using JQuery Mobile to Incorporate Links into List Elements

Creating a mobile webpage tailored for college students involves assigning each student a unique id and an additional field. Both fields can be treated as strings, with the unique id being guaranteed to be unique, while the second field may or may not be u ...

Discovering the method for accessing a variable within jQuery from a regular JavaScript function

As someone new to jQuery, I am currently facing a challenge with accessing a variable defined inside a jQuery block from a regular function. Despite my attempts, I have been unsuccessful in accessing it. Can anyone guide me on how to do this? <script l ...

The image is not appearing on the webpage even though the online URLs are functioning correctly

I am currently facing an issue with my Django portfolio website where I cannot get my photo to display even though the path is correct. However, online images are displaying properly on the site. <html lang="en"><head><link href="https: ...

Child components are not able to access properties from the root component in VUE3

Hi there! I'm currently learning VUE3 and had a question about defining global properties in the Root Component. I did some research on Google before posting this question, but couldn't find any relevant results. I would like to set a global pro ...

The distortion of Blender animation becomes apparent once it is imported into three.js

I've been working on a project where I'm incorporating animations into a scene using a combination of blender and three.js. It took me several hours of trial and error to finally get the model and animation successfully imported into three.js. I ...

Function to save prices as cent-based figures in Javascript using Regex

Trying to extract prices from a string using regex can be tricky, as unexpected issues may arise. For example, obtaining the following values: US$1234.56 $12 $12.34usd $0.56 .56 dollars and converting them to: 123456 1200 1234 56 56 is necessary for s ...

A function with a specific name for sorting a multidimensional object based on a specified sub-key's value, without anonymity

My data consists of a collection of objects, not an array: var people = {}; people['Zanny'] = {date: 447, last: 'Smith'}; people['Nancy'] = {date: 947, last: 'William'}; people['Jen'] = {date: 147, last: &a ...

Obtain sequence fragments from a FASTA file by utilizing coordinates specified in a GRanges object

I am looking to extract the intergenic sequences of Bacillus Subtilis using R. After importing the full DNA sequence of B.subtilis in R with seqinr, I used the read.fasta function from the seqinr package to import the dna sequence. The next step was crea ...

How to use jQuery to change the background color of HTML table cells based on their content value

A dynamic table will be displayed based on the dropdown element selected. Below is a snippet of the code: <table id="myPlaneTable" class="table table-bordered"> <tr> <td style="width: 20%">Max speed</td> <td ...

The selection feature is not supported for the type of input element in jQuery

I'm currently attempting to utilize AJAX to send a form with jQuery. However, upon clicking the submit button (which is technically a link), I encountered an error. The error displayed in the console is as follows: Failed to read the 'selection ...

Troubleshooting problem with JSON decoding in PHP and json_encode

Encountering an issue when parsing JSON received from a PHP backend. In my PHP code, I have an array that I send using json_encode: $result[] = (object) array('src' => "{$mergedFile}", 'thumb_src' => "{$thumb_file}"); echo json_e ...

Ensure the header remains fixed when scrolling in an HTML page with Bootstrap

I created the following code. However, when I scroll down the table, the header disappears from view. I would like the header to always remain at the top, even when scrolling. Despite searching Google multiple times and trying various solutions, I have no ...

In Laravel Blade, you can dynamically add rows and columns based on a certain condition

I'm working on creating a user interface for cinema seats in Laravel Blade. The database includes seat rows and columns, and the rows will be the same for two consecutive rows. For example, the first row could have an id of 1 with seat_row:1 and seat_ ...