2

I am new to R and Rmd and trying to generate a report using Rmd. This report has several images inserted along with the text. I am able to insert an image by hardcoding the path of the image. I have no problems with that but I need the path as a variable because it varies with the project. Can anyone help me with the syntax for calling a variable within a path to the image?

![Relatedness check](/data/array_processing/in_progress/Project123/files/data/plots/Project123.ibd.png)

"Project123" changes based on the project. Is there a way I can declare this variable and call it to define the path?

Help please.

2
  • 3
    In Rmd files, one can use inline code (even in image paths), so perhaps ![Relatedness](`r imgpath`) (where imgpath is a variable in R that you defined to be your /data/array.../file.png path/filename). Commented Jul 10, 2020 at 2:57
  • 1
    @r2evans could you submit your answer as an answer so that Suresh can accept it? Commented Jul 10, 2020 at 10:10

1 Answer 1

4

Images can use online R code for dynamic paths and/or alt text. (Early adopters of rmarkdown often tried this method as the default method of including R plots in the reports, using png(filepath...); plot(...); dev.off() followed by what I recommend you use.)

This will allow you to do what you need:

![something meaningful](`r filepath`)

as raw markdown (and not inside a traditional code chunk).

If you aren't familiar with inline code blocks, then know that you can put just about anything in an inline code block. This is handy for including dynamic content in a paragraph of text, for example "the variance of the sample is \r var(sample(99))``". (Often it is just a pre-created variable, if numeric it is often rounded or formated to control the display of significant figures.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for pointing out. I couldn't figure out how to accept yday. Done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.