You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac系统下如何在R中创建可双击生成指定数量文件夹的Bash脚本

Fixing Your Mac Bash Script to Create Folders via R (Double-Clickable)

Got it, let's work through why your .sh script isn't behaving like the Windows .bat version. The key differences here are Mac's shell environment, how R is invoked non-interactively, file permissions, and default app associations for .sh files. Here's a step-by-step solution:

1. Understand the Core Issue

On Windows, .bat files run in Command Prompt and can call R.exe directly, but on Mac:

  • Double-clicked .sh files might open in TextEdit by default instead of Terminal.
  • You need to use Rscript (not the interactive R command) to run R code non-interactively.
  • Shell scripts need executable permissions to run.
  • Working directories for double-clicked scripts aren't always what you expect (use absolute paths!).

2. Create a Working Bash Script

Here's a complete, tested script that will do exactly what you need. Save it as create_folders.sh:

#!/bin/bash
# Ensure we're using bash shell

# Define your R logic inline (or point to a separate .R file)
Rscript -e '
# Replace this with your absolute target path (critical for double-click!)
Saveloc <- "/Users/your_username/Projects/My_Folders"
# Your vector of folder names (length = number of folders created)
folder_names <- c("Experiment_1", "Experiment_2", "Control_Group", "Backup")

# Create the base directory if it doesn't exist
if (!dir.exists(Saveloc)) {
  dir.create(Saveloc, recursive = TRUE)
}

# Loop to create each subfolder
for (folder in folder_names) {
  folder_path <- file.path(Saveloc, folder)
  dir.create(folder_path, recursive = TRUE, showWarnings = FALSE)
}

# Optional: Print confirmation to Terminal
cat("Successfully created", length(folder_names), "folders at", Saveloc, "\n")
'

Key Notes for the Script:

  • Absolute Path for Saveloc: Never use relative paths here—double-clicked scripts run from a default working directory (like ~/Desktop), so absolute paths ensure R knows exactly where to create folders.
  • Rscript Instead of R: R opens an interactive session, but Rscript runs code silently in the background (perfect for double-click use).
  • showWarnings = FALSE: Prevents errors if folders already exist.

3. Give the Script Executable Permissions

Mac won't let you run a shell script unless it has execution rights. Do this:

  1. Open Terminal.
  2. Run this command (replace /path/to/create_folders.sh with your actual file path):
    chmod +x /path/to/create_folders.sh
    

4. Make Double-Clicks Run the Script in Terminal

By default, Mac might open .sh files in TextEdit. Fix this:

  1. Right-click your create_folders.sh file.
  2. Select Get Info.
  3. Under Open With, click the dropdown menu and choose Terminal.
  4. Click Change All... to set Terminal as the default app for all .sh files.

5. Alternative: Create a Double-Clickable App (More User-Friendly)

If you want a cleaner experience (no Terminal window popping up, or a dedicated app icon), use Automator:

  1. Open Automator (found in Applications or via Spotlight).
  2. Create a new Application.
  3. In the search bar, find Run Shell Script and drag it into the workflow area.
  4. Paste the entire Rscript -e '...' block from the script above into the shell script box.
  5. Save the app (e.g., Create_R_Folders.app). Now double-clicking this app will run your R folder-creation logic silently.

Troubleshooting Tips

  • If Rscript isn't found: Run which Rscript in Terminal to get its full path (e.g., /Library/Frameworks/R.framework/Versions/Current/Resources/bin/Rscript) and replace Rscript in your script with this absolute path.
  • Permission errors: If the target folder is in a restricted area (like /System), add sudo before Rscript (you'll need to enter your password when running the script).
  • Test first: Run the script in Terminal first (./create_folders.sh) to catch any R syntax errors or path issues before relying on double-click.

内容的提问来源于stack exchange,提问作者FlyingDutch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:40:27