Skip to content

Commit e334c59

Browse files
committed
end of the day
1 parent 477cdc6 commit e334c59

36 files changed

+655
-248
lines changed

DESCRIPTION

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ Description: This is a description still to be done but to
2525
prevent checks about complaining about to short descriptions
2626
this does not simply read TBD.
2727
Depends:
28-
R (>= 3.0.0)
28+
R (>= 3.0.0),
29+
stringb (>= 0.1.10),
30+
rtext (>= 0.1.10)
2931
License: MIT + file LICENSE
3032
LazyData: TRUE
3133
Imports:
3234
R6 (>= 2.1.2),
3335
hellno (>= 0.0.1),
3436
magrittr (>= 1.5),
3537
digest (>= 0.6.9),
36-
stringb (>= 0.1.0),
37-
rtext (>= 0.1.0),
3838
stats,
3939
graphics
4040
Suggests:
@@ -44,5 +44,5 @@ Suggests:
4444
BugReports: https://github.com/petermeissner/diffrprojects/issues
4545
URL: https://github.com/petermeissner/diffrprojects
4646
RoxygenNote: 5.0.1
47-
48-
47+
VignetteBuilder: knitr
48+
Additional_repositories: http://petermeissner.github.io/drat

NAMESPACE

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

3-
export(classes)
3+
export("%>%")
44
export(diffrproject)
5-
export(dim1)
6-
export(dim2)
7-
export(dp_ls)
85
export(dp_text_base_data)
9-
export(dp_tf)
10-
export(get_vector_element)
11-
export(is_between)
12-
export(modus)
13-
export(rbind_fill)
14-
export(seq_dim1)
15-
export(shift)
166
import(hellno)
177
import(rtext)
188
import(stringb)

R/IMPORTS.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#' imports
2+
#' @importFrom R6 R6Class
3+
#' @import hellno
4+
#' @import stringb
5+
#' @import rtext
6+
dummyimport <- function(){
7+
R6::R6Class()
8+
1 %>% magrittr::add(1)
9+
}
10+
11+
#' @importFrom magrittr %>%
12+
#' @export
13+
magrittr::`%>%`

R/diffrproject.R

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,34 @@ diffrproject <-
2323

2424
#### private ===============================================================
2525
private = list(
26+
text_add_worker = function( rtext, name = NULL ){
2627

28+
# input check
29+
stopifnot("rtext" %in% class(rtext) )
30+
31+
# working variable creation
32+
names <- names(self$texts)
33+
ids <- vapply(self$texts, `[[`, "", "id")
34+
id <- rtext$id
35+
36+
# doing-duty-to-do
37+
if( is.null(name) ){
38+
name <- tryCatch(basename(NULL), error=function(e){NA} )
39+
if( is.na(name) ){
40+
next_num <- max(c(as.numeric(text_extract(names, "\\d+")),0))+1
41+
name <- text_c( "noname_", next_num)
42+
}
43+
}
44+
self$texts[[name]] <- rtext
45+
i <- 0
46+
while( rtext$id %in% ids ){
47+
rtext$id <- text_c(id, "_", i)
48+
i <- i+1
49+
}
50+
51+
# return self for piping
52+
return(invisible(self))
53+
}
2754
),
2855

2956

@@ -44,30 +71,18 @@ diffrproject <-
4471

4572
#### methods =============================================================
4673
# add text
47-
text_add = function( rtext, name = NULL ){
48-
49-
# input check
50-
stopifnot("rtext" %in% class(rtext) )
51-
52-
# working variable creation
53-
names <- names(self$texts)
54-
ids <- vapply(self$texts, `[[`, "", "id")
55-
id <- rtext$id
56-
57-
# doing-duty-to-do
58-
if( is.null(name) ){
59-
next_num <- max(c(as.numeric(text_extract(names, "\\d+")),0))+1
60-
name <- text_c( "noname_", next_num)
61-
}
62-
self$texts[[name]] <- rtext
63-
i <- 0
64-
while( rtext$id %in% ids ){
65-
rtext$id <- text_c(id, "_", i)
66-
i <- i+1
74+
text_add = function(text, name=NULL, ...){
75+
if( any(class(text) %in% "character") ){
76+
stopifnot(file.exists(text))
77+
for(i in seq_along(text)){
78+
private$text_add_worker(
79+
rtext$new(text_file=text[i], ...),
80+
name = ifelse(is.null(name), basename(text[i]), name[i])
81+
)
82+
}
83+
}else{
84+
private$text_add_worker(text,name = name)
6785
}
68-
69-
# return self for piping
70-
return(invisible(self))
7186
},
7287

7388
# delete text
@@ -91,6 +106,10 @@ diffrproject <-
91106
},
92107

93108
texts_link = function(from=NULL, to=NULL, delete=FALSE){
109+
if( is.null(from) & is.null(to) ){
110+
from <- shift(names(dp$texts), 1, NULL)
111+
to <- shift(names(dp$texts), -1, NULL)
112+
}
94113
from <- names(self$texts[from])
95114
to <- names(self$texts[to])
96115
linker <- function(from, to, delete){

R/imports.r

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#' imports
22
#' @importFrom R6 R6Class
33
#' @import hellno
4-
#' @importFrom magrittr %>%
54
#' @import stringb
65
#' @import rtext
76
dummyimport <- function(){
87
R6::R6Class()
98
1 %>% magrittr::add(1)
109
}
1110

12-
# #' @useDynLib diffrprojects
13-
# #' @importFrom Rcpp sourceCpp
14-
# NULL
11+
#' @importFrom magrittr %>%
12+
#' @export
13+
magrittr::`%>%`

R/methods_of_comparison.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#' method of comparison
2+
#' @export
3+

R/testing_tools.R

Lines changed: 0 additions & 33 deletions
This file was deleted.

R/text_diff.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#' function for calculating distance matrix between two texts

R/tools.R

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,81 @@
1+
#' checking if value is uniqe in set
2+
#' @param x vector to check
3+
is_unique <- function(x){
4+
!is_duplicate(x)
5+
}
6+
7+
#' checking if value is duplicated in set
8+
#' @param x vector to check
9+
is_duplicate <- function(x){
10+
x %in% x[duplicated(x)]
11+
}
12+
13+
#' extract specific item from each list element
14+
#' @param l list
15+
#' @param item name or index of item to extract
16+
get_list_item <- function(l, item, unlist=TRUE){
17+
tmp <- lapply(l, `[`, item)
18+
index <- vapply(tmp, is.null, TRUE)
19+
tmp[index] <- NA
20+
if( unlist ){
21+
return(unlist(tmp))
22+
}else{
23+
return(tmp)
24+
}
25+
}
26+
27+
28+
#' text function: wrapper for system.file() to access test files
29+
#' @param x name of the file
30+
#' @param pattern pattern of file name
31+
#' @keywords internal
32+
test_file <- function(x=NULL, pattern=NULL, full.names=FALSE){
33+
if(is.numeric(x)){
34+
return(dp_tf(dp_tf()[(x-1) %% length(dp_tf()) +1 ]))
35+
}
36+
if(is.null(x)){
37+
return(
38+
list.files(
39+
system.file(
40+
"testfiles",
41+
package = "diffrprojects"
42+
),
43+
pattern = pattern,
44+
full.names = full.names
45+
)
46+
)
47+
}else if(x==""){
48+
return(
49+
list.files(
50+
system.file(
51+
"testfiles",
52+
package = "diffrprojects"
53+
),
54+
pattern = pattern,
55+
full.names = full.names
56+
)
57+
)
58+
}else{
59+
return(
60+
system.file(
61+
paste("testfiles", x, sep="/"),
62+
package = "diffrprojects")
63+
)
64+
}
65+
}
66+
67+
68+
69+
70+
#' function rbinding list elements
71+
#' @param l list
72+
#' @keywords internal
73+
rbind_list <- function(l){
74+
tmp <- do.call(rbind, l)
75+
rownames(tmp) <- NULL
76+
as.data.frame(tmp, stringsAsFactors = FALSE)
77+
}
78+
179
#' function that shifts vector values to right or left
280
#'
381
#' @param x Vector for which to shift values
@@ -8,7 +86,7 @@
886
#' @param default The value that should be inserted by default.
987
#' @param invert Whether or not the default shift directions
1088
#' should be inverted.
11-
#' @export
89+
#' @keywords internal
1290
shift <- function(x, n=0, default=NA, invert=FALSE){
1391
n <-
1492
switch (
@@ -44,6 +122,7 @@ shift <- function(x, n=0, default=NA, invert=FALSE){
44122
#' @param x the values to be bound
45123
#' @param max upper boundary
46124
#' @param min lower boundary
125+
#' @keywords internal
47126
bind_between <- function(x, min, max){
48127
x[x<min] <- min
49128
x[x>max] <- max
@@ -54,7 +133,7 @@ bind_between <- function(x, min, max){
54133
#' function for binding data.frames even if names do not match
55134
#' @param df1 first data.frame to rbind
56135
#' @param df2 second data.frame to rbind
57-
#' @export
136+
#' @keywords internal
58137
rbind_fill <- function(df1=data.frame(), df2=data.frame()){
59138
names_df <- c(names(df1), names(df2))
60139
if( dim1(df1) > 0 ){
@@ -74,10 +153,10 @@ rbind_fill <- function(df1=data.frame(), df2=data.frame()){
74153

75154

76155
#' function that checks is values are in between values
77-
#' @export
78156
#' @param x input vector
79157
#' @param y lower bound
80158
#' @param z upper bound
159+
#' @keywords internal
81160
is_between <- function(x,y,z){
82161
return(x>=y & x<=z)
83162
}
@@ -89,9 +168,7 @@ is_between <- function(x,y,z){
89168
#' @param length number of elements to be returned
90169
#' @param from first element to be returned
91170
#' @param to last element to be returned
92-
#'
93-
#' @export
94-
#'
171+
#' @keywords internal
95172
get_vector_element <-
96173
function(vec, length=100, from=NULL, to=NULL){
97174
# helper functions
@@ -151,23 +228,23 @@ get_vector_element <-
151228

152229
#' get first dimension or length of object
153230
#' @param x object, matrix, vector, data.frame, ...
154-
#' @export
231+
#' @keywords internal
155232
dim1 <- function(x){
156233
ifelse(is.null(dim(x)[1]), length(x), dim(x)[1])
157234
}
158235

159236

160237
#' get first dimension or length of object
161238
#' @param x object, matrix, vector, data.frame, ...
162-
#' @export
239+
#' @keywords internal
163240
dim2 <- function(x){
164241
dim(x)[2]
165242
}
166243

167244

168245
#' seq along first dimension / length
169246
#' @param x x
170-
#' @export
247+
#' @keywords internal
171248
seq_dim1 <- function(x){
172249
seq_len(dim1(x))
173250
}
@@ -177,10 +254,11 @@ seq_dim1 <- function(x){
177254

178255

179256
#' function giving back the mode
180-
#' @export
257+
181258
#' @param x vector to get mode for
182259
#' @param multimodal wether or not all modes should be returned in case of more than one
183260
#' @param warn should the function warn about multimodal outcomes?
261+
#' @keywords internal
184262
modus <- function(x, multimodal=FALSE, warn=TRUE) {
185263
x_unique <- unique(x)
186264
tab_x <- tabulate(match(x, x_unique))
@@ -204,8 +282,9 @@ modus <- function(x, multimodal=FALSE, warn=TRUE) {
204282

205283

206284
#' function to get classes from e.g. lists
207-
#' @export
285+
208286
#' @param x list to get classes for
287+
#' @keywords internal
209288
classes <- function(x){
210289
tmp <- lapply(x, class)
211290
data.frame(name=names(tmp), class=unlist(tmp) , row.names = NULL)
@@ -219,6 +298,7 @@ classes <- function(x){
219298
#' function to sort df by variables
220299
#' @param df data.frame to be sorted
221300
#' @param ... column names to use for sorting
301+
#' @keywords internal
222302
dp_arrange <- function(df, ...){
223303
sorters <- as.character(as.list(match.call()))
224304
if( length(sorters)>2 ){

0 commit comments

Comments
 (0)