Generating a Google Sunset Calendar using R on Ubuntu

Notes for generating a Google Sunset Calendar using R. Note, I adjusted the start time 10 min earlier. Thanks to Hilary Parker

https://www.timeanddate.com/sun/usa/parowan
https://hilaryparker.com/2014/05/27/sunsets-in-google-calendar-using-r/
https://www.digitalocean.com/community/tutorials/how-to-install-r-on-ubuntu-16-04-2
http://blog.samsandberg.com/2014/06/04/sunsets-in-google-calendar-for-r-noobs/
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
#sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu trusty/'
#sudo add-apt-repository 'deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/'
sudo apt update
sudo apt upgrade
sudo apt install r-base
wget https://raw.githubusercontent.com/hilaryparker/hilary/master/R/create_sunset_cal.R
vi create_sunset_cal.R
sudo -i R
install.packages("StreamMetabolism")
library(StreamMetabolism)
install.packages("gpclib")
library(maptools)
source("/YourPath/create_sunset_cal.R")
create_sunset_cal()
q()
head /YourPath/sunset.csv
Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private
Sunset,2017-09-03,19:47:54 PM,2017-09-03,20:27:54 PM,False,Sunset Calendar,YourAddress,False
Sunset,2017-09-04,19:46:24 PM,2017-09-04,20:26:24 PM,False,Sunset Calendar,YourAddress,False
Sunset,2017-09-05,19:44:54 PM,2017-09-05,20:24:54 PM,False,Sunset Calendar,YourAddress,False

cat /YourPath/create_sunset_cal.R
#' Create a sunset calendar
#'
#' This function creates a .CSV of sunset appointments--with a user-specified location--that can be imported into Google Calendar.
#' @param date Date at which you want the calendar to start, in yyyy/mm/dd format.
#' @param lat Latitude of location (for sunset time calculation)
#' @param long Longitude of location (for sunset time calculation, will be negative for continental US)
#' @param timezone Timezone of location (for sunset time calculation).
#' @param num.days Number of days you want sunset appointments for.
#' @param file Filename for outputted .CSV file (to be uploaded to Google Calendar).
#' @param location Location of sunset appointment. Will be input into Google Calendar event as the event location.
#' @importFrom StreamMetabolism sunrise.set
#' @export
#' @examples \dontrun{
#' create_sunset_cal(location = "40.7127, -74.0059")
#'}
#'

create_sunset_cal <- function(date="2017/09/03",
                              lat = 40.7127,
                              long = -74.0059,
                              timezone = "America/Denver",
                              num.days = 365,
                              file="sunset.csv",
                              location = "YourAddress"){

  location <- gsub(",", "", location)

  dates <- seq(
    as.Date(date),
    by = "day",
    length.out = num.days
  )

  sunset_times <- sunrise.set(
    lat = lat,
    long = long,
    date = date,
    timezone = timezone,
    num.days = num.days
  )$sunset

  nms <- c(
    'Subject',
    'Start Date',
    'Start Time',
    'End Date',
    'End Time',
    'All Day Event',
    'Description',
    'Location',
    'Private'
  )
  mat <- matrix(
    nrow = length(dates),
    ncol = length(nms)
  )
  mat <- data.frame(mat)
  colnames(mat) <- nms

  mat$Subject <- "Sunset"
  mat$"Start Date" <- dates
  mat$"End Date" <- dates
  mat$"All Day Event" <- "False"
  mat$Description <- "Sunset Calendar"
  mat$Location <- location
  mat$Private <- "False"

  starts <- strftime(sunset_times-60*10, format="%H:%M:%S %p")
  ends <- strftime(sunset_times+60*30, format="%H:%M:%S %p")
  mat$"Start Time" <- starts
  mat$"End Time" <- ends

  write.csv(
    mat,
    file=file,
    quote=FALSE,
    row.names=FALSE
  )

}



No comments: