Package works locally but fails when installed outside of the package project in RStudio

Hi,

I am creating a package that contains one function, which randomly samples an image (meme) and displays it on the RStudio Viewer pane (see readme file on the github repo).

Two questions:

  1. The package works locally but fails once it’s installed. It displays the error on the screenshot attached, which to my understanding could imply that the function is not sourcing the images correctly. Please help me out here.

  2. I have a list of 145 images totalling to 52 mb (the package has like 10 for now). I noticed that the package is taking too long to install with the 145 images. Is there a work around on this? Or do I just have to work with a small number of images? Or is the package taking too long to install because of a different issue?

1 Like

:wave: @shel!

For accessing the images of an installed package, well anything that’s in inst/, you need to use system.file() e.g. this line

pichas <- list.files('inst/extdata/')

should bee.g.

pichas <- dir(system.file("extdata", package = "rKenyaMemes"), full.names = TRUE)

where the full.names argument ensures you won’t need the paste0() for this other line as the path will be already complete.

meme_moja <- image_read(picha)

Using system.file() will ensure that the images can be found when the package is installed… but also when it is loaded with devtools because devtools uses pkgload for loading, that has “shimmed” version of system.file() cf Load complete package — load_all • pkgload (I had forgotten the word “shim” until right now :grin:).

Just a small note, next time would you mind copy-pasting the code instead of a screenshot? Thanks :pray:

1 Like

Thanks @maelle . The issue here is the images are being used to create the rKenyaMemes package. The entertain_me() function should source the files, randomly pick one and display it on the Viewer pane. So every time someone runs entertain_me(), a different image should be displayed.

Noted on the code-sharing part. I will avoid screenshots. Thanks

1 Like

Yes with these two changes, things will work both when the package is loaded or installed.

I made a PR and tested the change locally and was entertained :wink: https://github.com/Shelmith-Kariuki/rKenyaMemes/pull/1

1 Like

Wow!!! It works… :cry: Thank you for your assistance … and I am glad that you were entertained. A majority of the memes are in our local language but a few are in english. Do you know how I would go about the second question?

Oh right I forgot that! I had tunnel vision!

I have a list of 145 images totalling to 52 mb (the package has like 10 for now). I noticed that the package is taking too long to install with the 145 images. Is there a work around on this? Or do I just have to work with a small number of images? Or is the package taking too long to install because of a different issue?

I am not sure whether the size of the package influences its installation time but it might take longer to download it for sure. Maybe, if it’s ok for the package to only work when there’s an internet connection, it could rely on hosting the images somewhere else and either download one / try to show the remote image in the viewer pane (however that second idea didn’t work for me locally, I had a quick look at https://support.rstudio.com/hc/en-us/articles/202133558-Extending-RStudio-with-the-Viewer-Pane). If the images are hosted somewhere else the challenge will be to make sure the image list in the package is up-to-date with what’s hosted somewhere else.

Hosted somewhere else might be a Netlify website for instance. By the way I have no idea what legal implications there are to re-sharing memes. Is there an API for Kenyan memes or do you have to build it? :grin:

Similar packages for inspiration

  • meow fetches random cat images from an API.
  • kittyR does the same but from several websites.

Good luck!

Thank you for the information. I do not think that are any legal implications with meme sharing since they are freely shared online and circulate amongst us freely, and given that I am not earning anything from this package, I think I am safe…but let’s wait and see if any issue arises.
I am currently installing the package with the 145 images to see the download time…then I’ll know how to move on from there. Thank you for making my Friday amazing. :heart_eyes:

1 Like

The package is taking about 11 minutes to install on my laptop so yeah, I will need to look for a way to move the memes to an API and call them from there. Thanks again Maelle.

1 Like

I’m guessing you don’t need a full API, just a static website with all the images (and somewhere else a list of all the URL / using a sitemap as list of URLs) but just in case, as your website is Hugo, I’m posting this link: Build a JSON API With Hugo's Custom Output Formats | Forestry.io

1 Like

Cool … I will look at it this weekend.