Skip to content

Commit 65f3c21

Browse files
committed
Add nano_init() for setting global options
1 parent 4d8b45f commit 65f3c21

File tree

13 files changed

+241
-49
lines changed

13 files changed

+241
-49
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export(listen)
5252
export(logging)
5353
export(messenger)
5454
export(nano)
55+
export(nano_init)
5556
export(ncurl)
5657
export(nng_error)
5758
export(nng_version)

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
* New `stream()` interface exposes low-level byte stream functionality in the NNG library, intended for communicating with non-NNG endpoints, including but not limited to websocket servers.
66
* `ncurl()` adds an 'async' option to perform HTTP requests asynchronously, returning immediately with a 'recvAio'. Also adds explicit arguments for HTTP method, headers (which takes a named list or character vector) and request data.
77
* New `messenger()` function implements a multi-threaded console-based messaging system using NNG's scalability protocols.
8-
8+
* New `nano_init()` function intended to be called immediately after package load to set how to handle warnings, with a default of immediate printing of warnings (settings automatically reverted upon unload).
9+
910
#### Updates
1011

11-
* Behavioural change: messages have been upgraded to warnings across the package to allow for enhanced reporting of the originating call e.g. via `warnings()` and flexibility in handling by setting the value of 'warn' using `options()`. `options(warn = 1)` is set upon package load for immediate printing of warnings (and reverted upon unload).
12+
* Behavioural change: messages have been upgraded to warnings across the package to allow for enhanced reporting of the originating call e.g. via `warnings()` and flexibility in handling via setting `options()`.
1213
* Unified `send()` and `recv()` functions, and their asynchronous counterparts `send_aio()` and `recv_aio()`, are now S3 generics and can be used across Sockets, Contexts and Streams.
1314
* Revised 'block' argument for `send()` and `recv()` now allows an integer value for setting a timeout.
1415
* `send_ctx()` and `recv_ctx()` are deprecated and will be removed in a future package version - the methods for `send()` and `recv()` should be used instead.

R/nanonext-package.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#'
1212
#' @section Usage notes:
1313
#'
14+
#' Call \code{\link{nano_init}} after package load to set global options.
15+
#' The default by calling \code{nano_init()} with no arguments will cause
16+
#' generated warnings to print immediately as they occur.
17+
#'
1418
#' \{nanonext\} offers 2 equivalent interfaces: an object-oriented interface,
1519
#' and a functional interface.
1620
#'
@@ -128,16 +132,15 @@ NULL
128132
info = TRUE,
129133
FALSE)
130134
.logging. <<- .logging.
131-
.warn. <- getOption("warn")
132-
.warn. <<- .warn.
133-
options(warn = 1L)
134135
invisible()
135136
}
136137

137138
.onUnload <- function(libpath) {
138-
options(warn = .warn.)
139+
if (!is.null(warn <- getOption("nanonext.original.warn"))) {
140+
options(warn = warn)
141+
options(nanonext.original.warn = NULL)
142+
}
139143
invisible()
140144
}
141145

142-
.warn. <- NULL
143146
.logging. <- FALSE

R/utils.R

Lines changed: 102 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,31 @@ nng_version <- function() .Call(rnng_version)
3333
#'
3434
#' @section Warnings:
3535
#'
36-
#' A warning is generated every time an 'errorValue' is returned. The package
37-
#' changes the behaviour of warnings by setting \code{options(warn = 1)}
38-
#' upon package load. This prints the warning to the console as soon as it
39-
#' occurs, rather than the default of waiting for the top–level function to
40-
#' return.
36+
#' A warning is generated every time an 'errorValue' is returned.
4137
#'
42-
#' See \code{\link{warning}} or the entry for 'warn' in \code{\link{options}}
43-
#' for further details. The original option value for 'warn' is reverted upon
44-
#' package unload.
38+
#' \code{\link{nano_init}} may be used to set the value of option 'warn' and
39+
#' automatically reverts it upon package unload. The default, applied by
40+
#' calling \code{nano_init()} with no arguments, is 'immediate', which prints
41+
#' warnings as they occur.
42+
#'
43+
#' Further options for warnings may be set manually via \code{\link{options}}:
44+
#'
45+
#' \itemize{
46+
#'
47+
#' \item{warning.expression} { - an R code expression to be called if a
48+
#' warning is
49+
#' generated, replacing the standard message. If non-null it is called
50+
#' irrespective of the value of option warn.}
51+
#'
52+
#' \item{warning.length} { - sets the truncation limit in bytes for error and warning
53+
#' messages. A non-negative integer, with allowed values 100...8170, default
54+
#' 1000.}
55+
#'
56+
#' \item{nwarnings} { - the limit for the number of warnings kept when warn = 0,
57+
#' default 50. This will discard messages if called whilst they are being
58+
#' collected. If you increase this limit, be aware that the current
59+
#' implementation pre-allocates the equivalent of a named list for them.}
60+
#' }
4561
#'
4662
#' @examples
4763
#' nng_error(1L)
@@ -83,15 +99,31 @@ is_nul_byte <- function(x) identical(x, as.raw(0L))
8399
#'
84100
#' @section Warnings:
85101
#'
86-
#' A warning is generated every time an 'errorValue' is returned. The package
87-
#' changes the behaviour of warnings by setting \code{options(warn = 1)}
88-
#' upon package load. This prints the warning to the console as soon as it
89-
#' occurs, rather than the default of waiting for the top–level function to
90-
#' return.
102+
#' A warning is generated every time an 'errorValue' is returned.
103+
#'
104+
#' \code{\link{nano_init}} may be used to set the value of option 'warn' and
105+
#' automatically reverts it upon package unload. The default, applied by
106+
#' calling \code{nano_init()} with no arguments, is 'immediate', which prints
107+
#' warnings as they occur.
108+
#'
109+
#' Further options for warnings may be set manually via \code{\link{options}}:
110+
#'
111+
#' \itemize{
91112
#'
92-
#' See \code{\link{warning}} or the entry for 'warn' in \code{\link{options}}
93-
#' for further details. The original option value for 'warn' is reverted upon
94-
#' package unload.
113+
#' \item{warning.expression} { - an R code expression to be called if a
114+
#' warning is
115+
#' generated, replacing the standard message. If non-null it is called
116+
#' irrespective of the value of option warn.}
117+
#'
118+
#' \item{warning.length} { - sets the truncation limit in bytes for error and warning
119+
#' messages. A non-negative integer, with allowed values 100...8170, default
120+
#' 1000.}
121+
#'
122+
#' \item{nwarnings} { - the limit for the number of warnings kept when warn = 0,
123+
#' default 50. This will discard messages if called whilst they are being
124+
#' collected. If you increase this limit, be aware that the current
125+
#' implementation pre-allocates the equivalent of a named list for them.}
126+
#' }
95127
#'
96128
#' @examples
97129
#' is_error_value(1L)
@@ -100,6 +132,60 @@ is_nul_byte <- function(x) identical(x, as.raw(0L))
100132
#'
101133
is_error_value <- function(x) inherits(x, "errorValue")
102134

135+
#' Nanonext Initialise
136+
#'
137+
#' Initialise global options - intended to be called immediately after package load.
138+
#'
139+
#' @param warn [default 'immediate'] character string defining how to treat
140+
#' warnings generated by the package. 'immediate' to print warnings as they
141+
#' occur, 'deferred' to print warnings when evaluation returns to the top
142+
#' level, 'error' to upgrade all warnings to errors (stops execution), and
143+
#' 'none' to ignore all warnings.
144+
#'
145+
#' @return Integer \code{code} applied to \code{options(warn = code)} (invisibly).
146+
#'
147+
#' @section Warnings:
148+
#'
149+
#' A warning is generated every time an 'errorValue' is returned.
150+
#'
151+
#' This funnction sets the option 'warn' to the appropriate value and
152+
#' automatically reverts it upon package unload. The default, applied by
153+
#' calling \code{nano_init()} with no arguments, is 'immediate', which
154+
#' prints warnings as they occur.
155+
#'
156+
#' Further options for warnings may be set manually via \code{\link{options}}:
157+
#'
158+
#' \itemize{
159+
#'
160+
#' \item{warning.expression} { - an R code expression to be called if a
161+
#' warning is
162+
#' generated, replacing the standard message. If non-null it is called
163+
#' irrespective of the value of option warn.}
164+
#'
165+
#' \item{warning.length} { - sets the truncation limit in bytes for error and warning
166+
#' messages. A non-negative integer, with allowed values 100...8170, default
167+
#' 1000.}
168+
#'
169+
#' \item{nwarnings} { - the limit for the number of warnings kept when warn = 0,
170+
#' default 50. This will discard messages if called whilst they are being
171+
#' collected. If you increase this limit, be aware that the current
172+
#' implementation pre-allocates the equivalent of a named list for them.}
173+
#' }
174+
#'
175+
#' @export
176+
#'
177+
nano_init <- function(warn = c("immediate", "deferred", "error", "none")) {
178+
179+
cand <- c("immediate", "deferred", "error", "none")
180+
warn <- switch(pmatch(warn[1L], cand, nomatch = NULL),
181+
1L, 0L, 2L, -1L)
182+
is.null(warn) && stop("'warn' should be one of ", paste(cand, collapse = ", "))
183+
if (is.null(getOption("nanonext.original.warn"))) options(nanonext.original.warn = getOption("warn"))
184+
options(warn = warn)
185+
invisible(warn)
186+
187+
}
188+
103189
#' Logging Level
104190
#'
105191
#' This function is deprecated.

README.Rmd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ install.packages("nanonext", repos = "https://shikokuchuo.r-universe.dev")
7171

7272
### Interfaces
7373

74+
Call `nano_init()` after package load to set global options. The default by calling `nano_init()` with no arguments will cause generated warnings to print immediately as they occur.
75+
7476
{nanonext} offers 2 equivalent interfaces: an object-oriented interface, and a functional interface.
7577

7678
#### Object-oriented Interface
@@ -84,6 +86,8 @@ Create nano objects:
8486

8587
```{r example}
8688
library(nanonext)
89+
nano_init()
90+
8791
nano1 <- nano("req", listen = "inproc://nanonext")
8892
nano2 <- nano("rep", dial = "inproc://nanonext")
8993
```
@@ -110,6 +114,8 @@ Create sockets:
110114

111115
```{r example2}
112116
library(nanonext)
117+
nano_init()
118+
113119
socket1 <- socket("push", listen = "tcp://127.0.0.1:5555")
114120
socket2 <- socket("pull", dial = "tcp://127.0.0.1:5555")
115121
```

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ install.packages("nanonext", repos = "https://shikokuchuo.r-universe.dev")
7272

7373
### Interfaces
7474

75+
Call `nano_init()` after package load to set global options. The default
76+
by calling `nano_init()` with no arguments will cause generated warnings
77+
to print immediately as they occur.
78+
7579
{nanonext} offers 2 equivalent interfaces: an object-oriented interface,
7680
and a functional interface.
7781

@@ -90,6 +94,8 @@ Create nano objects:
9094

9195
``` r
9296
library(nanonext)
97+
nano_init()
98+
9399
nano1 <- nano("req", listen = "inproc://nanonext")
94100
nano2 <- nano("rep", dial = "inproc://nanonext")
95101
```
@@ -129,6 +135,8 @@ Create sockets:
129135

130136
``` r
131137
library(nanonext)
138+
nano_init()
139+
132140
socket1 <- socket("push", listen = "tcp://127.0.0.1:5555")
133141
socket2 <- socket("pull", dial = "tcp://127.0.0.1:5555")
134142
```
@@ -370,7 +378,7 @@ aio
370378
#> < recvAio >
371379
#> - $data for message data
372380
aio$data |> str()
373-
#> num [1:100000000] -0.257 -0.142 -0.473 0.718 -1.426 ...
381+
#> num [1:100000000] 0.946 -0.658 -1.397 0.983 0.339 ...
374382
```
375383

376384
As `call_aio()` is blocking and will wait for completion, an alternative
@@ -505,11 +513,11 @@ ncurl("http://httpbin.org/headers")
505513
#> [1] 7b 0a 20 20 22 68 65 61 64 65 72 73 22 3a 20 7b 0a 20 20 20 20 22 48 6f 73
506514
#> [26] 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22
507515
#> [51] 58 2d 41 6d 7a 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31
508-
#> [76] 2d 36 32 34 65 30 30 36 64 2d 37 38 30 37 36 62 37 63 31 35 65 64 66 37 65
509-
#> [101] 37 31 64 36 62 32 34 63 35 22 0a 20 20 7d 0a 7d 0a
516+
#> [76] 2d 36 32 34 65 61 39 38 66 2d 35 31 39 65 31 39 66 61 31 38 61 37 38 31 31
517+
#> [101] 31 31 66 62 34 36 62 35 36 22 0a 20 20 7d 0a 7d 0a
510518
#>
511519
#> $data
512-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-624e006d-78076b7c15edf7e71d6b24c5\"\n }\n}\n"
520+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-624ea98f-519e19fa18a781111fb46b56\"\n }\n}\n"
513521
```
514522

515523
For advanced use, supports additional HTTP methods such as POST or PUT.
@@ -524,7 +532,7 @@ res
524532
#> - $raw for raw message
525533

526534
call_aio(res)$data
527-
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-624e006d-06173bcd5b43d5622303afe5\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"78.145.225.121\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
535+
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-624ea98f-641a91ed52b661a054b5065c\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"79.173.189.204\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
528536
```
529537

530538
In this respect, it may be used as a performant and lightweight method

man/is_error_value.Rd

Lines changed: 25 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/nano_init.Rd

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/nanonext-package.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)