Module: Rust
- Defined in:
- lib/rust/core/rust.rb,
lib/rust.rb,
lib/rust/core/csv.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, MathArray, Matrix, Null, Options, RandomVariable, RandomVariableSlice, RustDatatype, S4Class, Sequence, UniformRandomVariable, Variable
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
name
library 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.
-
.help!(mod = nil) ⇒ Object
Ask for help on a given
mod
. -
.install_library(name) ⇒ Object
Installs the given
name
library and its dependencies. - .iris ⇒ Object
-
.load_library(name) ⇒ Object
Loads the given
name
library. -
.prerequisite(library) ⇒ Object
Installs the
library
library if it is not available and loads it. - .toothgrowth ⇒ 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 |
# File 'lib/rust/core/rust.rb', line 104 def self._rexec(r_command, return_warnings = false) puts "Calling _rexec with command: #{r_command}" if @@debugging 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.
137 138 139 140 141 142 |
# File 'lib/rust/core/rust.rb', line 137 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 |
.help!(mod = nil) ⇒ Object
Ask for help on a given mod
.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/rust/core/rust.rb', line 177 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) ⇒ Object
Installs the given name
library and its dependencies.
158 159 160 161 162 163 164 |
# File 'lib/rust/core/rust.rb', line 158 def self.install_library(name) self.exclusive do self._eval("install.packages(\"#{name}\", dependencies = TRUE)") 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.
147 148 149 150 151 152 153 |
# File 'lib/rust/core/rust.rb', line 147 def self.load_library(name) self.exclusive do self._eval("library(\"#{name}\", character.only = TRUE)") end return nil end |
.prerequisite(library) ⇒ Object
Installs the library
library if it is not available and loads it.
169 170 171 172 |
# File 'lib/rust/core/rust.rb', line 169 def self.prerequisite(library) self.install_library(library) unless self.check_library(library) self.load_library(library) end |