|
3 | 3 | # Set the precision to 16 digits: |
4 | 4 | options( digits = 16 ); |
5 | 5 |
|
6 | | -#' Get the script filepath. |
7 | | -#' |
8 | | -#' @return The absolute path of this script |
| 6 | +#' Generate test fixtures. |
9 | 7 | #' |
10 | 8 | #' @examples |
11 | | -#' filepath <- get_script_path() |
12 | | -get_script_path <- function() { |
13 | | - cmdArgs <- commandArgs( trailingOnly = FALSE ); |
14 | | - needle <- "--file="; |
15 | | - match <- grep( needle, cmdArgs ); |
16 | | - if ( length( match ) > 0 ) { |
17 | | - # Rscript: |
18 | | - filepath <- sub( needle, "", cmdArgs[match] ); |
19 | | - } else { |
20 | | - ls_vars <- ls( sys.frames()[[1]] ) |
21 | | - if ( "fileName" %in% ls_vars ) { |
22 | | - # Source'd via RStudio: |
23 | | - filepath <- sys.frames()[[1]]$fileName; |
| 9 | +#' main(); |
| 10 | +main <- function() { |
| 11 | + #' Get the script filepath. |
| 12 | + #' |
| 13 | + #' @return The absolute path of this script |
| 14 | + #' |
| 15 | + #' @examples |
| 16 | + #' filepath <- get_script_path(); |
| 17 | + get_script_path <- function() { |
| 18 | + args <- commandArgs( trailingOnly = FALSE ); |
| 19 | + needle <- "--file="; |
| 20 | + match <- grep( needle, args ); |
| 21 | + if ( length( match ) > 0 ) { |
| 22 | + # Rscript: |
| 23 | + filepath <- sub( needle, "", args[match] ); |
24 | 24 | } else { |
25 | | - # Source'd via R console: |
26 | | - filepath <- sys.frames()[[1]]$ofile; |
| 25 | + ls_vars <- ls( sys.frames()[[1]] ) |
| 26 | + if ( "fileName" %in% ls_vars ) { |
| 27 | + # Source'd via RStudio: |
| 28 | + filepath <- sys.frames()[[1]]$fileName; # nolint |
| 29 | + } else { |
| 30 | + # Source'd via R console: |
| 31 | + filepath <- sys.frames()[[1]]$ofile; |
| 32 | + } |
27 | 33 | } |
| 34 | + return( normalizePath( filepath ) ); |
28 | 35 | } |
29 | | - return( normalizePath( filepath ) ); |
30 | | -} |
31 | | - |
32 | | -#' Convert a data structure to JSON. |
33 | | -#' |
34 | | -#' @param x A data structure to convert |
35 | | -#' @return JSON blob |
36 | | -#' |
37 | | -#' @examples |
38 | | -#' x <- seq( -6.5, 25, 0.5 ); |
39 | | -#' json <- to_json( x ); |
40 | | -to_json <- function( x ) { |
41 | | - return( jsonlite::toJSON( x, digits = 16, auto_unbox = TRUE ) ); |
42 | | -} |
43 | | - |
44 | | -#' Generate an output absolute filepath based on the script directory. |
45 | | -#' |
46 | | -#' @param name An output filename |
47 | | -#' @return An absolute filepath |
48 | | -#' |
49 | | -#' @examples |
50 | | -#' filepath <- get_filepath( "data.json" ); |
51 | | -get_filepath <- function( name ) { |
52 | | - return( paste( source_dir, "/", name, sep = "" ) ); |
53 | | -} |
54 | | - |
55 | | -# Get the directory of this script: |
56 | | -source_dir <- dirname( get_script_path() ); |
57 | | - |
58 | | -rho = 0.5 |
59 | | -x <- rnorm( 200 ) |
60 | | -y <- rnorm( 200, 0.0, sqrt( 1.0 - rho*rho ) ) + rho * x |
61 | | -out <- cor.test( x, y, method = "pearson" ) |
62 | | - |
63 | | -# Convert fixture data to JSON: |
64 | | -twosided = list( |
65 | | - x = x, |
66 | | - y = y, |
67 | | - statistic = out$statistic, |
68 | | - pValue = out$p.value, |
69 | | - ci = out$conf.int |
70 | | -) |
71 | | -twosided = to_json( twosided ) |
72 | | - |
73 | | -# Write the data to file... |
74 | | -filepath <- get_filepath( "twosided.json" ); |
75 | | -write( twosided, filepath ); |
76 | | - |
77 | | - |
78 | | -x <- rnorm( 200 ) |
79 | | -y <- rnorm( 200 ) - 0.5 * x |
80 | | -out <- cor.test( x, y, method = "pearson", alternative = "less" ) |
81 | | - |
82 | | -less = list( |
83 | | - x = x, |
84 | | - y = y, |
85 | | - statistic = out$statistic, |
86 | | - pValue = out$p.value, |
87 | | - ci = out$conf.int |
88 | | -) |
89 | | -less = to_json( less ) |
90 | | - |
91 | | -# Write the data to file... |
92 | | -filepath <- get_filepath( "less.json" ); |
93 | | -write( less, filepath ); |
94 | 36 |
|
| 37 | + #' Convert a data structure to JSON. |
| 38 | + #' |
| 39 | + #' @param x A data structure to convert |
| 40 | + #' @return JSON blob |
| 41 | + #' |
| 42 | + #' @examples |
| 43 | + #' x <- seq( -6.5, 25, 0.5 ); |
| 44 | + #' json <- to_json( x ); |
| 45 | + to_json <- function( x ) { |
| 46 | + return( jsonlite::toJSON( x, digits = 16, auto_unbox = TRUE ) ); |
| 47 | + } |
95 | 48 |
|
96 | | -x <- rnorm( 200 ) |
97 | | -y <- rnorm( 200 ) - 0.1 * x |
98 | | -out <- cor.test( x, y, method = "pearson", alternative = "greater" ) |
| 49 | + #' Generate an output absolute filepath based on the script directory. |
| 50 | + #' |
| 51 | + #' @param name An output filename |
| 52 | + #' @return An absolute filepath |
| 53 | + #' |
| 54 | + #' @examples |
| 55 | + #' filepath <- get_filepath( "data.json" ); |
| 56 | + get_filepath <- function( name ) { |
| 57 | + return( paste( source_dir, "/", name, sep = "" ) ); |
| 58 | + } |
99 | 59 |
|
100 | | -greater = list( |
101 | | - x = x, |
102 | | - y = y, |
103 | | - statistic = out$statistic, |
104 | | - pValue = out$p.value, |
105 | | - ci = out$conf.int |
106 | | -) |
107 | | -greater = to_json( greater ) |
| 60 | + # Get the directory of this script: |
| 61 | + source_dir <- dirname( get_script_path() ); |
| 62 | + |
| 63 | + # Generate test fixture data: |
| 64 | + rho <- 0.5; |
| 65 | + x <- rnorm( 200 ); |
| 66 | + y <- rnorm( 200, 0.0, sqrt( 1.0 - rho*rho ) ) + rho*x; |
| 67 | + out <- cor.test( x, y, method = "pearson" ); |
| 68 | + |
| 69 | + # Convert fixture data to JSON: |
| 70 | + twosided <- list( |
| 71 | + x = x, |
| 72 | + y = y, |
| 73 | + statistic = out$statistic, |
| 74 | + pValue = out$p.value, |
| 75 | + ci = out$conf.int |
| 76 | + ); |
| 77 | + twosided <- to_json( twosided ); |
| 78 | + |
| 79 | + # Write the data to file... |
| 80 | + filepath <- get_filepath( "twosided.json" ); |
| 81 | + write( twosided, filepath ); |
| 82 | + |
| 83 | + # Generate test fixture data: |
| 84 | + x <- rnorm( 200 ); |
| 85 | + y <- rnorm( 200 ) - 0.5*x; |
| 86 | + out <- cor.test( x, y, method = "pearson", alternative = "less" ); |
| 87 | + |
| 88 | + less <- list( |
| 89 | + x = x, |
| 90 | + y = y, |
| 91 | + statistic = out$statistic, |
| 92 | + pValue = out$p.value, |
| 93 | + ci = out$conf.int |
| 94 | + ); |
| 95 | + less <- to_json( less ); |
| 96 | + |
| 97 | + # Write the data to file... |
| 98 | + filepath <- get_filepath( "less.json" ); |
| 99 | + write( less, filepath ); |
| 100 | + |
| 101 | + # Generate test fixture data: |
| 102 | + x <- rnorm( 200 ); |
| 103 | + y <- rnorm( 200 ) - 0.1*x; |
| 104 | + out <- cor.test( x, y, method = "pearson", alternative = "greater" ); |
| 105 | + |
| 106 | + greater <- list( |
| 107 | + x = x, |
| 108 | + y = y, |
| 109 | + statistic = out$statistic, |
| 110 | + pValue = out$p.value, |
| 111 | + ci = out$conf.int |
| 112 | + ); |
| 113 | + greater <- to_json( greater ); |
| 114 | + |
| 115 | + # Write the data to file... |
| 116 | + filepath <- get_filepath( "greater.json" ); |
| 117 | + write( greater, filepath ); |
| 118 | +} |
108 | 119 |
|
109 | | -# Write the data to file... |
110 | | -filepath <- get_filepath( "greater.json" ); |
111 | | -write( greater, filepath ); |
| 120 | +main(); |
0 commit comments