"last directory" from url as string

I would like to return "blah".

url <- "https://www.example.com/apples/pears/blah.csv"

I can get blah but I feel like I'm writing more lines of code than I should. Example, to get blah.csv I can do:

url_split <- str_split(url, "/")
dirname <- url_split[[1]][length(url_split[[1]])]

This gives me "blah.csv", where I can do a very similar code block as above to get "blah" by calling str_split again.

Is there a more sophisticated one liner to get the last directory in the url minus ".csv"?

1 answer

  • answered 2017-10-11 10:22 Thierry

    fn <- basename("https://www.example.com/apples/pears/blah.csv")
    gsub("\\..*$", "", fn)