@@ -99,6 +99,7 @@ check_tskit_py <- function(object, stop = FALSE) {
9999 }
100100}
101101
102+ # INTERNAL
102103# @title Validating logical args
103104# @param value logical from the argument
104105# @param name character of the argument
@@ -109,6 +110,174 @@ validate_logical_arg <- function(value, name) {
109110 }
110111}
111112
113+ # INTERNAL
114+ # @title Validating integer scalar args
115+ # @param value integer from the argument
116+ # @param name character of the argument
117+ # @param minimum lower bound
118+ # @return No return value; called for side effects.
119+ validate_integer_scalar_arg <- function (value , name , minimum = NULL ) {
120+ if (
121+ is.null(value ) ||
122+ ! is.integer(value ) ||
123+ length(value ) != 1L ||
124+ is.na(value ) ||
125+ (! is.null(minimum ) && value < minimum )
126+ ) {
127+ if (is.null(minimum )) {
128+ stop(name , " must be a non-NA integer scalar!" )
129+ }
130+ if (identical(minimum , 0L )) {
131+ stop(name , " must be a non-NA zero or positive integer scalar!" )
132+ }
133+ stop(name , " must be a non-NA integer scalar >= " , minimum , " !" )
134+ }
135+ }
136+
137+ # INTERNAL
138+ # @title Validating row indexes
139+ # @param index integer row index (0-based)
140+ # @param name character of the argument
141+ # @param allow_null logical
142+ # @return No return value; called for side effects.
143+ validate_row_index <- function (
144+ index ,
145+ name = " index" ,
146+ allow_null = FALSE
147+ ) {
148+ if (allow_null && is.null(index )) {
149+ return (invisible (NULL ))
150+ }
151+ validate_integer_scalar_arg(index , name , minimum = 0L )
152+ }
153+
154+ # INTERNAL
155+ # @title Validating optional numeric vectors with no missing values
156+ # @param value numeric vector or \code{NULL}
157+ # @param name character of the argument
158+ # @return No return value; called for side effects.
159+ validate_optional_numeric_vector_arg <- function (value , name ) {
160+ if (! is.null(value ) && (! is.numeric(value ) || anyNA(value ))) {
161+ stop(name , " must be NULL or a numeric vector with no NA values!" )
162+ }
163+ }
164+
165+ # INTERNAL
166+ # @title Validating optional integer vectors with no missing values
167+ # @param value integer vector or \code{NULL}
168+ # @param name character of the argument
169+ # @return No return value; called for side effects.
170+ validate_optional_integer_vector_arg <- function (value , name ) {
171+ if (! is.null(value ) && (! is.integer(value ) || anyNA(value ))) {
172+ stop(name , " must be NULL or an integer vector with no NA values!" )
173+ }
174+ }
175+
176+ # INTERNAL
177+ # @title Validating numeric scalar args
178+ # @param value numeric scalar
179+ # @param name character of the argument
180+ # @param allow_null logical
181+ # @param allow_nan logical
182+ # @return No return value; called for side effects.
183+ validate_numeric_scalar_arg <- function (
184+ value ,
185+ name ,
186+ allow_null = FALSE ,
187+ allow_nan = FALSE
188+ ) {
189+ if (is.null(value )) {
190+ if (allow_null ) {
191+ return (invisible (NULL ))
192+ }
193+ stop(name , " must be a non-NA numeric scalar!" )
194+ }
195+ if (! is.numeric(value ) || length(value ) != 1L ) {
196+ if (allow_null && allow_nan ) {
197+ stop(name , " must be NaN, NULL, or a non-NA numeric scalar!" )
198+ }
199+ stop(name , " must be a non-NA numeric scalar!" )
200+ }
201+ if (allow_nan ) {
202+ if (! is.na(value ) || is.nan(value )) {
203+ return (invisible (NULL ))
204+ }
205+ stop(name , " must be NaN, NULL, or a non-NA numeric scalar!" )
206+ }
207+ if (is.na(value )) {
208+ stop(name , " must be a non-NA numeric scalar!" )
209+ }
210+ }
211+
212+ # INTERNAL
213+ # @title Validating character scalar args
214+ # @param value character scalar
215+ # @param name character of the argument
216+ # @return No return value; called for side effects.
217+ validate_character_scalar_arg <- function (value , name ) {
218+ if (
219+ is.null(value ) ||
220+ ! is.character(value ) ||
221+ length(value ) != 1L ||
222+ is.na(value )
223+ ) {
224+ stop(name , " must be a length-1 non-NA character string!" )
225+ }
226+ }
227+
228+ # INTERNAL
229+ # @title Validating nullable integer scalar args with sentinel minimum
230+ # @param value integer scalar or \code{NULL}
231+ # @param name character of the argument
232+ # @param minimum integer scalar sentinel minimum
233+ # @return No return value; called for side effects.
234+ validate_nullable_integer_scalar_arg <- function (
235+ value ,
236+ name ,
237+ minimum = - 1L
238+ ) {
239+ if (is.null(value )) {
240+ return (invisible (NULL ))
241+ }
242+ if (
243+ ! is.integer(value ) ||
244+ length(value ) != 1L ||
245+ is.na(value ) ||
246+ value < minimum
247+ ) {
248+ stop(
249+ name ,
250+ " must be " ,
251+ minimum ,
252+ " , NULL, or a non-NA integer scalar!"
253+ )
254+ }
255+ }
256+
257+ # INTERNAL
258+ # @title Validating metadata argument and possibly converting it to raw
259+ # @param metadata \code{NULL}, character, or raw argument
260+ # @return \code{NULL} when metadata is \code{NULL} and raw vector otherwise.
261+ validate_metadata_arg <- function (metadata ) {
262+ if (is.null(metadata )) {
263+ return (NULL )
264+ }
265+ if (
266+ is.character(metadata ) &&
267+ length(metadata ) == 1L &&
268+ ! is.na(metadata )
269+ ) {
270+ return (charToRaw(metadata ))
271+ }
272+ if (is.raw(metadata )) {
273+ return (metadata )
274+ }
275+ stop(
276+ " metadata must be NULL, a length-1 non-NA character string, or a raw vector!"
277+ )
278+ }
279+
280+ # INTERNAL
112281# @title Converting load arguments to \code{tskit} bitwise options
113282# @param skip_tables logical
114283# @param skip_reference_sequence logical
0 commit comments