Create web file paths that can interpret "../"

I though about using fs::path_norm() -- which does interpret "../", but it eliminates the second / in http://.

base_url <- "https://<domain>.com/first/second"
ref <- "../file.ext"

p <- path(base_url, ref)
# https:/<domain>.com/first/file.ext

Any ideas?

1 Like

@seasmith A workaround to use the path computation features of the fs package while preserving the URL would be to prepend the domain after performing the path computation. For example, something like the following:

options(domain = "https://<domain>.com")

add_domain <- function(path, domain = getOption("domain")) paste0(domain, path)

resolve_path <- function(base, ref) path_norm(path(base, ref))

p <- resolve_path("/first/second", "../file.ext")
p <- add_domain(p)
## [1] "https://<domain>.com/first/file.ext"
1 Like

@jdblischak Turns out xml2 had the winning function. Earlier, I had only examined url_relative() because it sounds right while url_absolute() is the winner.

base_url <- ""
href <- "../../t/create-web-file-paths-that-can-interpret"
xml2::url_absolute(href, base_url)
# [1] ""

@seasmith Awesome! Thanks for sharing your solution. That is a useful function to know about.

For future readers, could you please mark your answer as the solution?

FAQ: How do I mark a solution?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.