Skip to content
How to open an R Shiny modal popup from a hyperlink in a datatable
R
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
DataTableShinyModal.R
LICENCE
README.md
modal-thumbnail.PNG

README.md

datatable_shinymodal

Sometimes there are too many rows in a data table to display on one page and scrolling is not the most user-friendly way to solve this. This examples provides a way to layer information into a lightbox modal pop-up which can be accessed by clicking a link in a data table.

You can try out the app here

alt text

How it works

Add an an extra column for the data in your table and attach a Shiny event listener to it to return its index

dat <- dat %>%
  mutate(More = paste0('
  <span style="float:right;">
    <a href="javascript:void(0)" onmousedown="',
             'Shiny.onInputChange(\'DTClick\',[', GID, ',Math.random()]);',
             ' event.preventDefault(); event.stopPropagation(); return false;">
                <font color="grey">&#9679;&#9679;&#9679;&nbsp;&nbsp;</font>
    </a>
  </span>')
)
    

Listen for the click and show a modal

observeEvent(input$DTClick, {
      showModal(modalDialog(
        title = "Sample message",
        div(HTML(paste0("You just clicked row ", as.numeric(input$DTClick[1]))),
            uiOutput("modalContent")),
        easyClose = TRUE,
        footer = NULL
      ))
    })
You can’t perform that action at this time.