R Cheatsheet: Files and Directories


Check whether a directory exists, if true then nothing happen

if(!file.exists("test")){

dir.create("test")}

 

Next Step, write a function that downloads a csv file from the internet:

downloadfileurl<-function(){

#Check if a directory exists, otherwise we create 
 if(!file.exists("data")){
 dir.create("data")}
 
 #Next step, download a file from the internet.
 
 #First we create a variable with the url which contains the data:
 fileURl<-"https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD"
 
 #Next variable containts the location of local copy of downloaded file
 localcopy<-"./data/cameras.csv"
 
 #In order to obtain an online file we must use the download.file() function
 #Since we are working from a Windows terminal the third parameter (method) should work
 #with the default value. If you're working from a Mac, then you must specify its value to "curl"
 #because that file is available via https protocol
 download.file(fileURl,destfile = localcopy)
 
 #we check the files in that directory
 files<-list.files("./data")
 print(files)
 #Finally we print the date we downloaded that file. This is very important specially because you need
 #to be able to keep track of that file.
 datedownloaded<-date()
 print(datedownloaded)
}

After executing this function we obtain as a result:

result-download-file

Now we check the existence of the new file using the File Explorer:

camerascsv

Etiquetas: , ,

About martincx

| Trabajo con Computadores (Y personas) | Ancient Alien Theorist (?)| Madman | Internet | Libros | Con una lista interminable de cosas por hacer, pero completada de a poco. Follow me on twitter.com @martincx

Este blog se enriquece con los comentarios, deja acá tu impresión.

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s

A %d blogueros les gusta esto: