Where am I?

[This article was first published on HighlandR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Notes on the here package –

The here package is pretty simple ( only 3 functions), but I cannot remember how to use it to navigate folders, so this is my aide-memoire.
It might be useful for others too.

Here finds the root of your current folder / working directory.
If you use Projects in RStudio, that will usually be the root of your project folder.
If not, you can use set_here() to create a small file which will set the root location.

<span class="w">
</span><span class="c1"># where am I in my computer?</span><span class="w">
</span><span class="n">here</span><span class="p">()</span><span class="w">

</span><span class="s2">"C:/Users/some/path/or/other"</span><span class="w">

</span><span class="c1">#</span><span class="w">
</span><span class="c1"># create a folder</span><span class="w">
</span><span class="n">dir.create</span><span class="p">(</span><span class="n">here</span><span class="p">(</span><span class="s2">"img"</span><span class="p">))</span><span class="w">
</span><span class="s2">"C:/Users/some/path/or/other/img"</span><span class="w">

</span><span class="n">dir.create</span><span class="p">(</span><span class="n">here</span><span class="p">(</span><span class="s2">"img"</span><span class="p">,</span><span class="s2">"png"</span><span class="p">))</span><span class="w">
</span><span class="s2">"C:/Users/some/path/or/other/img/png"</span><span class="w">


</span><span class="c1">#move to the outputs folder using setwd()</span><span class="w">
</span><span class="n">setwd</span><span class="p">(</span><span class="n">here</span><span class="p">(</span><span class="s2">"img"</span><span class="p">))</span><span class="w">

</span><span class="c1"># check this worked - get the current working directory with  getwd()</span><span class="w">
</span><span class="n">getwd</span><span class="p">()</span><span class="w">

</span><span class="s2">"C:/Users/some/path/or/other/img"</span><span class="w">

</span><span class="c1"># go back to the root of the project</span><span class="w">
</span><span class="n">setwd</span><span class="p">(</span><span class="n">here</span><span class="p">())</span><span class="w">

</span><span class="c1">#check you are back where you started</span><span class="w">

</span><span class="n">getwd</span><span class="p">()</span><span class="w">

</span><span class="s2">"C:/Users/some/path/or/other"</span><span class="w">

</span>

Now the next time I am like this:

¯_(ヅ)_/¯

I can just read this post, and hope no-one sees me reading my own blog.

To leave a comment for the author, please follow the link and comment on their blog: HighlandR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)