Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get filename from url path in R

Tags:

regex

r

gsub

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?

like image 790
andilabs Avatar asked Sep 13 '13 14:09

andilabs


People also ask

How to show only the name of a file in R?

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”.

How do I create a directory path in R?

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"

What is the difference between basename and dirname in R?

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

How to find the path to a folder in RStudio?

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.


2 Answers

There's a function for that...

basename(path)
[1] "fooXbar.xls"
like image 183
Simon O'Hanlon Avatar answered Oct 01 '22 01:10

Simon O'Hanlon


@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"
like image 29
Justin Avatar answered Oct 01 '22 01:10

Justin