Skip to content

Commit daec6ce

Browse files
v1.1
1 parent 01f3843 commit daec6ce

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

R/calendR.R

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
#' @param mbg.col Background color of the month names. Defaults to "white".
3939
#' @param legend.pos If `gradient = TRUE`, is the position of the legend. It can be set to `"none"` (default), `"top"`, `"bottom"`, `"left"` and `"right"`.
4040
#' @param legend.title If `legend.pos != "none"` and `gradient = TRUE`, is the title of the legend.
41-
#' @param pdf Boolean. If `TRUE`, saves the calendar in the working directory in A4 format.
42-
#' @param doc_name If `pdf = TRUE`, is the name of the generated file (without the file extension). If not specified, creates files of the format: `Calendar_year.pdf` for yearly calendars and `Calendar_month_year.pdf` for monthly calendars.
4341
#' @param bg.col Background color of the calendar. Defaults to "white".
4442
#' @param bg.img Character string containing the URL or the local directory of a image to be used as background.
43+
#' @param margin Numeric. Allows controlling the margin of the calendar.
4544
#' @param lunar Boolean. If `TRUE`, draws the lunar phases. Only available for monthly calendars.
4645
#' @param lunar.col If `lunar = TRUE`, is the color of the hide part of the moons.
4746
#' @param lunar.size If `lunar = TRUE`, is the size of the representation of the moons.
47+
#' @param pdf Boolean. If `TRUE`, saves the calendar in the working directory in A4 format.
48+
#' @param doc_name If `pdf = TRUE`, is the name of the generated file (without the file extension). If not specified, creates files of the format: `Calendar_year.pdf` for yearly calendars and `Calendar_month_year.pdf` for monthly calendars.
49+
#' @param papersize PDF paper size. Possible options are `"A6"`, `"A5"`, `"A4"` (default), `"A3"`, `"A2"`, `"A1"` and `"A0"`.
4850
#'
4951
#' @author
5052
#' \itemize{
@@ -119,21 +121,25 @@ calendR <- function(year = format(Sys.Date(), "%Y"),
119121
legend.pos = "none",
120122
legend.title = "",
121123

122-
pdf = FALSE,
123-
doc_name = "",
124-
125124
bg.col = "white",
126125
bg.img = "",
127126

127+
margin = 1,
128+
128129
lunar = FALSE,
129130
lunar.col = "gray60",
130-
lunar.size = 7) {
131+
lunar.size = 7,
132+
133+
pdf = FALSE,
134+
doc_name = "",
135+
papersize = "A4") {
131136

132137
if(year < 0) {
133138
stop("You must be kidding. You don't need a calendar of a year Before Christ :)")
134139
}
135140

136141
wend <- TRUE
142+
l <- TRUE
137143

138144
if((!is.null(start_date) & is.null(end_date))) {
139145
stop("Provide an end date with the 'end_date' argument")
@@ -157,6 +163,7 @@ calendR <- function(year = format(Sys.Date(), "%Y"),
157163

158164
match.arg(start, c("S", "M"))
159165
match.arg(orientation, c("landscape", "portrait", "l", "p"))
166+
match.arg(papersize, c("A6", "A5", "A4", "A3", "A2", "A1", "A0"))
160167

161168

162169
if(!is.null(month)){
@@ -198,6 +205,7 @@ calendR <- function(year = format(Sys.Date(), "%Y"),
198205
if(!is.null(start_date) & !is.null(end_date)){
199206

200207
if(lunar == TRUE) {
208+
l <- FALSE
201209
warning("Lunar phases are only available for monthly calendars")
202210
}
203211

@@ -249,7 +257,6 @@ calendR <- function(year = format(Sys.Date(), "%Y"),
249257
} else {
250258
wend <- FALSE
251259
}
252-
253260
}
254261

255262

@@ -409,7 +416,7 @@ calendR <- function(year = format(Sys.Date(), "%Y"),
409416

410417
if(is.null(month)) {
411418

412-
if(lunar == TRUE) {
419+
if(lunar == TRUE & l != FALSE) {
413420
warning("Lunar phases are only available for monthly calendars")
414421
}
415422

@@ -446,7 +453,7 @@ calendR <- function(year = format(Sys.Date(), "%Y"),
446453
plot.title = element_text(hjust = 0.5, size = title.size, colour = title.col),
447454
plot.subtitle = element_text(hjust = 0.5, face = "italic", colour = subtitle.col, size = subtitle.size),
448455
legend.position = legend.pos,
449-
plot.margin = unit(c(1, 0.5, 1, 0.5), "cm"),
456+
plot.margin = unit(c(1 * margin, 0.5 * margin, 1 * margin, 0.5 * margin), "cm"),
450457
text = element_text(family = font.family, face = font.style),
451458
strip.placement = "outsite")
452459

@@ -509,7 +516,7 @@ calendR <- function(year = format(Sys.Date(), "%Y"),
509516
plot.title = element_text(hjust = 0.5, size = title.size, colour = title.col),
510517
plot.subtitle = element_text(hjust = 0.5, face = "italic", colour = subtitle.col, size = subtitle.size),
511518
legend.position = legend.pos,
512-
plot.margin = unit(c(1, 0, 1, 0), "cm"),
519+
plot.margin = unit(c(1 * margin, 0, 1 * margin, 0), "cm"),
513520
text = element_text(family = font.family, face = font.style),
514521
strip.placement = "outsite")
515522

@@ -527,6 +534,40 @@ calendR <- function(year = format(Sys.Date(), "%Y"),
527534

528535
if(pdf == TRUE) {
529536

537+
switch (papersize,
538+
A6 = {
539+
a <- 148
540+
b <- 105
541+
542+
},
543+
A5 = {
544+
a <- 210
545+
b <- 148
546+
547+
},
548+
A4 = {
549+
a <- 297
550+
b <- 210
551+
},
552+
A3 = {
553+
a <- 420
554+
b <- 297
555+
},
556+
A2 = {
557+
a <- 594
558+
b <- 420
559+
},
560+
A1 = {
561+
a <- 841
562+
b <- 594
563+
},
564+
A0 = {
565+
a <- 1189
566+
b <- 841
567+
},
568+
)
569+
570+
530571
if(doc_name == "") {
531572
if(!is.null(month)) {
532573

@@ -547,10 +588,10 @@ calendR <- function(year = format(Sys.Date(), "%Y"),
547588

548589
if(orientation == "landscape" | orientation == "l") {
549590
ggsave(filename = if(!file.exists(doc_name)) doc_name else stop("File does already exist!"),
550-
height = 210, width = 297, units = "mm")
591+
height = b, width = a, units = "mm")
551592
} else {
552593
ggsave(filename = if(!file.exists(doc_name)) doc_name else stop("File does already exist!"),
553-
width = 210, height = 297, units = "mm")
594+
width = b, height = a, units = "mm")
554595
}
555596
}
556597

man/calendR.Rd

Lines changed: 13 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)