Skip to contents

Generate a HTML link as <a></a> or in the form of image as <a><img /></a>.

Usage

create_a_link(
  href,
  text,
  href_rel_start = NULL,
  target = "_blank",
  do_cat = FALSE,
  ...
)

create_img_link(
  href,
  img_src,
  href_rel_start = NULL,
  img_src_rel_start = NULL,
  img_width = "250px",
  img_height = "",
  target = "_blank",
  do_cat = FALSE,
  ...
)

Arguments

href

A character scalar or vector (for create_img_link()): URL.

text

A character scalar or vector (for create_img_link()): text of the link.

href_rel_start

A character scalar: relative start of href. See the section Relative links.

target

A character scalar: target parameter of <a> tag.

do_cat

A logical scalar: if TRUE, print (using cat()) the result before returning.

...

Named parameters to put in the resulting <a> tag.

img_src

A character scalar: path to image.

img_src_rel_start

A character scalar: same as href_rel_start, but for the image.

img_width, img_height

A character scalar: image size.

Value

A character scalar.

Sometimes URL in the supplied href parameter is absolute, or not relative to the output in which will be displayed. For such cases use the href_rel_start parameter, which allows to make href relative to something.

For example, you are saving a plot to a file relative to working directory, output/plots/plot.pdf, and you want to include link to this file in your RMarkdown output, output/report.html. From the point of view of this HTML file, output/plots/plot.pdf obviously does not exist. However, you can use href_rel_start = "output" to specify that the href is relative to this directory, resulting in a link to plots/plot.pdf. Thanks to fs::path_rel(), which is used underhood, even non-children href can be used, e.g. if HTML is located in output/reports/report.html, than href_rel_start = "output/reports" will result in a link to ../plots/plot.pdf.

Examples

create_a_link("google.com", "Google")
#> [1] "<a href='google.com' target='_blank' >Google</a>"
# If you want to reference a file relative to HTML saved in "output/report.html"
create_a_link("output/plots/plot.pdf", "Link to plot", href_rel_start = "output")
#> [1] "<a href='plots/plot.pdf' target='_blank' >Link to plot</a>"