fmt: Auxiliary Formatting Functions

View source: R/formatters.R

fmtR Documentation

Auxiliary Formatting Functions

Description

Auxiliary functions for displaying numeric elements in exercises.

Usage

fmt(x, digits = 2L, zeros = digits < 4L, ...)

round2(x, digits = 0) 

char_with_braces(x)

num_to_tol(x, reltol = 0.0002, min = 0.01, digits = 2)

## S3 method for class 'matrix'
toLatex(object, skip = FALSE, fix = getOption("olat_fix"),
  escape = TRUE, ...)

## S3 method for class 'data.frame'
toLatex(object, rotate = FALSE, pad = " ~ ", align = NULL, row.names = FALSE, ...)

Arguments

x

numeric vector.

digits

integer. Digits that should be used for rounding.

zeros

logical. Should trailing zeros be added?

reltol

numeric. Relative tolerance (relative to correct solution x).

min

numeric. Minimum absolute tolerance.

object

matrix or data frame, respectively.

skip

logical. Should an additional skip be added between rows?

fix

logical. Should an additional empty column be added between all columns? This is a workaround for OLAT that collapses spaces between columns in MathML.

escape

logical. Should LaTeX commands be escaped (as appropriate for Sweave) or not (as appropriate for knit)?

...

passed to format for fmt.

rotate

logical. Should the table be transposed/rotated by 90 degrees?

pad

character for padding columns of the resulting table.

align

character indicating the alignment of the columns. Can either be a single string like "|l|rrr|" or a vector of characters per column. By default numeric columns are right-aligned and character columns are left-aligned.

row.names

logical. Should a column (or row, if rotate = TRUE) with the row names be included?

Details

Various functions that help displaying numerical results in exercises:

The function fmt rounds and adds trailing zeros (by default if digits is lower than 4).

The function round2 does what is known in German as kaufmaennisches Runden (rounding away from zero for trailing 5s).

The function char_with_braces adds parentheses for negative elements (in order to facilitate their display in equations).

The function num_to_tol (or equivalently num2tol) computes the absolute tolerance based on a numeric solution x and a relative tolerance reltol.

The toLatex method sets up a matrix array with parentheses.

Examples

## emulate how students round
## (rather than using the round-to-even strategy R employs)
round2(c(0.005, 0.015), digits = 2)
round(c(0.005, 0.015), digits = 2)

## this is also employed internally in the fmt() formatting function
fmt(c(0.005, 0.015))

## the main purpose of fmt() is that some numeric result can be displayed
## both at high accuracy and then at the rounding that students should do
## (e.g., with 2 or 3 digits)
sol <- runif(1)
fmt(sol, 6)
fmt(sol, 2)

## but fmt() also assures showing a very high numer of significant digits
## (up to 12)
sol <- 123456 + sol
sol
fmt(sol, 6)
fmt(sol, 2)

## and fmt() also takes care of adding trailing zeros (if digits < 4)
fmt(1)
fmt(1, digits = 3)
fmt(1, digits = 6)

## char_with_braces() is for adding parentheses, e.g., before constructing a sum
paste(char_with_braces(-2:2), collapse = " + ")

## for including a matrix in a LaTeX formula
x <- matrix(1:4, ncol = 2)
toLatex(x)
toLatex(x, skip = TRUE)

## for including a data frame as a plain LaTeX tabular (without caption etc.)
d <- data.frame(Label = c("Foo first", "Bar second"), Value = c(12.3, 1234))
toLatex(d, big.mark = ",", nsmall = 2)

## compute absolute tolerances:
## minimum is 0.01
num_to_tol(1)
## but can be larger for larger solutions
num_to_tol(100)

exams documentation built on Nov. 14, 2022, 3:02 p.m.