1- # ////////////////////////////////////////////////////////////
1+ #   ////////////////////////////////////////////////////////////
22# 
33#                        Helper functions
44# 
5- # ////////////////////////////////////////////////////////////
5+ #   ////////////////////////////////////////////////////////////
66
77
88# ' Create empty dataframe while keeping columns names
9- # '  
9+ # '
1010# ' Also works on lists if dataframes are contained as list elements.
1111# ' @param x Dataframe or list.
1212# ' @export
1313# ' @keywords internal
14- # ' 
15- emptify_object  <-  function (x ) 
16- {
14+ # '
15+ emptify_object  <-  function (x ) {
1716  if  (is.data.frame(x )) {
1817    return (x [integer(0 ), ])
1918  }
20-    
19+ 
2120  for  (nm  in  names(x )) {
2221    df  <-  x [[nm ]]
2322    if  (is.data.frame(df )) {
24-       df  <-  df [integer(0 ), ]  
23+       df  <-  df [integer(0 ), ]
2524    } else  {
26-       df  <-  list ()   
25+       df  <-  list ()
2726    }
2827    x [[nm ]] <-  df 
2928  }
@@ -32,65 +31,62 @@ emptify_object <- function(x)
3231
3332
3433# ' Default datatable output when loading
35- # '  
34+ # '
3635# ' @param text Text to be shown
3736# ' @param header Table header, defaults to an  empty string.
3837# ' @export
3938# ' @keywords internal
40- dt_default  <-  function (text  =  " Waiting for data ..." 
41-                        header  =  " " 
42- {
39+ dt_default  <-  function (text  =  " Waiting for data ..." 
40+                        header  =  " " 
4341  df  <-  data.frame (col1  =  text )
4442  names(df ) <-  header 
45-    
43+ 
4644  #  create datatable and format
4745  df  %> %
48-     (DT :: datatable )(filter  =  " none"   
49-                selection  =  " none"   
50-                colnames  =  header ,
51-                class  =  ' compact' rownames  =  FALSE ,  
52-                options  =  list (
53-                  dom  =  ' t ' ,   
54-                  ordering  =  TRUE ,
55-                  pageLength  =  10 
56-                )
57-    )
46+     (DT :: datatable )(filter  =  " none" 
47+       selection  =  " none" 
48+       colnames  =  header ,
49+       class  =  " compact" rownames  =  FALSE ,
50+       options  =  list (
51+         dom  =  " t " , 
52+         ordering  =  TRUE ,
53+         pageLength  =  10 
54+       )
55+      )
5856}
5957
6058
6159# ' String splitter for comma separated values in Excel cell
62- # '  
60+ # '
6361# ' @param x String to be split
6462# ' @export
6563# ' @keywords internal
6664# ' @examples
67- # ' 
68- # ' cell_text_split("10, 20,30" )
69- # ' cell_text_split("; ,  10  ,,,  20;30,," ) # very robust
70- # ' 
71- cell_text_split  <-  function (x )
72- {
73-   x  %> % 
74-     stringr :: str_replace_all("  +|;+" " ," > %   #  space, semicolon to comma
75-     stringr :: str_replace_all(" ,+" " ," > %      #  several commas to one
76-     stringr :: str_replace_all(" ^,+|,+$" " " > %  #  remove leading and trainling commas
77-     strsplit(" ," #  split string at comma
65+ # '
66+ # ' cell_text_split("10, 20,30")
67+ # ' cell_text_split("; ,  10  ,,,  20;30,,") # very robust
68+ # '
69+ cell_text_split  <-  function (x ) {
70+   x  %> %
71+     stringr :: str_replace_all("  +|;+" " ," > % #  space, semicolon to comma
72+     stringr :: str_replace_all(" ,+" " ," > % #  several commas to one
73+     stringr :: str_replace_all(" ^,+|,+$" " " > % #  remove leading and trainling commas
74+     strsplit(" ," #  split string at comma
7875}
7976
8077
8178# ' @rdname make-names
8279# ' @export
83- make_names_vec  <-  function (x ) 
84- {
80+ make_names_vec  <-  function (x ) {
8581  nms  <-  tolower(x )
86-   nms  <-  stringr :: str_replace_all(nms , " [[:blank:]]+" " _"        #  replace blanks
87-   nms  <-  stringr :: str_replace_all(nms , " \\ .|-|/|\\ (|\\ )|&|\\ ?" " _"   #  replace . - \ ? to _ (underscore)
88-   nms  <-  stringr :: str_replace_all(nms , " [_]+" " _"                #  replace multiple underscores by one
89-   nms  <-  stringr :: str_replace_all(nms , " [_]+$" " "                #  remove trailing underscores
90-   nms  <-  stringr :: str_replace_all(nms , " \u 00DF" " ss"                 
91-   nms  <-  stringr :: str_replace_all(nms , " \u 00E4" " ae"             #  replace German umlauts by their two letter ASCII version
92-   nms  <-  stringr :: str_replace_all(nms , " \u 00FC" " ue"   
93-   nms  <-  stringr :: str_replace_all(nms , " \u 00F6" " oe"   
82+   nms  <-  stringr :: str_replace_all(nms , " [[:blank:]]+" " _" #  replace blanks
83+   nms  <-  stringr :: str_replace_all(nms , " \\ .|-|/|\\ (|\\ )|&|\\ ?" " _" #  replace . - \ ? to _ (underscore)
84+   nms  <-  stringr :: str_replace_all(nms , " [_]+" " _" #  replace multiple underscores by one
85+   nms  <-  stringr :: str_replace_all(nms , " [_]+$" " " #  remove trailing underscores
86+   nms  <-  stringr :: str_replace_all(nms , " \u 00DF" " ss" 
87+   nms  <-  stringr :: str_replace_all(nms , " \u 00E4" " ae" #  replace German umlauts by their two letter ASCII version
88+   nms  <-  stringr :: str_replace_all(nms , " \u 00FC" " ue" 
89+   nms  <-  stringr :: str_replace_all(nms , " \u 00F6" " oe" 
9490  nms 
9591}
9692
@@ -101,23 +97,22 @@ make_names_vec <- function(x)
10197# ' @keywords internal
10298# ' @rdname make-names
10399# ' @export
104- make_names  <-  function (x ) 
105- {
106-   if  (! is.data.frame(x ))
100+ make_names  <-  function (x ) {
101+   if  (! is.data.frame(x )) {
107102    stop(" x muste be a dataframe" 
108-   
109-   names(x ) <-  make_names_vec( names(x ) )
103+   }
104+ 
105+   names(x ) <-  make_names_vec(names(x ))
110106  x 
111107}
112108
113109
114110# ' Format as x digit number
115- # '  
111+ # '
116112# ' @export
117113# ' @keywords internal
118- # ' 
119- fnum  <-  function (x , digits  =  2 )
120- {
114+ # '
115+ fnum  <-  function (x , digits  =  2 ) {
121116  x  <-  round(x , digits )
122117  formatC(x , digits  =  digits , format  =  " f" 
123118}
0 commit comments