Skip to content

Commit 295bef2

Browse files
committed
improve as.code
1 parent d21b7ca commit 295bef2

14 files changed

+215
-122
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: struct
22
Type: Package
33
Title: Statistics in R Using Class-based Templates
4-
Version: 1.7.0
4+
Version: 1.7.1
55
Authors@R: c(
66
person(
77
c("Gavin","Rhys"),
@@ -49,7 +49,7 @@ Collate:
4949
'resampler_class.R'
5050
'struct.R'
5151
'struct_templates.R'
52-
RoxygenNote: 7.1.1
52+
RoxygenNote: 7.1.2
5353
Depends: R (>= 4.0)
5454
Suggests:
5555
testthat,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(.DollarNames,DatasetExperiment)
34
S3method(.DollarNames,chart)
45
S3method(.DollarNames,iterator)
56
S3method(.DollarNames,metric)

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Changes 1.7.1
2+
+ fix as.code generic
3+
+ improve as.code for all objects
4+
15
Changes in 1.5.3
26
+ fix variable_meta assignment
37

R/DatasetExperiment_class.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,18 @@ setMethod(f = "export_xlsx",
233233
}
234234
)
235235

236+
#' @export
237+
#' @rdname autocompletion
238+
#' @method .DollarNames DatasetExperiment
239+
.DollarNames.DatasetExperiment <- function(x, pattern = "") {
240+
IN = c('data', 'sample_meta','variable_meta','name','description','type',
241+
'libraries','citations','ontology')
242+
return(IN)
243+
}
244+
245+
#' @export
246+
#' @rdname autocompletion
247+
setMethod('.DollarNames','DatasetExperiment',.DollarNames.DatasetExperiment)
248+
249+
250+

R/chart_class.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ setMethod(f = "show",
5353
}
5454
)
5555

56-
# autocompletion, return sample_meta column names
56+
# autocompletion
5757
#' @export
5858
#' @rdname autocompletion
5959
#' @method .DollarNames chart
@@ -63,4 +63,5 @@ setMethod(f = "show",
6363

6464
#' @export
6565
#' @rdname autocompletion
66-
setMethod('.DollarNames','chart',.DollarNames.chart)
66+
setMethod('.DollarNames','chart',.DollarNames.chart)
67+

R/generics.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,15 @@ setGeneric("as.DatasetExperiment",function(obj)standardGeneric("as.DatasetExperi
611611
#' @param start text prepended to the code. Default is "M = "
612612
#' @param mode "compact" will use the least amount of lines, "expanded" will
613613
#' put each object and input on a new line. "neat" will produce an output
614-
#' somewhere between "compact" and "extended".
614+
#' somewhere between "compact" and "expanded".
615+
#' @param quiet TRUE or FALSE to print code to console
615616
#' @return A string of code to reproduce the input object.
616617
#' @export
617618
#' @rdname as.code
618619
#' @examples
619620
#' M = example_model(value_1 = 10)
620621
#' as.code(M)
621-
setGeneric('as.code',function(M,start='M = ',mode='compact')standrdGeneric("as.code"))
622+
setGeneric('as.code',function(M,start='M = ',mode='compact',quiet=FALSE)standardGeneric("as.code"))
622623

623624
#' convert to data.frame
624625
#'

R/iterator_class.R

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ setMethod(f = 'show',
248248
#' @return a string of code to reproduce the iterator
249249
setMethod(f = 'as.code',
250250
signature = c('iterator'),
251-
definition = function(M,start='M = ',mode='compact') {
251+
definition = function(M,start='M = ',mode='compact',quiet=FALSE) {
252252
str=.as_code(M,start,mode)
253253
# get models
254254
m=models(M)
@@ -257,20 +257,24 @@ setMethod(f = 'as.code',
257257
if (is(m,'model_seq') & length(m) > 1) {
258258
if (mode=='expanded') {
259259
str=paste0(str,paste0(paste0(rep(' ',nchar(start)),collapse=''),'(\n'))
260-
str=paste0(str,as.code(m,start=paste0(paste0(rep(' ',nchar(start)+2),collapse='')),mode))
260+
str=paste0(str,as.code(m,start=paste0(paste0(rep(' ',nchar(start)+2),collapse='')),mode,quiet=TRUE))
261261
} else {
262-
str=paste0(str,as.code(m,start=paste0(paste0(rep(' ',nchar(start)),collapse=''),'('),mode))
262+
str=paste0(str,as.code(m,start=paste0(paste0(rep(' ',nchar(start)),collapse=''),'('),mode,quiet=TRUE))
263263
}
264264
if (mode != 'compact') {
265265
str=paste0(str,'\n',paste0(rep(' ',nchar(start)),collapse=''))
266266
}
267267

268268
str=paste0(str,')')
269269
} else {
270-
str=paste0(str,as.code(m,start=paste0(rep(' ',nchar(start)),collapse=''),mode))
270+
str=paste0(str,as.code(m,start=paste0(rep(' ',nchar(start)),collapse=''),mode,quiet=TRUE))
271271
}
272272

273-
return(str)
273+
if (!quiet){
274+
cat(str)
275+
}
276+
277+
invisible(str)
274278
}
275279
)
276280

R/model_class.R

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -232,90 +232,7 @@ setMethod(f = "show",
232232
)
233233

234234

235-
#' @rdname as.code
236-
#' @export
237-
#' @examples
238-
#' M = example_model()
239-
#' as.code(M)
240-
#' @return a string of code to reproduce the model
241-
setMethod(f = 'as.code',
242-
signature = c('model'),
243-
definition = function(M,start = 'M = ',mode = 'compact') {
244-
.as_code(M,start,mode)
245-
}
246-
)
247-
248-
249235

250-
.as_code = function(M,start='M = ',mode = 'compact') {
251-
252-
if (!(mode %in% c('compact','neat','expanded','full'))) {
253-
stop(paste0('unknown option "', mode , '" for as.code()'))
254-
}
255-
str=start
256-
# model object name
257-
str=paste0(str,class(M)[1],'(')
258-
259-
# parameters
260-
P = param_ids(M)
261-
262-
# add seq_in if not equal to data
263-
if (is(M,'model')) {
264-
if (M@seq_in != 'data' | mode=='full') {
265-
P=c(P,'seq_in')
266-
}
267-
}
268-
# add predicted if its not the default
269-
if (is(M,'model')) {
270-
N=new_struct(class(M)[1])
271-
if (length(predicted_name(N))==0) {
272-
N@predicted='cake'
273-
}
274-
275-
if (predicted_name(N) != predicted_name(M) | mode=='full') {
276-
P=c(P,'predicted')
277-
}
278-
}
279-
280-
if (mode != "compact") {
281-
str=paste0(str,'\n')
282-
indent=nchar(start)+2
283-
} else {
284-
indent=(nchar(start)+1)+nchar(class(M)[1])
285-
}
286-
287-
for (p in seq_len(length(P))) {
288-
if (p>1 | mode!="compact") {
289-
str=paste0(str,paste0(rep(' ',indent),collapse=''))
290-
}
291-
292-
if (P[p]=='seq_in') {
293-
str=paste0(str,P[p], ' = "', seq_in(M), '"')
294-
} else if (P[p]=='predicted') {
295-
str=paste0(str,P[p], ' = "', predicted_name(M), '"')
296-
} else if (is(param_value(M,P[p]),'character')) {
297-
str=paste0(str,P[p], ' = "', as.character(param_value(M,P[p])), '"')
298-
} else {
299-
str=paste0(str,P[p], ' = ', as.character(param_value(M,P[p])))
300-
}
301-
302-
303-
if (p==length(P)) {
304-
if (mode=='expanded') {
305-
str=paste0(str,'\n',paste0(rep(' ',indent-2),collapse=''))
306-
}
307-
308-
309-
str=paste0(str,')')
310-
311-
312-
} else {
313-
str=paste0(str,',\n')
314-
}
315-
}
316-
317-
return(str)
318-
}
319236

320237
# autocompletion, return sample_meta column names
321238
#' @export

R/model_list_class.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,18 +329,23 @@ setMethod(f = "model_apply",
329329
#' @return a string of code to reproduce the model sequence
330330
setMethod(f = 'as.code',
331331
signature = c('model_seq'),
332-
definition = function(M,start='M = ',mode='compact') {
332+
definition = function(M,start='M = ',mode='compact',quiet=FALSE) {
333333
str=''
334334
for (i in seq_len(length(M))) {
335335
if (i==1) {
336-
str=paste0(str,as.code(M[i],start=start,mode=mode))
336+
str=paste0(str,as.code(M[i],start=start,mode=mode,quiet=TRUE))
337337
} else {
338-
str=paste0(str,as.code(M[i],start=paste0(rep(' ',nchar(start)),collapse=''),mode))
338+
str=paste0(str,as.code(M[i],start=paste0(rep(' ',nchar(start)),collapse=''),mode,quiet=TRUE))
339339
}
340340
if (i<length(M)) {
341341
str=paste0(str,' +\n')
342342
}
343343
}
344-
return(str)
344+
345+
if (!quiet) {
346+
cat(str)
347+
}
348+
349+
invisible(str)
345350
}
346351
)

0 commit comments

Comments
 (0)