-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_loop.R
More file actions
executable file
·66 lines (52 loc) · 1.77 KB
/
example_loop.R
File metadata and controls
executable file
·66 lines (52 loc) · 1.77 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
#!/usr/bin/env Rscript
# Preamble
##########################################################################
args <- commandArgs(trailingOnly = TRUE)
args
require(SmoothR, quietly = TRUE)
# Initialize session and set working directory
################################################################################
args <- InitNow(session_file_name = "empty.RData")
# Load command line arguments
################################################################################
cat(sprintf("Configuration:\n- Output directory: %s\n- Script name: %s\n- Suffix: %s\n- Timestamp: %s\n- Threads: %d\n",
args[1], args[2], args[3], args[4], as.integer(args[5])))
# Some analysis
################################################################################
require(ggplot2, quietly = TRUE)
Sys.sleep(30) # Simulated delay for process separation
checkpoint("Start of Analysis")
# Example plotting
pdf(file.path("plot1.pdf"))
plot(mpg)
dev.off()
checkpoint("Analysis Complete")
Sys.sleep(30)
# Prime number calculation function
is_prime <- function(num) {
if (num < 2) return(FALSE)
for (i in 2:sqrt(num)) {
if (num %% i == 0) return(FALSE)
}
return(TRUE)
}
find_primes <- function(limit) {
primes <- c()
for (num in 2:limit) {
if (length(primes) >= limit) break
if (is_prime(num)) primes <- c(primes, num)
}
return(primes)
}
prime_numbers <- find_primes(200)
checkpoint("First 20 prime numbers calculated.")
# Error handling with SafeExecute
safeExecute({ I_have_no_mouth_and_I_must_scream })
Sys.sleep(30)
# Save session and cleanup
################################################################################
saveNow()
Sys.sleep(30)
# Properly exit script
################################################################################
quit(status = 0)