33# ' @param file_path The path to the file that should be written or appended.
44# ' @param overwrite If the file exists, overwrite it? Defaults to \code{TRUE}.
55# ' @examples
6- # ' tdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ),
7- # ' p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"),
8- # ' o = c("qb:Observation",
9- # ' "qb:Observation",
10- # ' "\"1\"^^<xs:decimal>",
11- # ' "\"2\"^^<xs:decimal>",
12- # ' '"Example observation"'))
6+ # ' tdf <- data.frame(
7+ # ' s = c("eg:01", "eg:02", "eg:01", "eg:02", "eg:01"),
8+ # ' p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"),
9+ # ' o = c(
10+ # ' "qb:Observation",
11+ # ' "qb:Observation",
12+ # ' "\"1\"^^<xs:decimal>",
13+ # ' "\"2\"^^<xs:decimal>",
14+ # ' '"Example observation"'
15+ # ' )
16+ # ' )
1317# '
1418# ' examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl")
1519# '
16- # ' dataset_ttl_write(tdf= tdf, file_path = examplefile)
20+ # ' dataset_ttl_write(tdf = tdf, file_path = examplefile)
1721# '
1822# ' readLines(examplefile)
1923# ' @export
2024
21- ttl_statements_write <- function (tdf , file_path , overwrite = TRUE ) {
25+ ttl_statements_write <- function (tdf , file_path , overwrite = TRUE ) {
2226 # # tdf must be s, o, p
2327 validate_tdf(tdf )
24-
28+
2529 if (overwrite ) {
26- reset_text_file(file_path = file_path )
30+ reset_text_file(file_path = file_path )
2731 } else {
28- write(" \n " , file = file_path , append = TRUE )
32+ write(" \n " , file = file_path , append = TRUE )
2933 }
3034
3135 unique_subjects <- unique(tdf $ s )
32-
33- lapply ( unique_subjects , function (x ) write_statements(x , tdf , file_path ))
34-
36+
37+ lapply( unique_subjects , function (x ) write_statements(x , tdf , file_path ))
38+
3539 invisible (TRUE )
3640}
3741
3842# ' @keywords internal
3943get_class_definition <- function (instance ) {
4044 return_value <- which(instance $ p == " a" )[1 ]
41- if (length(return_value )== 0 ) stop(instance $ s , " has no class definition." )
45+ if (length(return_value ) == 0 ) stop(instance $ s , " has no class definition." )
4246 return_value
4347}
4448
@@ -48,39 +52,42 @@ write_statements <- function(x, tdf, file_path) {
4852 instance_definition <- get_class_definition(instance )
4953 instance_rows <- 1 : nrow(instance )
5054 instance_rows <- instance_rows [! instance_rows %in% instance_definition ]
51-
52- definition_statement <- paste0(instance $ s [instance_definition ],
53- " a " ,
54- instance $ o [instance_definition ], " ;" )
55-
56- if (length(instance_rows )> 0 ) {
57- write(x = paste0(definition_statement ),
58- file = file_path , append = T
55+
56+ definition_statement <- paste0(
57+ instance $ s [instance_definition ],
58+ " a " ,
59+ instance $ o [instance_definition ], " ;"
60+ )
61+
62+ if (length(instance_rows ) > 0 ) {
63+ write(
64+ x = paste0(definition_statement ),
65+ file = file_path , append = T
5966 )
6067 statements <- c(
61- # further statements
68+ # further statements
6269 unique(paste0(" " , instance $ p [instance_rows ], " " , instance $ o [instance_rows ], " ;" ))
6370 )
6471
6572 # last statement must end with a .
6673 last_statement <- statements [length(statements )]
67- statements [length(statements )] <- paste0(substr(last_statement ,1 , nchar(last_statement )- 1 ), " ." )
68- write( statements ,
69- file = file_path , append = T
70- )
71- write( " \n " ,
72- file = file_path , append = T
73- )
74-
75- } else {
76- # # there is only a class definition
77- write( x = paste0(definition_statement , " ." ),
78- file = file_path , append = T
79- )
80-
81- write( " \n " ,
82- file = file_path , append = T
83- )
84- # # what if no class definition?
85- }
74+ statements [length(statements )] <- paste0(substr(last_statement , 1 , nchar(last_statement ) - 1 ), " ." )
75+ write(statements ,
76+ file = file_path , append = T
77+ )
78+ write(" \n " ,
79+ file = file_path , append = T
80+ )
81+ } else {
82+ # # there is only a class definition
83+ write(
84+ x = paste0(definition_statement , " ." ),
85+ file = file_path , append = T
86+ )
87+
88+ write(" \n " ,
89+ file = file_path , append = T
90+ )
91+ # # what if no class definition?
92+ }
8693}
0 commit comments