Check existence of directory and create if doesn’t exist
I often find myself writing R scripts that generate a lot of output. I find it cleaner to put this output into it’s own directory(s). What I’ve written below will check for the existence of a directory and move into it, or create the directory and then move into it. Is there a better way to approach this?
9 Answers 9
Use showWarnings = FALSE :
dir.create() does not crash if the directory already exists, it just prints out a warning. So if you can live with seeing warnings, there is no problem with just doing this:
As of April 16, 2015, with the release of R 3.2.0 there’s a new function called dir.exists() . To use this function and create the directory if it doesn’t exist, you can use:
This will return FALSE if the directory already exists or is uncreatable, and TRUE if it didn’t exist but was succesfully created.
Note that to simply check if the directory exists you can use
In terms of general architecture I would recommend the following structure with regard to directory creation. This will cover most potential issues and any other issues with directory creation will be detected by the dir.create call.
Also be aware that if
/foo doesn’t exist then a call to dir.create(‘
/foo/bar’) will fail unless you specify recursive = TRUE .
Here’s the simple check, and creates the dir if doesn’t exists:
The use of file.exists() to test for the existence of the directory is a problem in the original post. If subDir included the name of an existing file (rather than just a path), file.exists() would return TRUE, but the call to setwd() would fail because you can’t set the working directory to point at a file.
I would recommend the use of file_test(op=»-d», subDir), which will return «TRUE» if subDir is an existing directory, but FALSE if subDir is an existing file or a non-existent file or directory. Similarly, checking for a file can be accomplished with op=»-f».
Additionally, as described in another comment, the working directory is part of the R environment and should be controlled by the user, not a script. Scripts should, ideally, not change the R environment. To address this problem, I might use options() to store a globally available directory where I wanted all of my output.
So, consider the following solution, where someUniqueTag is just a programmer-defined prefix for the option name, which makes it unlikely that an option with the same name already exists. (For instance, if you were developing a package called «filer», you might use filer.mainDir and filer.subDir).
The following code would be used to set options that are available for use later in other scripts (thus avoiding the use of setwd() in a script), and to create the folder if necessary:
Then, in any subsequent script that needed to manipulate a file in subDir, you might use something like:
This solution leaves the working directory under the control of the user.
Источник
This X Does Not Exist
Using generative adversarial networks (GAN), we can learn how to create realistic-looking fake versions of almost anything, as shown by this collection of sites that have sprung up in the past month. Learn how it works .
This Person Does Not Exist
The site that started it all, with the name that says it all. Created using a style-based generative adversarial network (StyleGAN), this website had the tech community buzzing with excitement and intrigue and inspired many more sites.
This Cat Does Not Exist
These purr-fect GAN-made cats will freshen your feeline-gs and make you wish you could reach through your screen and cuddle them. Once in a while the cats have visual deformities due to imperfections in the model – beware, they can cause nightmares.
This Rental Does Not Exist
Why bother trying to look for the perfect home when you can create one instead? Just find a listing you like, buy some land, build it, and then enjoy the rest of your life.
This Waifu Does Not Exist
This one is a little stranger than the others, but I bet you’ll read more of the plot summary than you expect. It’s engrossing.
This URL Does Not Exist
It doesn’t, but at the same time it kind of does. Does that sound too cryptic? Click on it and you’ll understand (and look at the URL). A simple but effective satire into the entire hype, but definitely doesn’t use any deep learning.
This Startup Does Not Exist
If your IP address is in San Francisco, this site actually just redirects to a random portfolio company from an AI-, blockchain-, or crypto-focused venture capital firm. Can you spot the difference? (Disclaimer: that was a joke).
This Question Does Not Exist
By generating seemingly realistic Stack Overflow questions, you can keep this open on your computer to seem like you’re actually doing work. The most interesting aspect is the «Other Random Questions» section on the right side of the page, which shows other equally synthetic questions.
This Resume Does Not Exist
To showcase their resume template builder, this company went as far as to incorporate TextgenRNN – not quite a GAN but rather a recurrent neural network (RNN) specializing in text generation. Bonus points – the headshots used in each resume are from thispersondoesnotexist.com.
This Emotion Does Not Exist
While it doesn’t necessarily use Deep Learning, this site showcases the power of a relatively simple model’s ability to interpret emotions from facial expressions. The results may shock you. Or delight you. Or anger you. Regardless, the site can identify the emotion.
This Vessel Does Not Exist
Drawing a beautiful parallel between Generators and Discriminators in GANs and apprentices and masters in ceramics, this site demonstrates the beautiful ability of neural networks to replicate the mastery of professionals at yet another craft.
These Lyrics Do Not Exist
Now we can generate lyrics for a song given a theme or topic. If only we combined this with text-to-speech and a melody-generating model to create completely original songs. Then the music industry would take GANs seriously.
This Snack Does Not Exist
For when you’re feeling especially hungry and creative, this website will remind you that you’re more hungry and less creative than you thought.
This Meme Does Not Exist
Honestly, if I had to quantify the site with the largest value-add to humanity, this would be in the top three. Memes galore and so much more on every pageload.
This Chair Does Not Exist
Before I explain this site, you may want to take a seat, preferably in one of the chairs generated by this GAN. What’s more is that the chairs are all 3D and you can fine tune the «weirdness.»
This Foot Does Not Exist
Note that this is an SMS chatbot. You can text it to get pictures of feet. The pictures are animated. The feet are nonexistent. Why would you want to do this? Who knows.
This Artwork Does Not Exist
Be inspired by minimalism, realism, post-modernism, pre-modernism, modernism, and ancientism (not actually a thing). No matter your art preferences, you can find it here with enough refreshing.
This Chemical Does Not Exist
Who said drug discovery is hard? Just refresh until you find the right chemical. In all seriousness, the fact that this renders a 3D model with the correct bond pairings is impressive.
This Horse Does Not Exist
Some of the horses have people on them, but it seems like they’re also generated by the GAN since something always seems a bit off with the people. The horses, as always, look great.
This Word Does Not Exist
Using GPT-2, this website manages to generate words that sound like they should exist, but don’t. Great for startup names or baby names (now that the coveted X Æ A-12 is taken).
This Satire Does Not Exist
It’s hard enough to tell what news is real and what news isn’t with The Onion and the craziness of the world today. But this site takes it a bit further by generating everything. And yet it all seems so plausible.
This MP Does Not Exist
With the state of politics in the world, who wouldn’t want a refreshing new face in charge? The author of this site trained a GAN on real faces of UK Parliament. Unfortunately the faces this model generates don’t actually exist, but they still get my vote.
This Pony Does Not Exist
This site was created with user experience in mind. You can pan the generated ponies, zoom in, and even auto-expand on hover. And best of all, each pony is uniquely made for you.
This Automobile Does Not Exist
Legend has it Elon used this website to design the Cybertruck. From there, he made some slight changes to the curvature on the left windows, but that was it.
This Eye Does Not Exist
It’s not hard to see why this site is impactful. In fact, while you’re looking at this site, you might find this site looking back at you. A helpful feature would be Sephora links to the shown makeup, but I wouldn’t hold your breath for that feature coming anytime soon.
This Butterfly Does Not Exist
Surprisingly, butterflies look a lot scarier up close and on your monitor. Or is that just me? Anyway, take digitised specimens from the Natural History Musem in London, sprinkle in a little GAN magic, and you get this website.
This Beetle Does Not Exist
It turns out there are insects creepier than large butterflies. Using public domain images from the book Biologia Centrali-Americana :zoology, botany and archaeology , this site manages to create nonexistent species of beetles – as if the world didn’t already have enough of them.
This City Does Not Exist
The next time your flight attendant tells you to close the window shades, look no further than this website. With it, you can get a unique new overhead view every time you refresh the page. And the best part is, you can almost make out your house from up here!
This Food Blog Does Not Exist
Looking to surprise someone special with a new recipe that’ll leave them wondering where you got your newly found culinary talents? Yeah, this is not the site for you. But, if you’re looking to share a dank, new, AI-generated site that boasts the prowess of GANs, this site IS for you!
This Fu***ed-up Homer Does Not Exist
I have seen the dark side of the interwebs, and this is 100% cacao (read: very dark), folks. Visit at your own peril – you’ll never look at the Simpsons in the same way.
This Fursona Does Not Exist
I’m not sure exactly what to do think of this. On the one hand, I learned the word «fursona» today. On the other hand, I discovered the page «wikifur.com» today. These are strange times. Regardless, excellent execution of a StyleGAN.
This Night Sky Does Not Exist
Over 55% of the world lives in an urban area with immense light pollution, unable to see the beauty of the stars and constellations. Unable to see the wonders of space. Unable to see whether they’re a Scorpio or a Pisces – this site aims to solve that by democratizing access to the night sky.
This Music Video Does Not Exist
Next up on the AI chopping block – the music industry. By combining OpenAI’s Jukebox and a StyleGAN for visuals, this website will give you a never-before-seen music video worthy of a VMA. Watch out MTV – you’re about to have a lot more nominations this year than you expected.
Источник