R-tools: Difference between revisions

From Opasnet
Jump to navigation Jump to search
m (→‎Parameters: showcode instruction update)
Line 86: Line 86:
name:c|description:Fruit|type:selection|options:'banana';Banana;'orange';Orange;'kiwi';Kiwi|default:'kiwi'|
name:c|description:Fruit|type:selection|options:'banana';Banana;'orange';Orange;'kiwi';Kiwi|default:'kiwi'|
name:d|description:Cars|type:checkbox|options:1;Ferrari;2;Porsche;3;Lamborghini|default:1;3
name:d|description:Cars|type:checkbox|options:1;Ferrari;2;Porsche;3;Lamborghini|default:1;3
name:e|description:Secrets|type:Hidden|options:1;Govermernt secrets;2;Personal secrets;|default:1;2
">
">
# Print out the variable values
# Print out the variable values
Line 92: Line 93:
c
c
d
d
e
</rcode>
</rcode>



Revision as of 13:14, 3 July 2012



R-Tools is a MediaWiki extension that allows registered Opasnet users to run R code directly from a wiki page.

Question

How should R-based modelling be performed in a MediaWiki environment?

Answer

Libraries

Opasnet R software has some additional libraries installed e.g. for plotting and connecting to external databases. More libraries can be installed on request with proper reasons. List of currently installed libraries:

Usage

R-tools uses <rcode> tags. Any R code written between these tags can be run from wiki page using the "Run code"-button. Press the button and new window (or tab) opens up showing the status and the results of the run in real time. An example code that counts one plus one using R:

<rcode>
1+1
</rcode>

Parameters

Some optional parameters can be given to <rcode> tag as described below.

  • label - Label for the "Run code" button.
  • graphics - If the code is meant to draw any graphs then define graphics to '1'.
  • showcode - By default the code is hidden under 'Show code'-link (value 0). If this parameter is defined as '1', then the code is shown when the page is entered. If the value is '-1', then the code cannot be shown by any means (no visible link).
  • variables - Allows user to input values for named variables in R code. Inputs will be displayed in a box below the actual R code in wiki page. Variables syntax is described in its own chapter on this page.
  • include - Includes external <rcode> block to code. Code needs to be identified by "name" parameter before it can be included. More than one code blocks can be included. The syntax is "page:<wiki page>|name:<code name>|...".

Variables syntax (legacy way)

Each variable needs three attributes separated with | (pipe) character. These attributes (in correct order) are variable name in code,variable description in wiki page and default value. Variable triplets must also be separated with pipe. An example R code with two defined attributes calculates the area of a rectangle:

<rcode variables="width|Width of the rect|10|height|Height of the rect|10">
# The area of the rect is
width *  height
</rcode>

Note that variable default value can also be a string or even an array (e.g. c(1,2,3) or 'Some name').

Variables syntax (new way with more possibilities)

Each variable can have a set of attributes. Attributes must be given as name/value pairs, separated with colon (:). Each variable must have at least one attribute, which is called "name" (variable name in R code). Optionally "description" attribute can be defined to give users more information about the variable, and "default" can be used for giving variable some predefined input. Order in which the attributes are given is not relevant. All variables are considered as text type by default, providing users a input field for writing. There are other possible types too (date, select, checkbox...) and the list of possibilities may grow longer in future. Each type of a variable have custom set of attributes. Variable type can be defined by giving it "type" attribute. Different types for variables are described in their own chapter on this page. Each attribute must be separated with a vertical bar (aka pipe) character. Example code of two variables with default values:

<rcode variables="name:width|default:10|name:height|description:Rect height|default:10">
# The area of the rect is
width *  height
</rcode>

Variable types

This chapter describes all the implemented variable types.

default

This is the default type if "type" attribute is not present. Renders a text field where users can input any text. Given input will be assigned to R variable as is, making it possible to use this type for inputting strings, numbers, vectors etc. Optionally "default" attribute can be given to insert predefined value to text field.

text

Otherwise equal to the default type but the given value will be encapsulated with quotes when assigned. This forces the variable to be string in R code.

date

Renders three select-elements for choosing a day, month and year. Given date will be assigned to R variable as date (as.Date). Optionally "default" attribute can be used for setting default date. Default date must be given in ISO 8601 format (YYYY-MM-DD).

datetime

Like the date type but extended with three select-elements (hours, minutes and seconds) for time. Default must be given in ISO 8601 format (YYYY-MM-DD hh:mm:ss).

select

Renders a drop down element with options defined by "options" attribute. Options must be given as value/name pairs separated with semicolon ('value1';Name one;'value2';Name two...). Selected value will be assigned into R variable. NOTE: values can be strings, numbers or even vectors. Default selection can be defined with "default" attribute.

checkbox

Renders a group of checkbox elements defined by "options" attribute. Options must be given as value/name pairs separated with semicolon ('value1';Name one;'value2';Name two...). Selected values will be assigned into R variable as vector. NOTE: values can be strings, numbers or even vectors. Default selections can be defined with "default" attribute (separate multiple values with semicolon).

hidden

Renders nothing, but still assigns "default" attribute's value to variable. This kind of variables can be very helpful when there is need to assign variables for included code blocks.

Example code

The example code below prints out the user selected values for variables a,b,c and d. Press the 'Run code'-button to execute.

A
Al

+ Show code

Rationale

See also

Keywords

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>