-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrstudio-mac.sh
More file actions
121 lines (95 loc) · 2.99 KB
/
rstudio-mac.sh
File metadata and controls
121 lines (95 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# Get the path to RStudio application
RSTUDIO_APP="/Applications/RStudio.app"
# Check if RStudio is installed
if [ ! -d "$RSTUDIO_APP" ]; then
echo "Error: RStudio not found at $RSTUDIO_APP"
exit 1
fi
# Create a new project file if one doesn't exist
create_project_file() {
local dir_path="$1"
local dir_name=$(basename "$dir_path")
local project_file="${dir_path}/${dir_name}.Rproj"
# Create project file with basic configuration
cat > "$project_file" << EOF
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
EOF
echo "Created new RStudio project at $project_file" >&2
# Return just the path without any additional text
echo "$project_file"
}
# Find an existing project file in a directory
find_project_file() {
local dir_path="$1"
local project_file=$(find "$dir_path" -maxdepth 1 -name "*.Rproj" | head -n 1)
echo "$project_file"
}
# No arguments - just open RStudio
if [ $# -eq 0 ]; then
open "$RSTUDIO_APP"
exit 0
fi
# Get the absolute path of the argument
input_path="$1"
# If path is ".", use current directory
if [ "$input_path" = "." ]; then
input_path="$(pwd)"
fi
# Get absolute path
if [ -d "$input_path" ]; then
absolute_path=$(cd "$input_path" 2>/dev/null && pwd)
else
absolute_path=$(cd "$(dirname "$input_path")" 2>/dev/null && pwd)/$(basename "$input_path")
fi
# Check if the path exists
if [ ! -e "$absolute_path" ]; then
echo "Error: Path does not exist: $input_path"
exit 1
fi
# Check if the path is a directory
if [ -d "$absolute_path" ]; then
# Look for an existing project file
project_file=$(find_project_file "$absolute_path")
# If no project file exists, create one
if [ -z "$project_file" ]; then
project_file=$(create_project_file "$absolute_path")
# Verify the project file was created
if [ ! -f "$project_file" ]; then
echo "Error: Failed to create project file at $project_file"
exit 1
fi
fi
# Verify project file exists before opening
if [ ! -f "$project_file" ]; then
echo "Error: Project file does not exist: $project_file"
exit 1
fi
# Open RStudio with the project file
open -a "$RSTUDIO_APP" "$project_file"
# Check if the path is an .Rproj file
elif [[ "$absolute_path" == *.Rproj ]]; then
# Open RStudio with the project file
open -a "$RSTUDIO_APP" "$absolute_path"
# Check if the path is a file (any type)
elif [ -f "$absolute_path" ]; then
# Open RStudio with the file
open -a "$RSTUDIO_APP" "$absolute_path"
else
echo "Error: Not a directory, .Rproj file, or valid file: $input_path"
exit 1
fi