Module: Rust
- Defined in:
- lib/rust/core/rust.rb,
lib/rust.rb,
lib/rust/core/csv.rb,
lib/rust/core/manual.rb,
lib/rust/models/anova.rb,
lib/rust/core/types/list.rb,
lib/rust/core/types/utils.rb,
lib/rust/core/types/factor.rb,
lib/rust/core/types/matrix.rb,
lib/rust/core/types/s4class.rb,
lib/rust/core/types/datatype.rb,
lib/rust/core/types/language.rb,
lib/rust/stats/probabilities.rb,
lib/rust/core/types/dataframe.rb
Overview
Basic module for the Rust package. It includes a series of sub-modules that provide specific features, such as statistical hypothesis tests, plots, and so on.
Defined Under Namespace
Modules: Correlation, Descriptive, EffectSize, Models, Plots, Probabilities, RBindings, RandomVariableExamples, StatisticalTests, TestCases Classes: ANOVAModel, Arguments, CSV, Call, DataFrame, DataFrameArray, DataFrameHash, Environment, Factor, FactorValue, Formula, Function, List, Manual, MathArray, Matrix, Null, Options, RandomVariable, RandomVariableSlice, RustDatatype, S4Class, Sequence, UniformRandomVariable, Variable, Verbatim
Constant Summary collapse
- @@datasets =
{}
- @@debugging =
$RUST_DEBUG || false
- @@in_client_mutex =
false
Class Method Summary collapse
-
.[](variable) ⇒ Object
Retrieves the value of a variable from the R environment.
-
.[]=(variable, value) ⇒ Object
Sets a variable in the R environment with a given value.
- ._eval(r_command, return_warnings = false) ⇒ Object
- ._eval_big(r_command, return_warnings = false) ⇒ Object
- ._pull(r_command, return_warnings = false) ⇒ Object
- ._rexec(r_command, return_warnings = false) ⇒ Object
- .cars ⇒ Object
-
.check_library(name) ⇒ Object
Checks if the given
namelibrary can be used. -
.debug ⇒ Object
Sets the debug mode.
-
.debug? ⇒ Boolean
Checks if the debug mode is active.
-
.exclusive ⇒ Object
Runs the given block with a mutex.
- .formula(left_part, right_part) ⇒ Object
- .function(name) ⇒ Object
-
.help!(mod = nil) ⇒ Object
Ask for help on a given
mod. -
.install_library(name, github = false) ⇒ Object
Installs the given
namelibrary and its dependencies. - .iris ⇒ Object
-
.load_library(name) ⇒ Object
Loads the given
namelibrary. -
.prerequisite(library, github = false) ⇒ Object
Installs the
librarylibrary if it is not available and loads it. - .toothgrowth ⇒ Object
- .variable(variable) ⇒ Object
- .verbatim(expression) ⇒ Object
Class Method Details
.[](variable) ⇒ Object
Retrieves the value of a variable from the R environment.
Example: Rust
71 72 73 |
# File 'lib/rust/core/rust.rb', line 71 def self.[](variable) return RustDatatype.pull_variable(variable) end |
.[]=(variable, value) ⇒ Object
Sets a variable in the R environment with a given value.
Raises an error if the value can not be translated into an R object.
Example: Rust = 0.
56 57 58 59 60 61 62 63 64 |
# File 'lib/rust/core/rust.rb', line 56 def self.[]=(variable, value) if value.is_a?(RustDatatype) value.load_in_r_as(variable.to_s) elsif value.is_a?(String) || value.is_a?(Numeric) || value.is_a?(Array) || value.is_a?(::Matrix) R_ENGINE.assign(variable, value) else raise "Trying to assign #{variable} with #{value.class}; expected RustDatatype, String, Numeric, or Array" end end |
._eval(r_command, return_warnings = false) ⇒ Object
100 101 102 |
# File 'lib/rust/core/rust.rb', line 100 def self._eval(r_command, return_warnings = false) self._rexec(r_command, return_warnings) { |cmd| R_ENGINE.eval(cmd) } end |
._eval_big(r_command, return_warnings = false) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rust/core/rust.rb', line 75 def self._eval_big(r_command, return_warnings = false) r_command = r_command.join("\n") if r_command.is_a?(Array) self._rexec(r_command, return_warnings) do |cmd| result = true instructions = cmd.lines while instructions.size > 0 current_command = "" while (instructions.size > 0) && (current_command.length + instructions.first.length < 10000) current_command << instructions.shift end result &= R_ENGINE.eval(current_command) end result end end |
._pull(r_command, return_warnings = false) ⇒ Object
96 97 98 |
# File 'lib/rust/core/rust.rb', line 96 def self._pull(r_command, return_warnings = false) self._rexec(r_command, return_warnings) { |cmd| R_ENGINE.pull(cmd) } end |
._rexec(r_command, return_warnings = false) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rust/core/rust.rb', line 104 def self._rexec(r_command, return_warnings = false) if @@debugging puts "Calling _rexec with command: #{r_command}" puts "\t" + Kernel.caller.select { |v| !v.include?("irb") }.last(3).map { |v| v.sub(/^.*gems\//, "")}.join("\n\t") end R_MUTEX.synchronize do assert("This command must be executed in an exclusive block") { @@in_client_mutex } result = nil begin $stdout = StringIO.new if return_warnings R_ENGINE.echo(true, true) else R_ENGINE.echo(false, false) end result = yield(r_command) ensure R_ENGINE.echo(false, false) warnings = $stdout.string $stdout = STDOUT end if return_warnings puts " Got #{warnings.size} warnings, with result #{result.inspect[0...100]}" if @@debugging return result, warnings.lines.map { |w| w.strip.chomp } else puts " Result: #{result.inspect[0...100]}" if @@debugging return result end end end |
.cars ⇒ Object
14 15 16 17 |
# File 'lib/rust.rb', line 14 def self.cars @@datasets[:cars] = Rust.exclusive { Rust['cars'] } unless @@datasets[:cars] return @@datasets[:cars] end |
.check_library(name) ⇒ Object
Checks if the given name library can be used. Returns true if it is available, false otherwise.
140 141 142 143 144 145 |
# File 'lib/rust/core/rust.rb', line 140 def self.check_library(name) self.exclusive do result, _ = self._pull("require(\"#{name}\", character.only = TRUE)", true) return result end end |
.debug ⇒ Object
Sets the debug mode. Any call to R will be written on the standard output.
25 26 27 |
# File 'lib/rust/core/rust.rb', line 25 def self.debug @@debugging = true end |
.debug? ⇒ Boolean
Checks if the debug mode is active.
32 33 34 |
# File 'lib/rust/core/rust.rb', line 32 def self.debug? return @@debugging end |
.exclusive ⇒ Object
Runs the given block with a mutex. It is mandatory to run any R command with this method.
39 40 41 42 43 44 45 46 47 |
# File 'lib/rust/core/rust.rb', line 39 def self.exclusive result = nil CLIENT_MUTEX.synchronize do @@in_client_mutex = true result = yield @@in_client_mutex = false end return result end |
.formula(left_part, right_part) ⇒ Object
228 229 230 |
# File 'lib/rust/core/types/language.rb', line 228 def self.formula(left_part, right_part) Formula.new(left_part, right_part) end |
.function(name) ⇒ Object
224 225 226 |
# File 'lib/rust/core/types/language.rb', line 224 def self.function(name) Function.new(name) end |
.help!(mod = nil) ⇒ Object
Ask for help on a given mod.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rust/core/rust.rb', line 193 def self.help!(mod = nil) unless mod puts "You have the following modules:" Rust.constants.map { |c| Rust.const_get(c) }.select { |c| c.class == Module }.each do |mod| puts "\t- #{mod}" end puts "Run \"help! {module}\" for more detailed information about the module" else if mod.methods.include?(:help!) mod.help! else puts "Sorry, no help available for #{mod}" end end end |
.install_library(name, github = false) ⇒ Object
Installs the given name library and its dependencies. github indicates whether the package is in GitHub.
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rust/core/rust.rb', line 162 def self.install_library(name, github = false) self.prerequisite("remotes") if github self.exclusive do if github self._eval("remotes::install_github(\"#{name}\", dependencies=TRUE)") else self._eval("install.packages(\"#{name}\", dependencies = TRUE)") end end return nil end |
.iris ⇒ Object
19 20 21 22 |
# File 'lib/rust.rb', line 19 def self.iris @@datasets[:iris] = Rust.exclusive { Rust['iris'] } unless @@datasets[:iris] return @@datasets[:iris] end |
.load_library(name) ⇒ Object
Loads the given name library.
150 151 152 153 154 155 156 |
# File 'lib/rust/core/rust.rb', line 150 def self.load_library(name) self.exclusive do self._eval("library(\"#{name}\", character.only = TRUE)") end return nil end |
.prerequisite(library, github = false) ⇒ Object
Installs the library library if it is not available and loads it. github indicates whether the package appears in GitHub.
180 181 182 183 184 185 186 187 188 |
# File 'lib/rust/core/rust.rb', line 180 def self.prerequisite(library, github = false) full_library = library library = library.split("/").last if github unless self.check_library(library) self.install_library(full_library, github) end self.load_library(library) end |
.toothgrowth ⇒ Object
9 10 11 12 |
# File 'lib/rust.rb', line 9 def self.toothgrowth @@datasets[:ToothGrowth] = Rust.exclusive { Rust['ToothGrowth'] } unless @@datasets[:ToothGrowth] return @@datasets[:ToothGrowth] end |
.variable(variable) ⇒ Object
220 221 222 |
# File 'lib/rust/core/types/language.rb', line 220 def self.variable(variable) Variable.new(variable) end |
.verbatim(expression) ⇒ Object
216 217 218 |
# File 'lib/rust/core/types/language.rb', line 216 def self.verbatim(expression) Verbatim.new(expression) end |