R语言here()函数疑问:.here文件定义及创建方法咨询
Hey there! Let's sort out your issue with the here package in R and that confusing .here file.
.here File? The .here file is a simple, empty marker file that tells the here() function where your project's root directory is located. When you call here(), it scans upward from your current working directory until it finds a .here file—once it does, that directory becomes the "root" it uses to build file paths for your project. It doesn't need any content inside; the only thing that matters is its exact name: .here.
You've got two straightforward options:
From R (easiest way)
Just navigate to your target workflow directory (the one you want to use as your project root) in R, then run this command:library(here) set_here()This will automatically create a proper
.herefile in the right place—no manual file management needed.Manually (outside R)
If you prefer to create it yourself:- On Mac/Linux: Open a terminal, navigate to your project root directory, and run
touch .here. This creates an empty file named.here. - On Windows: Open Command Prompt, go to your project root, and run
type nul > .here. Windows' file explorer can be finicky about creating files without extensions, so using CMD avoids the "you must enter a filename" error.
- On Mac/Linux: Open a terminal, navigate to your project root directory, and run
.here.txt Didn't Work The key problem here is the file extension! The here() function only looks for a file with the exact name .here—it ignores any suffix like .txt. Your .here.txt file was invisible to the function, which is why it didn't set your project root correctly.
Once you've created the proper .here file, test it by running:
library(here) here()
This should print the absolute path to your target workflow directory—if it does, you're all set!
内容的提问来源于stack exchange,提问作者Pierre Dudek

