---
title: "Miscellaneous Functions for 'SciViews::R'"
author: "Philippe Grosjean"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 3
    fig_caption: yes
vignette: >
  %\VignetteIndexEntry{Miscellaneous Functions for 'SciViews::R'}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(svMisc)
```

The {svMisc} package contains a large collection of functions that are useful in the context of (G)UI (Graphical) User Interface development, and also, of more general usage. Here is a series of functions you should look at.

## Help

In R, you access man pages for the various R objects with `help(topic)`, or `?topic`. But, if `topic` is not found, these function do not return a very useful information. For instance, if you want to make a [Kalman filtering](https://doi.org/10.18637/jss.v039.i02) in R, you may be inclined to search for the topic `kalman`

```{r}
?kalman
```

OK, it is suggested to use `??` to search the documentation for `kalman`. However, using `about()` instead immediately produces a more useful result:

```{r}
library(svMisc)
about("kalman")
```

... and it also provides a list of potential man pages that could interest you. In case the topic is found, `about()` does the same as `help()` and shows the page (try with `about("log")`, for instance). If you still have not found what you are looking for, you could try to search on the Web by using `search_web()`.

You may also be interested by `is_help()` that indicates if an object is associated with a man page, and if it has a running example.

## Packages

In R, the use of `library()` to load a **package** is very confusing. Given the number of questions raised about it, one could consider another function to load R packages in memory. `svMisc` proposes `package()`.  That function loads one or several R packages as silently as possible and it returns `TRUE` only if **all** the packages are loaded. Otherwise, the list of missing packages is recorded^[The list of missing packages is written in a variable named `.packages_to_install` located in the `SciViews:TempEnv` environment], and one could simply issue `Install()` to install them. This is indeed a semi-automatic installation mechanisms for R packages. The UseR still masters the process, but it is more straightforward.

## Analyses in batch and show progression

If you need to perform an analysis in batch mode, you may be happy with `batch()` and `progress()`. The first function runs a function sequentially on all items **allowing for an informative message in case of failure**. Also, `batch()` provides a mechanism to recover from the error, so that following items in the list are also analyzed. Indeed, if you use a simple `for()` loop or `applyXXX()` functions, the execution is stopped at the first error encountered. Imagine 500 items to process, and an error that appears at the second one... it leaves you 498 items unanalyzed! allows to continue to the next item.

The example shows a fake batch process of files, which fails randomly. Here is the function to run sequentially:

```{r}
fake_process <- function(file) {
  message("Processing ", file, "...")
  flush.console()
  Sys.sleep(0.5)
  if (runif(1) > 0.7) {# Fail
    warning("fake_process was unable to process ", file)
    invisible(FALSE)
  } else invisible(TRUE)
}
```

The key aspect here is that you function, instead of using `stop()` must use `warning()` and return `FALSE`. Otherwise, in case of success, it should return `TRUE`. Then, calling your function on a series of objects is straightforward:

```{r}
# Run it in batch mode on ten items
batch(paste0("file", 1:10), fake_process)
```

In case an error occurred, the information is recorded i, `.last.batch`:

```{r}
.last.batch
```

The `items` and `ok` attributes are also available from that object for further inspection and action.

If you run `batch()` in R, you noted also the `progress()`ion message that appeared. Indeed the `progress()` function allows to display such a message, either as a text at the R console, or in a dialog box. There are many different forms, see the man page `?progress`. for instance, here is a progress bar in percent, stopped at 75% () you need to call `progress()` with a value higher than `max.value =` to dismiss it):

```{r}
for (i in 0:75) {
  progress(i, progress.bar = TRUE)
  # Some process here...
}
```

## Subsettable functions

The `$` operator is not applicable on functions. It is not meaningful in that context. Yet, it may be convenient to use it in certain conditions. From the example of `?subsettable`:

```{r}
foo <- structure(function(x, type = c("histogram", "boxplot"), ...) {
  type <- match.arg(type, c("histogram", "boxplot"))
  switch(type,
    histogram = hist(x, ...),
    boxplot = boxplot(x, ...),
    stop("unknow type")
  )
}, class = c("function", "subsettable_type"))
foo

# This function can be used as usual:
foo(rnorm(50), type = "histogram")
# ... but also this way:
foo$histogram(rnorm(50))
foo$boxplot(rnorm(50))
```

## Capture and parse R code

The `capture.output()` function from the 'utils' package can capture output usually send to the R console, but it does so in an imperfect way. If you want to capture output *exactly*  as it would appear at the R console, you could use `capture_all()`:

```{r}
captured <- capture_all(parse_text('1:2 + 1:3'), split = FALSE)
captured
```

Only the prompt is changed to `:>`. You can use that content, or print it somewhere, for instance:

```{r}
writeLines(captured)
```

The `parse_text()` function parse one or more character strings exactly as if they were commands entered at the R prompt:

```{r}
parse_text(c("1 + 1", "log(10)"))
```

... and for an incomplete expression:

```{r}
parse_text("log(")
```

The `source_clipboard()` source code directly from the clipboard. All these functions form the basis to simulate an R console in a different context (a console widget in your own GUI). You can combine this with `to_rjson()`/`eval_rjson` to encode and decode R objects on both sides of a pipeline between the R process and your GUI.

## Encode/decode R objects in Rjson

Rjson is a version of [JSON](https://www.json.org/json-en.html) that allows to encore and decode rapidly almost all R objects. From the example at `?to_rjson`:

```{r}
# A complex R object
# Note: we round doubles to 14 digits because precision is lost in the process
obj <- structure(list(
  a = as.double(c(1:5, 6)),
  LETTERS,
  c = c(c1 = 4.5, c2 = 7.8, c3 = Inf, c4 = -Inf, NA, c6 = NaN),
  c(TRUE, FALSE, NA),
  e = factor(c("a", "b", "a")),
  f = 'this is a "string" with quote',
  g = matrix(round(rnorm(4), 14), ncol = 2),
  `h&$@` = list(x = 1:3, y = round(rnorm(3), 14),
    fact = factor(c("b", "a", "b"))),
  i = Sys.Date(),
  j = list(1:5, y = "another item")),
  comment = "My comment",
  anAttrib = 1:10,
  anotherAttrib = list(TRUE, y = 1:4))

# Convert to RJSON
(rjson1 <- to_rjson(obj, attributes = TRUE))
# Get back an R object from Rjson
(obj2 <- eval_rjson(rjson1))
# Is it identical to obj?
identical(obj, obj2)
```

## Get system file or directory

There are several different functions in R to access system files, or files inside R packages: `R.home()`, `system.file()`, `Sys.which()`, `tempdir()`. The `system_dir()` and `system_file()` functions centralize their functionalities. For instance:

- Get the temporary directory used by this R process

```{r}
system_dir("temp")
```

- Get the system temporary directory

```{r}
system_dir("sysTemp")
```

- Get the home directory of the current user

```{r}
system_dir("user")
```

- Get the R home directory

```{r}
system_dir("home")
```

- Get the path to an executable

```{r}
system_dir("zip", exec = TRUE)
```

- Get the file of that executable

```{r}
system_file("zip", exec = TRUE)
```


- Get the root directory of a package

```{r}
system_dir(package = "stats")
```

- Get a file from a package

```{r}
system_file("help", "AnIndex", package = "splines")
```

There are other possibilities. See `?system_dir`. You may also be interested by `file_edit()` that allows to create and edit a text file from a template.

## Various information functions

- `compare_r_version()` conveniently compares the current R version with a specified one. It returns 1 if it is newer, 0, if it is equal and -1 if it is older.

```{r}
compare_r_version("5.6.0") # Probably older
```

```{r}
compare_r_version("0.6.0") # Probably newer
```

- Check the environment:

```{r}
is_win() # Windows?
is_mac() # MacOS?
is_rgui() # Is it RGui under Windows?
is_sdi() # Is RGui run in SDI mode (separate windows)?
is_rstudio() # Is it RStudio?
is_rstudio_desktop() # RStudio desktop?
is_rstudio_server() # RStudio server?
is_jgr() # Is R running under JGR?
```

## Miscellaneous

- Make sure a vector is of a defined mode and length (possibly by applying recycling rule) using `def()`:

```{r}
def(0:2, mode = "logical", length.out = 5) # logical, size 5
```

- Get a nicely formatted `args()` (see `?arg_tips` for other functions to get short textual information about functions):

```{r}
args_tip("ls")
```

- Get the name of an (unused) temporary variable:

```{r}
temp_var("my_var")
```

- Manage a temporary environment attached to the search path using `TempEnv()` and the `temp_XXX()` functions. The **temporary_environment** vignette gives more details on this series of functions.

```{r}
search()
# Assign a variable in a temporary environment
assign_temp("my_var", 1:5)
# The environment is named SciViews:TempEnv
search()
# Get the variable
get_temp("my_var")
# List variables in the temporary environment
ls(envir = TempEnv())
# Delete the variable
rm_temp("my_var")
```

