SHARE
TWEET

Untitled

a guest Aug 19th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ####Packages and Basic Setup####
  2.   #Packages
  3.   library(needs)
  4.   needs(tidyr)
  5.   needs(lubridate)
  6.   needs(officer)
  7.   needs(ical)
  8.   needs(dplyr)
  9.  
  10. ####Define Basic Parameters####
  11.   startDate <- dmy("17-Aug-2019")
  12.   endDate <- dmy("19-Feb-2020")
  13.  
  14.   #Weekdays select 2= Monday, 3=Tuesday etc.
  15.   weekdays<-"Mon"
  16.  
  17.  
  18.  
  19. ####Calculations####
  20.   #Find all days between the dates
  21.   myDates <-seq(from = startDate, to = endDate, by = "days")
  22.  
  23.   #Identify Date of Weekday
  24.     select.vec<-which(wday(myDates) %in% c(2,4))
  25.    
  26.   #Subset Data by Weekday
  27.    
  28.     date.vec<-myDates[select.vec]
  29.     date.vec<-as.data.frame(date.vec)
  30.     date.vec$holiday<-""
  31.    
  32.  
  33.   #Generate Number Vector
  34.     number.vec<-seq(1,length=length(date.vec))
  35.  
  36.   #Generate Vector of Heading Names (German Format)
  37.    
  38.     name.vec1<-paste("Sitzung",number.vec,sep=": ")
  39.     date.vec1<-paste(day(date.vec),month(date.vec), sep=".")
  40.    
  41. ####Include Breaks and Holidays####
  42.    
  43.    
  44. ####Check for Public Holidays####
  45.     #Read in Public Holidays for Berlin - find a better source than this and do this for each state?
  46.         ics_raw = readLines("http://i.cal.to/ical/58/berlin/feiertage/a6afca09.3e559e14-5d35ef26.ics")
  47.    
  48.         holidays.ger = ical_parse_df(text=ics_raw)
  49.        
  50.         #Arrange Calender for easier Reading
  51.         holidays.ger<-holidays.ger %>%
  52.           arrange(start) %>%
  53.           select(start,end,summary)
  54.        
  55.         #Generate cleaned time vector
  56.         holidays.ger$date<-as.Date(ymd_hms(holidays.ger$start), "%a, %d %b %Y" , tz="GMT")
  57.        
  58.        
  59.         #Compare holiday vector to own dataset
  60.         holiday.vec<-date.vec$date.vec  %in% holidays.ger$date
  61.        
  62.        
  63.           #Compare own dataset to holiday data frame
  64.         holiday.name.vec<-   holidays.ger[holidays.ger$date %in% date.vec$date.vec,]$summary
  65.        
  66.         #Note if a day is a holiday in data.frame of dates
  67.         date.vec[holiday.vec,]$holiday<-as.character(holiday.name.vec)
  68.        
  69.  
  70.        
  71.        
  72.          
  73. ####Write Output to Word Document####
  74.     #Generate basic document
  75.     doc.full<-read_docx() %>%
  76.       body_add_par("Seminarplan", style="Normal")
  77.    
  78.    
  79.     #Generate Date Section(s) that are right-oriented for each of the dates (German format)
  80.         list.dates<-list()
  81.         for(i in 1:length(date.vec1)){
  82.           temp.date<-fpar(paste(day(date.vec[i,]$date.vec),
  83.                                 month(date.vec[i,]$date.vec),
  84.                                 year(date.vec[i,]$date.vec),
  85.                                 sep="."),
  86.                           fp_p = fp_par(text.align = "right"))
  87.           list.dates[[i]]<-temp.date
  88.         }
  89.        
  90.       #Generate Holiday Names
  91.         list.holidays<-list()
  92.        
  93.         for(i in 1:length(date.vec1)){
  94.           temp.holiday<-fpar(paste(date.vec[i,]$holiday),
  95.                           fp_p = fp_par(text.align = "center"))
  96.           list.holidays[[i]]<-temp.holiday
  97.         }
  98.        
  99.    
  100.    
  101.     #Write seminar date in for loop
  102.     for (j in 1:length(date.vec1)){
  103.     doc.full<-doc.full %>%
  104.       body_add_par(value = name.vec1[j], style =  "heading 1") %>%
  105.       body_add_fpar(list.dates[[j]]) %>%
  106.       body_add_fpar(list.holidays[[j]])
  107.     }
  108.    
  109.     #Write Output
  110.     doc.full%>%
  111.       print(target = "body_add_demo.docx")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top