Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference an image from JS file in packaged Shiny app?

I am creating an R package which provides some helper functions to build a Shiny app.

One of the JS files packaged with the app needs to reference an image file. But I can't figure out how to reference it.

Currently, the js file is located in

my_package/inst/www/js/my_jsfile.js

This file needs to reference

my_package/inst/www/img/my_img.gif

In the JS file, what should the relative URL to the image be?

I tried various options, such as the following, which do not work, when launching in a Shiny app built with the package:

../my_img.gif

www/img/my_img.gif

My JS looks like this:

function showRecordingIcon() {

  var img = document.createElement("img");
  img.style.display = "block";
  img.src =  "img/record.gif";
  img.width = "280";
  img.height = "280";
}

This was working before I packaged it.

like image 429
eartoolbox Avatar asked Jun 29 '21 13:06

eartoolbox


Video Answer


2 Answers

If your goal is to get file path from the inst folder in your package on the user system, try:

system.file('www/js/my_file.js', package = 'NAMEOFYOURPACKAGE', mustWork = T)
like image 172
SmokeyShakers Avatar answered Oct 31 '22 15:10

SmokeyShakers


Did you use shiny::addResourcePath("img", "/path/to/img/folder")

Something like shiny::addResourcePath("img", system.file('www', 'img', package = 'NAMEOFYOURPACKAGE', mustWork = T)

Next, you can refer to it as "img/record.gif"

like image 40
user13818093 Avatar answered Oct 31 '22 14:10

user13818093