/
rDNA.R
8998 lines (8848 loc) · 385 KB
/
rDNA.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Startup ----------------------------------------------------------------------
dnaEnvironment <- new.env(hash = TRUE, parent = emptyenv())
# display version number and date when the package is attached
#' @importFrom utils packageDescription
.onAttach <- function(libname, pkgname) {
desc <- packageDescription(pkgname, libname)
packageStartupMessage(
'Version: ', desc$Version, '\n',
'Date: ', desc$Date, '\n',
'Author: Philip Leifeld (University of Glasgow)\n',
'Contributors: Johannes B. Gruber (University of Glasgow),\n',
' Tim Henrichsen (Scuola superiore Sant\'Anna Pisa)\n',
'Project home: github.com/leifeld/dna'
)
}
# more settings which quiet concerns of R CMD check about ggplot and dplyr pipelines
if (getRversion() >= "2.15.1") {
utils::globalVariables(
c("rn",
"cols3",
"labels_short",
"leaf",
"x",
"y",
"mean_dim1",
"mean_dim2",
"name",
"it",
"color"
)
)
}
# Data access ------------------------------------------------------------------
#' Establish a database connection
#'
#' Connect to a local .dna file or remote mySQL DNA database.
#'
#' Before any data can be loaded from a database, a connection with
#' the database must be established. The \code{dna_connection}
#' function establishes a database connection and loads the documents
#' and statements into memory for further processing.
#'
#' @param infile The file name of the .dna database or the URL of the mySQL
#' database to load
#' @param login The user name for accessing the database (only applicable
#' to remote mySQL databases; can be \code{NULL} if a local .dna file
#' is used).
#' @param password The password for accessing the database (only applicable
#' to remote mySQL databases; can be \code{NULL} if a local .dna file
#' is used).
#' @param create If the file or remote database structure does not exist yet,
#' should it be created with default values?
#' @param verbose Print details the number of documents and statements after
#' loading the database?
#'
#' @examples
#' \dontrun{
#' dna_init()
#' dna_connection(dna_sample())
#' }
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava .jnew
#' @export
dna_connection <- function(infile, login = NULL, password = NULL, create = FALSE, verbose = TRUE) {
if (is.null(login) & is.null(password) & !file.exists(infile) & !isTRUE(create)) {
if (grepl("/", infile, fixed = TRUE)) {
msg <- paste0("infile '",
infile,
"' could not be located. Use 'create = TRUE' to create a new database.")
} else {
msg <- paste0("infile '",
infile,
"' could not be located in working directory '",
getwd(),
"'. Use 'create = TRUE' to create a new database.")
}
stop(msg)
}
if (!grepl("/", infile, fixed = TRUE)) {
infile <- paste0(getwd(), "/", infile)
}
if (is.null(dnaEnvironment[["dnaJarString"]])) {
stop("No connection between rDNA and DNA detected. Maybe dna_init() would help.")
}
if (is.null(login) || is.null(password)) {
export <- .jnew("dna.export/ExporterR", "sqlite", infile, "", "", verbose)
} else {
export <- .jnew("dna.export/ExporterR", "mysql", infile, login, password, verbose)
}
obj <- list(dna_connection = export)
class(obj) <- "dna_connection"
if (isTRUE(verbose)) {
print(obj)
}
return(obj)
}
#' Print the summary of a \code{dna_connection} object
#'
#' Show details of a \code{dna_connection} object.
#'
#' Print the number of documents and statements to the console after
#' establishing a DNA connection.
#'
#' @param x A \code{dna_connection} object.
#' @param ... Further options (currently not used).
#'
#' @examples
#' \dontrun{
#' dna_init()
#' conn <- dna_connection(dna_sample(), verbose = FALSE)
#' conn
#' }
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava .jcall
#' @export
print.dna_connection <- function(x, ...) {
cat(.jcall(x$dna_connection, "S", "rShow"))
}
#' Print the summary of a \code{dna_dataframe} object
#'
#' Show details of a \code{dna_dataframe} object.
#'
#' Print a data frame returned by \link{dna_getDocuments},
#' \link{dna_getStatements}, or \link{dna_getAttributes}. The only difference
#' between this print method and the default print method for data frames is
#' that the \code{text} column and other columns containing character strings
#' are truncated for better readability on screen.
#'
#' @param x A \code{dna_connection} object.
#' @param truncate Number of characters to which character columns in the data
#' frame should be truncated.
#' @param ... Further options (currently not used).
#'
#' @author Philip Leifeld
#'
#' @export
print.dna_dataframe <- function(x, truncate = 20, ...) {
x2 <- x
class(x2) <- class(x2)[-1]
x2[, unlist(sapply(x2, is.character))] <- apply(x2[, unlist(sapply(x2, is.character))],
1:2,
function(t) trim(x = trimws(t), n = truncate, e = "*"))
cat("Note: text denoted by * is truncated to", truncate, "characters for readability.\n\n")
print(x2)
}
#' Download the binary DNA jar file
#'
#' Downloads the newest released DNA jar file necessary for running
#' \link{dna_init}.
#'
#' This function uses GitHub's API to download the latest DNA jar file to the
#' working directory.
#'
#' @param path Directory path in which the jar file will be stored.
#' @param force Logical. Should the file be overwritten if it already exists?
#' @param returnString Logical. Return the file name of the downloaded jar file?
#'
#' @author Philip Leifeld, Johannes B. Gruber
#'
#' @importFrom utils download.file
#' @export
dna_downloadJar <- function(path = paste0(dirname(system.file(".", package = "rDNA")), "/", "extdata"),
force = FALSE,
returnString = FALSE) {
u <- url("https://api.github.com/repos/leifeld/dna/releases")
open(u)
lines <- readLines(u, warn = FALSE)
m <- gregexpr("https://github.com/leifeld/dna/releases/download/.{3,15}?/dna-.{3,15}?\\.jar", lines, perl = TRUE)
m <- regmatches(lines, m)[[1]]
close(u)
filename <- strsplit(m[1], "/")[[1]]
filename <- filename[length(filename)]
filename <- paste0(path, ifelse(endsWith(path, "/"), "", "/"), filename)
if (force == TRUE || (force == FALSE && !file.exists(filename))) {
download.file(url = m[1], destfile = filename, mode = "wb", cacheOK = FALSE)
} else {
warning("Latest DNA jar file already exists. Use 'force = TRUE' to overwrite it.")
}
if (returnString == TRUE) {
return(filename)
}
}
#' Open the DNA GUI
#'
#' Start DNA graphical user interface and optionally load a database.
#'
#' Start the DNA graphical user interface (GUI). Optionally load a .dna database
#' or a mySQL online database upon start-up of the GUI.
#'
#' @param infile The file name of the .dna database or the URL of the mySQL
#' database to load upon start-up of the GUI or a \link{dna_connection}
#' object.
#' @param login If \code{infile} is a mySQL connection string, \code{login} is
#' the login user name for the database. This argument is not used if the
#' \code{infile} is a file-based database.
#' @param password If \code{infile} is a mySQL connection string,
#' \code{password} is the password for the database. This argument is not used
#' if the \code{infile} is a file-based database.
#' @param javapath The path to the \code{java} command. This may be useful if
#' the CLASSPATH is not set and the java command can not be found. Java
#' is necessary to start the DNA GUI.
#' @param memory The amount of memory in megabytes to allocate to DNA, for
#' example \code{1024} or \code{4096}.
#' @param verbose Print details and error messages from the call to DNA?
#'
#' @examples
#' \dontrun{
#' dna_init()
#' dna_gui()
#' }
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava J
#' @export
dna_gui <- function(infile = NULL,
login = NULL,
password = NULL,
javapath = NULL,
memory = 1024,
verbose = TRUE) {
if (is.null(dnaEnvironment[["dnaJarString"]])) {
stop("No connection between rDNA and DNA detected. Maybe dna_init() would help.")
}
djs <- dnaEnvironment[["dnaJarString"]]
if (is.null(djs)) {
stop(paste0("'", djs, "' could not be located in working directory '", getwd(), "'."))
}
if (!is.null(infile)) {
if (class(infile) == "dna_connection") {
if (is.null(login)) {
login <- J(infile$dna_connection, "getLogin")
}
if (is.null(password)) {
password <- J(infile$dna_connection, "getPassword")
}
infile <- J(infile$dna_connection, "getDbfile")
}
if (!file.exists(infile)) {
stop(
if (grepl("/", infile, fixed = TRUE)) {
paste0("'", infile, "' could not be located.")
} else {
paste0("'", infile, "' could not be located in working directory '", getwd(), "'.")
}
)
}
}
if (is.null(infile)) {
f <- ""
} else if (is.null(login) || is.null(password) || (login == "" && password == "")) {
f <- paste0(" \"", infile, "\"")
} else {
f <- paste0(" \"", infile, "\" \"", login, "\" \"", password, "\"")
}
if (is.null(javapath)) {
jp <- "java"
} else if (grepl("/$", javapath)) {
jp <- paste0(javapath, "java")
} else {
jp <- paste0(javapath, "/java")
}
if (verbose == TRUE) {
message("To return to R, close the DNA window when done.")
}
system(paste0(jp, " -jar -Xmx", memory, "M \"", djs, "\"", f), intern = !verbose)
}
#' Initialize the connection with DNA
#'
#' Establish a connection between \pkg{rDNA} and the DNA software.
#'
#' To use \pkg{rDNA}, DNA first needs to be initialized. This means that
#' \pkg{rDNA} needs to be told where the DNA executable file, i.e., the JAR
#' file, is located. When the \code{dna_init} function is used, the connection
#' to the DNA software is established, and this connection is valid for the
#' rest of the \R session. To initialize a connection with a different DNA
#' version or path, the \R session would need to be restarted first.
#'
#' @param jarfile The file name of the DNA jar file, e.g.,
#' \code{"dna-2.0-beta23.jar"}. Will be auto-detected by choosing the most
#' recent version stored in the library path or working directory if
#' \code{jarfile = NULL}.
#' @param memory The amount of memory in megabytes to allocate to DNA, for
#' example \code{1024} or \code{4096}.
#' @param returnString Return a character object representing the jar file name?
#'
#' @author Philip Leifeld
#'
#' @export
#' @import rJava
dna_init <- function(jarfile = NULL, memory = 1024, returnString = FALSE) {
if (is.null(jarfile) || is.na(jarfile)) {
# auto-detect file name in library directory
path <- paste0(dirname(system.file(".", package = "rDNA")), "/", "extdata")
files <- dir(path)
files <- files[grepl("^dna-.+\\.jar$", files)]
files <- sort(files)
if (length(files) > 0) {
jarfile <- paste0(path, "/", files[length(files)])
}
# auto-detect file name in working directory
jarfile_wd <- NULL
path_wd <- getwd()
files_wd <- dir(path_wd)
files_wd <- files_wd[grepl("^dna-.+\\.jar$", files_wd)]
files_wd <- sort(files_wd)
if (length(files_wd) > 0) {
jarfile_wd <- paste0(path_wd, "/", files_wd[length(files_wd)])
}
# use file in working directory if version is more recent or none found in library path
if ((!is.null(jarfile) && !is.null(jarfile_wd) && basename(jarfile_wd) > basename(jarfile)) || is.null(jarfile)) {
jarfile <- jarfile_wd
}
# if none was found whatsoever, attempt to download to library path
if (is.null(jarfile)) {
message("No jar file found. Trying to download most recent version to library path.")
jarfile <- dna_downloadJar(path = path, returnString = TRUE)
message("Done.")
}
}
if (is.null(jarfile) || length(jarfile) == 0) {
message("No DNA jar file found in the library path or working directory.")
if (isTRUE(returnString)) {
return(NULL)
}
}
if (!is.character(jarfile) || length(jarfile) > 1 || !grepl("^dna-.+\\.jar$", basename(jarfile))) {
stop("'jarfile' must be a character object of length 1 that points to the DNA jar file.")
}
if (!file.exists(jarfile)) {
stop(paste0("jarfile '", jarfile, "' could not be located."))
}
assign("dnaJarString", jarfile, pos = dnaEnvironment)
message(paste("Jar file:", dnaEnvironment[["dnaJarString"]]))
.jinit(dnaEnvironment[["dnaJarString"]],
force.init = TRUE,
parameters = paste0("-Xmx", memory, "m"))
if (isTRUE(returnString)) {
return(jarfile)
}
}
#' Provides a small sample database
#'
#' Copies a small .dna sample file to the current working directory and returns
#' the location of this newly created file.
#'
#' A small sample database to test the functions of rDNA.
#'
#' @param overwrite Logical. Should sample.dna be overwritten if found in the
#' current working directory?
#' @param verbose Display warning message if file exists in current wd.
#'
#' @examples
#' \dontrun{
#' dna_init()
#' dna_connection(dna_sample())
#' }
#'
#' @author Johannes B. Gruber
#'
#' @export
dna_sample <- function(overwrite = FALSE,
verbose = TRUE) {
if (file.exists(paste0(getwd(), "/sample.dna")) & overwrite == FALSE) {
if (verbose) {
warning(
"Sample file already exists in working directory. Use 'overwrite = TRUE' to create fresh sample file."
)
}
} else {
file.copy(from = system.file("extdata", "sample.dna", package = "rDNA"),
to = paste0(getwd(), "/sample.dna"),
overwrite = overwrite)
}
return(paste0(getwd(), "/sample.dna"))
}
# Data management --------------------------------------------------------------
#' Add an attribute to the DNA database
#'
#' Add a new attribute to the DNA database.
#'
#' The \code{dna_addAttribute} function can add new attributes to an existing
#' DNA database. Attributes are the annotations that are coded within a
#' variable, along with some meta-data. For example, if the variable is
#' "organization", then an attribute value could be "Environmental Protection
#' Agency", and other attribute meta-data for this value could be its color,
#' actor type etc. The \code{dna_addAttribute} function can not only be used to
#' add entries to the attributes table in an ongoing project, but also to
#' pre-populate a DNA database with values for deductive coding. Either way, the
#' user supplies a \link{dna_connection} object as well as various details about
#' the attribute to be added, for example the value, color, type etc. The
#' attribute ID will be automatically generated and can be returned if
#' \code{returnID} is set to \code{TRUE}.
#'
#' @param connection A \code{dna_connection} object created by the
#' \code{dna_connection} function.
#' @param statementType The ID of the statement type (as an integer) or the name
#' of the statement type (as a character object) in which the variable is
#' defined.
#' @param variable The name of the variable for which attribute data should be
#' retrieved, for example \code{"organization"} or \code{"concept"}.
#' @param value The value of the new attribute as a character object.
#' @param color A character object containing the color of the new document as a
#' hexadecimal RGB value.
#' @param type The type of the new attribute as a character object.
#' @param alias A character object containing the alias of the new attribute.
#' @param notes The notes of the new attribute as a character object.
#' @param returnID Return the ID of the newly created attribute as a numeric
#' value?
#' @param verbose Print details?
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava .jcall
#' @export
dna_addAttribute <- function(connection,
statementType = 1,
variable = "organization",
value = "new value",
color = "#000000",
type = "",
alias = "",
notes = "",
returnID = FALSE,
verbose = TRUE) {
if (is.numeric(statementType) && !is.integer(statementType)) {
statementType <- as.integer(statementType)
}
if (!class(statementType) %in% c("character", "integer")) {
stop("The statement type must be either an integer value or a character object.")
}
if (length(statementType) > 1) {
stop("Only one single statement type must be provided, not multiple types.")
}
if (!is.character(variable)) {
stop("The variable must be provided as a character object.")
}
if (length(variable) > 1) {
stop("Only one single variable must be provided, not multiple types.")
}
if (!is.character(value)) {
stop("The value must be provided as a character object.")
}
if (length(value) > 1) {
stop("Only one single value must be provided, not multiple values.")
}
if (!is.character(color)) {
stop("The color must be provided as a character object in hexadecimal RGB format.")
}
if (length(color) > 1) {
stop("Only one single color must be provided, not multiple colors.")
}
if (!is.character(type)) {
stop("The type must be provided as a character object.")
}
if (length(type) > 1) {
stop("Only one single type must be provided, not multiple types.")
}
if (!is.character(alias)) {
stop("The alias must be provided as a character object.")
}
if (length(alias) > 1) {
stop("Only one single alias must be provided, not multiple aliases.")
}
if (!is.character(notes)) {
stop("The notes must be provided as a character object.")
}
if (length(notes) > 1) {
stop("Only one single note must be provided, not multiple notes.")
}
id <- .jcall(connection$dna_connection,
"I",
"addAttribute",
statementType,
variable,
value,
color,
type,
alias,
notes)
if (verbose == TRUE) {
message("A new attribute with ID ", id, " was added to the database.")
}
if (returnID == TRUE) {
return(id)
}
}
#' Add a new coder to the DNA database
#'
#' Add a new coder to the DNA database.
#'
#' The \code{dna_addCoder} function can add a new coder to an existing DNA
#' database. The user supplies a \link{dna_connection} object, the name of the
#' new coder, the color used to display the coder in the graphical user
#' interface, as well as various permissions of the coder.
#'
#' @param connection A \code{dna_connection} object created by the
#' \code{dna_connection} function.
#' @param name A character object indicating the name of the new coder.
#' @param color A character object indicating the color in which the new coder
#' should be displayed in the graphical user interface. The color must be
#' supplied as a hexadecimal string, for example \code{"#FFFF00"} for yellow.
#' @param addDocuments Logical: should the coder have the permission to add new
#' documents via the graphical user interface?
#' @param editDocuments Logical: should the coder have permission to edit the
#' meta-data of documents in the graphical user interface?
#' @param deleteDocuments Logical: should the coder have permission to delete
#' documents from the database in the graphical user interface?
#' @param importDocuments Logical: should the coder have permission to import
#' documents into the database via the graphical user interface?
#' @param viewOthersDocuments Logical: should the coder have permission to view
#' the documents that were added by other coders?
#' @param editOthersDocuments Logical: should the coder have permission to edit
#' the meta-data of documents added by other coders?
#' @param addStatements Logical: should the coder have permission to add new
#' statements to the database?
#' @param viewOthersStatements Logical: should the coder have permission to view
#' the statements in the graphical user interface that were added by other
#' coders?
#' @param editOthersStatements Logical: should the coder have permission to edit
#' the statements in the graphical user interface that were added by other
#' coders?
#' @param editCoders Logical: should the coder have permission to add, remove,
#' or edit coders in the graphical user interface?
#' @param editStatementTypes Logical: should the coder have permission to add,
#' remove, or edit statement types in the graphical user interface?
#' @param editRegex Logical: should the coder have permission to add, remove, or
#' edit regular expressions in the graphical user interface?
#' @param verbose Print details?
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava .jcall
#' @export
dna_addCoder <- function(connection,
name,
color,
addDocuments = TRUE,
editDocuments = TRUE,
deleteDocuments = TRUE,
importDocuments = TRUE,
viewOthersDocuments = TRUE,
editOthersDocuments = TRUE,
addStatements = TRUE,
viewOthersStatements = TRUE,
editOthersStatements = TRUE,
editCoders = TRUE,
editStatementTypes = TRUE,
editRegex = TRUE,
verbose = TRUE) {
if (is.null(name) || is.na(name) || !is.character(name) || length(name) != 1) {
stop("'name' must be a character object of length 1.")
}
if (is.null(color) || is.na(color) || !is.character(color) || length(color) != 1) {
stop("'color' must be a character object of length 1 (in hexadecimal format, for example '#FFFF00' for yellow).")
}
if (is.null(addDocuments) || is.na(addDocuments) || !is.logical(addDocuments) || length(addDocuments) != 1) {
stop("'addDocuments' must be a TRUE or FALSE.")
}
if (is.null(editDocuments) || is.na(editDocuments) || !is.logical(editDocuments) || length(editDocuments) != 1) {
stop("'editDocuments' must be a TRUE or FALSE.")
}
if (is.null(deleteDocuments) || is.na(deleteDocuments) || !is.logical(deleteDocuments) || length(deleteDocuments) != 1) {
stop("'deleteDocuments' must be a TRUE or FALSE.")
}
if (is.null(importDocuments) || is.na(importDocuments) || !is.logical(importDocuments) || length(importDocuments) != 1) {
stop("'importDocuments' must be a TRUE or FALSE.")
}
if (is.null(viewOthersDocuments) || is.na(viewOthersDocuments) || !is.logical(viewOthersDocuments) || length(viewOthersDocuments) != 1) {
stop("'viewOthersDocuments' must be a TRUE or FALSE.")
}
if (is.null(editOthersDocuments) || is.na(editOthersDocuments) || !is.logical(editOthersDocuments) || length(editOthersDocuments) != 1) {
stop("'editOthersDocuments' must be a TRUE or FALSE.")
}
if (is.null(addStatements) || is.na(addStatements) || !is.logical(addStatements) || length(addStatements) != 1) {
stop("'addStatements' must be a TRUE or FALSE.")
}
if (is.null(viewOthersStatements) || is.na(viewOthersStatements) || !is.logical(viewOthersStatements) || length(viewOthersStatements) != 1) {
stop("'viewOthersStatements' must be a TRUE or FALSE.")
}
if (is.null(editOthersStatements) || is.na(editOthersStatements) || !is.logical(editOthersStatements) || length(editOthersStatements) != 1) {
stop("'editOthersStatements' must be a TRUE or FALSE.")
}
if (is.null(editCoders) || is.na(editCoders) || !is.logical(editCoders) || length(editCoders) != 1) {
stop("'editCoders' must be a TRUE or FALSE.")
}
if (is.null(editStatementTypes) || is.na(editStatementTypes) || !is.logical(editStatementTypes) || length(editStatementTypes) != 1) {
stop("'editStatementTypes' must be a TRUE or FALSE.")
}
if (is.null(editRegex) || is.na(editRegex) || !is.logical(editRegex) || length(editRegex) != 1) {
stop("'editRegex' must be a TRUE or FALSE.")
}
if (is.null(verbose) || is.na(verbose) || !is.logical(verbose) || length(verbose) != 1) {
stop("'verbose' must be TRUE or FALSE.")
}
.jcall(connection$dna_connection,
"V",
"addCoder",
name,
color,
"",
addDocuments,
editDocuments,
deleteDocuments,
importDocuments,
viewOthersDocuments,
editOthersDocuments,
addStatements,
viewOthersStatements,
editOthersStatements,
editCoders,
editStatementTypes,
editRegex,
verbose)
}
#' Add a document to the DNA database
#'
#' Add a new document to the DNA database.
#'
#' The \code{dna_addDocument} function can add new documents to an existing DNA
#' database. The user supplies a \link{dna_connection} object as well as various
#' details about the document, for example the title, text, date etc. The date
#' can be supplied either as a \link{POSIXct} object or as an integer value
#' containing millisecond since 1970-01-01. The document ID will be
#' automatically generated and can be returned if \code{returnID} is set to
#' \code{TRUE}.
#'
#' @param connection A \code{dna_connection} object created by the
#' \code{dna_connection} function.
#' @param title A character object containing the title of the new document.
#' @param text A character object containing the text of the new document. Line
#' breaks can be included as \code{"\\n"}.
#' @param coder An integer value indicating which coder created the document.
#' @param author A character object containing the author of the document.
#' @param source A character object containing the source of the document.
#' @param section A character object containing the section of the document.
#' @param notes A character object containing notes about the document.
#' @param type A character object containing the type of the document.
#' @param date A \code{POSIXct} object containing the date/time stamp of the
#' document. Alternatively, the date/time can be supplied as an integer
#' value indicating the milliseconds since the start of 1970-01-01.
#' @param returnID Return the ID of the newly created document as a numeric
#' value?
#' @param verbose Print details?
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava .jcall
#' @export
dna_addDocument <- function(connection,
title = "",
text = "",
coder = 1,
author = "",
source = "",
section = "",
notes = "",
type = "",
date = Sys.time(),
returnID = FALSE,
verbose = TRUE) {
if (!is.character(title)) {
stop("The title must be provided as a character object.")
}
if (!is.character(text)) {
stop("The text must be provided as a character object.")
}
if (!is.integer(coder)) {
if (is.numeric(coder)) {
coder <- as.integer(coder)
} else {
stop("The coder must be provided as a numeric object (see dna_getCoders).")
}
}
if (!is.character(author)) {
stop("The author must be provided as a character object.")
}
if (!is.character(source)) {
stop("The source must be provided as a character object.")
}
if (!is.character(section)) {
stop("The section must be provided as a character object.")
}
if (!is.character(notes)) {
stop("The notes must be provided as a character object.")
}
if (!is.character(type)) {
stop("The type must be provided as a character object.")
}
if (any(class(date) %in% c("POSIXct", "POSIXt"))) {
dateLong <- .jlong(as.integer(date) * 1000)
} else if (is.numeric(date)) {
dateLong <- .jlong(as.integer(date))
} else {
stop("The document date must be provided as a POSIXct object or as a numeric value indicating milliseconds since 1970-01-01.")
}
id <- .jcall(connection$dna_connection,
"I",
"addDocument",
title,
text,
coder,
author,
source,
section,
notes,
type,
dateLong)
if (verbose == TRUE) {
message("A new document with ID ", id, " was added to the database.")
}
if (returnID == TRUE) {
return(id)
}
}
#' Add a new regular expression to the database
#'
#' Add a new regular expression to the database.
#'
#' Add a new statement type to a database. The statement type contains no
#' variables but can be populated with variables using the
#' \link{dna_addVariable} function. Along with the the label used to describe
#' the statement type, a color needs to be supplied in order to display the
#' statement type in this color in the GUI (see \link{dna_gui}).
#'
#' @param connection A \code{dna_connection} object created by the
#' \code{dna_connection} function.
#' @param regex A regular expression.
#' @param color A color in the form of a hexadecimal RGB string, such as
#' \code{"#FFFF00"} for yellow.
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava .jcall
#' @export
dna_addRegex <- function(connection, regex, color = "#FFFF00") {
if (is.null(regex) || is.na(regex) || length(regex) != 1 || !is.character(regex)) {
stop("'regex' must be a character object of length 1.")
}
if (is.null(color) || is.na(color) || length(color) != 1 || !is.character(color)) {
stop("'color' must be a character object of length 1 containing a hexadecimal RGB value.")
}
if (!grepl("^#[0-9a-fA-F]{6}$", color)) {
stop("'color' is not a hex RGB value of the form '#FFFF00'.")
}
.jcall(connection$dna_connection, "V", "addRegex", regex, color)
}
#' Add a statement to the DNA database
#'
#' Add a new statement to the DNA database.
#'
#' The \code{dna_addStatement} function can add a new statement to an existing
#' DNA database. The user supplies a \link{dna_connection} object as well as
#' the document ID, location of the statement in the document, and the variables
#' and their values. As different statement types have different variables, the
#' \code{...} argument catches all variables and their values supplied by the
#' user. The statement ID will be automatically generated and can be returned
#' if \code{returnID} is set to \code{TRUE}.
#'
#' @param connection A \code{dna_connection} object created by the
#' \code{dna_connection} function.
#' @param documentID An integer specifying the ID of the document for which the
#' statement should be added.
#' @param startCaret An integer for the start location of the statement in the
#' document text. Must be non-negative and not larger than the number of
#' characters minus one in the document.
#' @param endCaret An integer for the stop location of the statement in the
#' document text. Must be non-negative, greater than \code{startCaret}, and not
#' larger than the number of characters in the document.
#' @param statementType The statement type of the statement that will be added.
#' Can be provided as an integer ID of the statement type or as a character
#' object representing the name of the statement type (if there is no
#' ambiguity).
#' @param coder An integer value indicating which coder created the document.
#' @param returnID Return the ID of the newly created statement as a numeric
#' value?
#' @param verbose Print details?
#' @param ... Values of the variables contained in the statement, for example
#' \code{organization = "some actor", concept = "my concept", agreement = 1}.
#' Values for Boolean variables can be provided as \code{logical} values
#' (\code{TRUE} or \code{FALSE}) or \code{numeric} values (\code{1} or
#' \code{0}).
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava .jarray
#' @importFrom rJava .jcall
#' @export
dna_addStatement <- function(connection,
documentID,
startCaret = 1,
endCaret = 2,
statementType = "DNA Statement",
coder = 1,
returnID = FALSE,
verbose = TRUE,
...) {
if (!is.integer(documentID)) {
if (is.numeric(documentID)) {
documentID <- as.integer(documentID)
} else {
stop("'documentID' must be a numeric value specifying the ID of the document to which the statement should be added. You can look up document IDs using the 'dna_getDocuments' function.")
}
}
if (!is.integer(startCaret)) {
if (is.numeric(startCaret)) {
startCaret <- as.integer(startCaret)
} else {
stop("'startCaret' must be a single numeric value specifying the start location in of the statement in the document.")
}
}
if (!is.integer(endCaret)) {
if (is.numeric(endCaret)) {
endCaret <- as.integer(endCaret)
} else {
stop("'endCaret' must be a single numeric value specifying the end location in of the statement in the document.")
}
}
if (!is.character(statementType) && !is.numeric(statementType)) {
stop("'statementType' must be a numeric ID of the statement type or a character object indicating the name of the statement type.")
} else if (is.numeric(statementType) && !is.integer(statementType)) {
statementType <- as.integer(statementType)
}
if (!is.integer(coder)) {
if (is.numeric(coder)) {
coder <- as.integer(coder)
} else {
stop("The coder must be provided as a numeric object (see dna_getCoders).")
}
}
ellipsis <- list(...)
ellipsis <- lapply(ellipsis, function(x) {
if (is.logical(x)) {
if (x == TRUE) {
x <- 1
} else if (x == FALSE) {
x <- 0
}
}
if (is.numeric(x)) {
x <- as.integer(x)
}
if (!class(x) %in% c("character", "integer", "logical")) {
stop("All supplied values must be character, integer, or logical.")
}
if (length(x) != 1) {
stop("All supplied values must be of length 1.")
}
return(x)
})
varNames <- names(ellipsis)
ellipsis <- as.data.frame(ellipsis, stringsAsFactors = FALSE)
ellipsis <- .jarray(lapply(ellipsis, .jarray))
id <- .jcall(connection$dna_connection,
"I",
"addStatement",
documentID,
startCaret,
endCaret,
statementType,
coder,
varNames,
ellipsis,
verbose)
if (returnID == TRUE) {
return(id)
}
}
#' Add a new statement type (without variables) to the database
#'
#' Add a new statement type (without variables) to the database.
#'
#' Add a new statement type to a database. The statement type contains no
#' variables but can be populated with variables using the
#' \link{dna_addVariable} function. Along with the the label used to describe
#' the statement type, a color needs to be supplied in order to display the
#' statement type in this color in the GUI (see \link{dna_gui}).
#'
#' @param connection A \code{dna_connection} object created by the
#' \code{dna_connection} function.
#' @param label A descriptive label for the statement type. For example
#' \code{"DNA Statement" or "My new statement type"}. The label may contain
#' spaces.
#' @param color A color in the form of a hexadecimal RGB string, such as
#' \code{"#FFFF00"} for yellow.
#' @param ... Additional arguments can be added here to define the variables
#' associated with the statement type. For example,
#' \code{person = "short text"} or \code{agreement = "boolean"} or multiple
#' arguments like these separated by comma. The variable names should not
#' contain any spaces, and the values that indicate the data types should be
#' of types "short text", "long text", "boolean", or "integer".
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava .jarray
#' @importFrom rJava .jcall
#' @export
dna_addStatementType <- function(connection, label, color = "#FFFF00", ...) {
if (is.null(label) || is.na(label) || length(label) != 1 || !is.character(label)) {
stop("'label' must be a character object of length 1.")
}
if (is.null(color) || is.na(color) || length(color) != 1 || !is.character(color)) {
stop("'color' must be a character object of length 1 containing a hexadecimal RGB value.")
}
if (!grepl("^#[0-9a-fA-F]{6}$", color)) {
stop("'color' is not a hex RGB value of the form '#FFFF00'.")
}
dots <- list(...)
if (any(sapply(dots, length) > 1)) {
stop("Some arguments in ... are longer than 1. All variables need to be associated with exactly one data type.")
}
if (!all(sapply(dots, is.character))) {
stop("Some arguments in ... are not character strings. They need to indicate the variable type as a character string.")
}
if (any(grepl("\\W", names(dots)))) {
stop("Variable names must not contain any spaces.")
}
variableNames <- names(dots)
if (is.null(variableNames)) {
variableNames <- character()
}
variableNames <- .jarray(variableNames) # wrap in .jarray in case there is only one element
variableTypes <- unlist(dots)
if (is.null(variableTypes)) {
variableTypes <- character()
}
variableTypes <- .jarray(variableTypes) # wrap in .jarray in case there is only one element
.jcall(connection$dna_connection, "V", "addStatementType", label, color, variableNames, variableTypes)
}
#' Add a new variable to a statement type in the database
#'
#' Add a new variable to a statement type in the database.
#'
#' Add a new variable to an existing statement type in the database, based on
#' the statement type ID or label, a name for the new variable, and a data type
#' specification.
#'
#' @param connection A \code{dna_connection} object created by the
#' \code{dna_connection} function.
#' @param statementType The statement type in which the new variable should be
#' defined. The statement type can be supplied as an integer ID or character
#' string, for example \code{1} or \code{"DNA Statement"}.
#' @param variable The name of the new variable as a character object. Only
#' characters and numbers are allowed, i.e., no whitespace characters.
#' @param dataType The data type of the new variable. Valid values are
#' \code{"short text"} (for things like persons, organizations, locations
#' etc., up to 200 characters), \code{"long text"} (for things like notes,
#' can store more than 200 characters), \code{"boolean"} (for qualifier
#' variables such as a binary agreement variable), and \code{"integer"} (for
#' ordinal Likert scales, such as -5 to +5 or -1 to +1).
#' @param simulate Should the changes only be simulated instead of actually
#' applied to the DNA connection and the SQL database? This can help to
#' plan more complex recode operations.
#' @param verbose Print details about the recode operations?
#'
#' @examples
#' \dontrun{
#' dna_init()
#' conn <- dna_connection(dna_sample())
#' dna_addVariable(conn, 1, "location", "short text")
#' }
#'
#' @author Philip Leifeld
#'
#' @importFrom rJava .jcall
#' @export