Summary
この記事では、Rの`here`パッケージによるファイルパス管理について、日本語環境特有の注意点とともに、その利便性や活用法をご紹介します。このテーマは、多くのRユーザーにとって大変価値ある内容となっています。 Key Points:
- Rの`here`パッケージを使って、OSに依存しないファイルパスを構築する方法を学ぶことで、日本語環境特有の文字コード問題を解決できます。例えば、`fs::path(here("画像"))`を利用すれば、自動的にセパレータが処理されるので、安心してプロジェクトが進められます。
- 日本語圏における`here`パッケージのドキュメント不足を補うため、高度なユーザー向けに具体例やベストプラクティスを提供することが重要です。これによって、実際のプロジェクトで遭遇しがちなエラーも回避できるようになります。
- 再現性と共有性の向上は、複数人で作業する際には欠かせません。`here`パッケージによって管理されたファイルパスは、Gitなどとの連携でも威力を発揮し、一貫した開発環境を維持できます。
日本語フォルダでhereパッケージを使う時の落とし穴
Alright, so here’s the deal with the `here` package in R—a lifesaver for managing file paths, but somehow, I still manage to fumble with it every now and then. It’s super simple, really—just three functions—but creating subfolders always trips me up. That’s why I’m jotting this down, mostly for myself, but maybe you’ll find it handy too.
The `here()` function finds the root of your project folder, which is usually where your RStudio Project is located. (If it’s not, you can force it with `set_here()` by creating a small marker file.) From there, `here()` becomes your anchor point, and you can build paths relative to it. This makes your code portable—no more breaking scripts because someone’s directory structure doesn’t match yours. And trust me, on a network, those file paths can get *messy*.
The part I always mess up? Creating subfolders. So here’s how it works:
- `here()` tells me where I am, e.g., `C:/Users/some/path/or/other`.
- To create a folder named `img`, I’d use `dir.create(here("img"))`.
- For a nested folder, like `img/png`, it’s `dir.create(here("img", "png"))`.
Then, if I want to move into the `img` folder, I’d use `setwd(here("img"))`. To check if it worked, `getwd()` confirms I’m in the right spot. And to get back to the root? `setwd(here())`, and we’re golden.
Next time I’m staring at my screen like ¯\_(ツ)_/¯, I’ll just come back to this and pretend no one’s watching me read my own notes.
The `here()` function finds the root of your project folder, which is usually where your RStudio Project is located. (If it’s not, you can force it with `set_here()` by creating a small marker file.) From there, `here()` becomes your anchor point, and you can build paths relative to it. This makes your code portable—no more breaking scripts because someone’s directory structure doesn’t match yours. And trust me, on a network, those file paths can get *messy*.
The part I always mess up? Creating subfolders. So here’s how it works:
- `here()` tells me where I am, e.g., `C:/Users/some/path/or/other`.
- To create a folder named `img`, I’d use `dir.create(here("img"))`.
- For a nested folder, like `img/png`, it’s `dir.create(here("img", "png"))`.
Then, if I want to move into the `img` folder, I’d use `setwd(here("img"))`. To check if it worked, `getwd()` confirms I’m in the right spot. And to get back to the root? `setwd(here())`, and we’re golden.
Next time I’m staring at my screen like ¯\_(ツ)_/¯, I’ll just come back to this and pretend no one’s watching me read my own notes.
本段の原文をご参照ください: https://www.johnmackintosh.net/blog/2018-09-23-where-am-i/
日本語圏でhereパッケージを広めるヒント
`here`パッケージを使うのは便利だけど、日本語圏でこれを広めようとすると、ちょっとした壁にぶつかるかもしれないなぁ。まず、フォルダ名やパスに日本語を使う場合、文字コードの問題が発生しやすい。特にWindowsとMacで文字コードが違ったりするし、ネットワーク上で共有するときはもっと複雑になる。例えば、日本語のフォルダ名を指定して`dir.create(here("画像"))`とかやると、エラーが出たり、パスが正しく認識されなかったりする可能性がある。
それに、日本語のドキュメントやチュートリアルが少ないので、Rを使い始めたばかりの人には敷居が高く感じられるかもしれない。「ここで`here()`を使えばパス問題が解決するよ」と言っても、英語のドキュメントを読むのが苦手な人には伝わりにくい。また、日本ではファイル管理の習慣が違うこともある。プロジェクトのルートをどこに置くか、どんなフォルダ構造にするか、といった部分で文化差が出てくるかもしれない。
でも、これって実はチャンスでもあると思う。日本語での解説やサンプルコードを作ってシェアすれば、もっとみんなに使ってもらえるんじゃないかな。ちょっと手間はかかるけど、それが自分のスキルアップにもつながるし。うまくいけば、`here`パッケージが日本語圏でも当たり前のツールになるかもしれない。まあ、焦らず少しずつやっていこうかな。
それに、日本語のドキュメントやチュートリアルが少ないので、Rを使い始めたばかりの人には敷居が高く感じられるかもしれない。「ここで`here()`を使えばパス問題が解決するよ」と言っても、英語のドキュメントを読むのが苦手な人には伝わりにくい。また、日本ではファイル管理の習慣が違うこともある。プロジェクトのルートをどこに置くか、どんなフォルダ構造にするか、といった部分で文化差が出てくるかもしれない。
でも、これって実はチャンスでもあると思う。日本語での解説やサンプルコードを作ってシェアすれば、もっとみんなに使ってもらえるんじゃないかな。ちょっと手間はかかるけど、それが自分のスキルアップにもつながるし。うまくいけば、`here`パッケージが日本語圏でも当たり前のツールになるかもしれない。まあ、焦らず少しずつやっていこうかな。

Reference Articles
R のインストールと管理
主ライブラリは 'R HOME/library' であるが、他のも使える。例えば、環境変数 R_LIBS. を設定するか、R の関数 .libPaths() を使う。 5.1 パッケージのインストール.
Source: 中澤 港日本語プロットの文字化けストレスを低減する - RStudio v1.4 ...
RStudio v1.4とraggパッケージの登場でRStudio上で日本語の作図が面倒な指定不要で行えるようになりました。記事中で紹介する方法をとれば、RStudio ...
Source: はてなブログ
Related Discussions