1

I want to load an image into a .rmd file.

I am working on a university project where I have to hand in this .rmd file at the end. As a restriction, this file has to run out of the box, so unfortunately it is not an option to load the image from a given file path as I can't submit a folder containing the image or similar (and I don't want to upload it somewhere and access it via URL either).

I was looking for a way to serialize the image information and hard code it into the file but I couldn't find anything helpful related to that. So in short, I want to do the following:

  1. serialize image
  2. hard code serialized image as variable in .rmd
  3. deserialize hard coded variable and plot image data in .rmd

Is there a simple way to do this?

Lavair
  • 888
  • 10
  • 21
  • Does this https://stackoverflow.com/a/25454753/5784831 help? – Christoph Jan 22 '21 at 12:51
  • @Christoph unforunately not. In this post, images are also loaded from external files. I don't want to do that. I wanted to copy the plain image information into a variable so my .rmd file could run without any external files. But I guess this is not possible. – Lavair Jan 22 '21 at 18:21

1 Answers1

0

This seems to be unfeasible. I've tried:

image <- load.image(path)
serialized_image <- serialize(image, NULL)
some_file <- file("test", "wb")
write(serialized_image, "test")

and then I wanted to open test and copy the content manually with CTRL+C, CTRL+V into a variable in my .rmd but it turned out that the content is way too large for doing that (around 30 million lines of bytes..), so theoretically this would be a solution but unless you want a 30 million lines .rmd, this is not an option.

Lavair
  • 888
  • 10
  • 21