Home > OS >  How to source a locally stored image for embedding into a table cell in R Shiny?
How to source a locally stored image for embedding into a table cell in R Shiny?

Time:10-19

The below code does a terrific job of rendering a web-sourced image in a cell of the rhandsontable. However, I'd like swap that image with a jpg image I have stored on my computer. I've tried modifying the below as.character(img(src = "...")) to reflect the local directory and filename, with no luck.

Any suggestions for a straightforward way to do this?

I searched for solutions, for example, enter image description here

Code:

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

DF = data.frame(
  Col_1 = c("Row 1"), 
  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;")
      )
  ),
  text = c("Row 1 does xxx"),
  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)

CodePudding user response:

Put your file, say question_mark.jpg in the www folder of your shiny app, and then adjust your DF definition as below:

DF = data.frame(
  Col_1 = c("Row 1"), 
  Col_Help = c(
    as.character(img(
      src = "question_mark.jpg", 
      title = "My first help text", 
      style = "width: 50px;")
      )
  ),
  text = c("Row 1 does xxx"),
  stringsAsFactors = FALSE
)

Output:

shiny_app_running_with_pic_in_cell

  • Related