Certain cli styles create clickable links, if your IDE or terminal supports them.

URLs

There are two cli styles to link to generic URLs. .url does not allow custom link text, but \href does.

cli_text(
  "See the cli homepage at {.url https://cli.r-lib.org} for details."
)

#> See the cli homepage at <https://cli.r-lib.org> for details.

cli_text(
  "See the {.href [cli homepage](https://cli.r-lib.org)} for details."
)

#> See the cli homepage for details.

This is how these links look without hyperlink support:

local({
  withr::local_options(cli.hyperlink = FALSE)
  cli_text(
    "See the cli homepage at {.url https://cli.r-lib.org} for details."
  )
  cli_text(
    "See the {.href [cli homepage](https://cli.r-lib.org)} for details."
  )
})

#> See the cli homepage at <https://cli.r-lib.org> for details.
#> See the cli homepage (<https://cli.r-lib.org>) for details.

URL encoding

Note that cli does not encode the url, so you might need to call utils::URLencode() on it, especially, if it is substituted in via {}.

weirdurl <- utils::URLencode("https://example.com/has some spaces")
cli_text("See more at {.url {weirdurl}}.")

Files

The .file style now automatically creates a file: hyperlink. Because file: hyperlinks must contain an absolute path, cli tries to convert relative paths, and paths starting with ~ to aboslute path.

cli_text("... edit your {.file ~/.Rprofile} file.}")

#> ... edit your ~/.Rprofile file.}

.file cannot use a custom link text. If you custom link text, then you can use .href with a file: URL.

prof <- path.expand("~/.Rprofile")
cli_text("... edit your {.href [R profile](file://{prof})}.")

#> ... edit your R profile.

Line and column numbers

You may add a line number to a file name, separated by :. Handlers typically place the cursor at that line after opening the file. You may also add a column number, after the line number, separated by another :.

cli_text("... see line 5 in {.file ~/.Rprofile:5}.")

#> ... see line 5 in ~/.Rprofile:5.

Default handler

In RStudio file: URLs open within RStudio. If you click on a file link outside of RStudio, typically the operating system is consulted for the application to open it.

One issue with using .href file files is that it does not look great if hyperlinks are not available. This will be improved in the future:

local({
  withr::local_options(cli.hyperlink = FALSE)
  prof <- path.expand("~/.Rprofile")
  cli_text("... edit your {.href [R profile](file://{prof})}.")
})

#> ... edit your R profile (<file:///Users/gaborcsardi/.Rprofile>).

Click to run code

RStudio also supports a special link type that runs R code in the current R session upon clicking.

You can create these links with .run:

cli::cli_text("Run {.run testthat::snapshot_review()} to review")

#> Run testthat::snapshot_review() to review

Sometimes you want to show a slightly different expression in the link, than the one that is evaluated. E.g. the evaluated expression probably needs to qualify packages with ::, but you might not want to show this:

cli::cli_text(
  "Run {.run [snapshot_review()](testthat::snapshot_review())} to review"
)

#> Run snapshot_review() to review

Security considerations

To make .run hyperlinks more secure, RStudio will not run code

  • that is not in the pkg::fun(args) form,

  • if args contains (, ) or ;,

  • if it calls a core package (base, stats, etc.),

  • if it calls a package that is not loaded, and it is not one of testthat, devtools, usethis, rlang, pkgload, or pkgdown which are explicitly allowed.

When RStudio does not run a .run hyperlink, then it shows the code and the user can copy and paste it to the console, if they consider it safe to run.

Note that depending on your version of RStudio, the behavior can change.