I would like to extract filename from url in R. For now I do it as follows, but maybe it can be done shorter like in python. assuming path is just string.
path="http://www.exanple.com/foo/bar/fooXbar.xls"
in R:
tail(strsplit(path,"[/]")[[1]],1)
in Python:
path.split("/")[-1:]
Maybe some sub, gsub solution?
The dirname function automatically removes the file name and returns on the directory path: This example illustrates how to show only the name of our file by applying the basename function to our path object: The RStudio console has returned the name of our file, i.e “my file.txt”.
In Example 1, I’ll demonstrate how to create a directory path using the file.path function in R. For this, we simply have to specify all elements of our path as character strings within the file.path function: my_directory <- file.path("C:", "Users", "Joach", "Desktop") # Create directory path my_directory # [1] "C:/Users/Joach/Desktop"
basename removes all of the path up to and including the last path separator (if any). dirname returns the part of the path up to but excluding the last path separator, or "." if there is no path separator. basename: Manipulate File Paths rdrr.ioFind an R packageR language docsRun R in your browser Home R Documentation base
As you can see based on the previous output of the RStudio console, we have created a new data object called my_directory, which contains the path to a folder on our computer. Note that this path was created in a platform-independent way, i.e. the file.path function provides proper operating system (OS) path format detection.
There's a function for that...
basename(path)
[1] "fooXbar.xls"
@SimonO101 has the most robust answer IMO, but some other options:
Since regular expressions are greedy, you can use that to your advantage
sub('.*/', '', path)
# [1] "fooXbar.xls"
Also, you shouldn't need the []
around the /
in your strsplit
.
> tail(strsplit(path,"/")[[1]],1)
[1] "fooXbar.xls"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With