svmiller / stevetemplates

My collection of R Markdown templates, as an R package.
http://svmiller.com/stevetemplates
126 stars 22 forks source link

Mac R studio issues kniting syllabus #16

Open Condamine opened 1 year ago

Condamine commented 1 year ago

Apologies in advance if this is a straightforward issue to resolve, but trying to generate a syllabus from your template on my Mac (2022.07.0+548) generates the following error @ line 26:

Error in dev.control(displaylist = if (record) "enable" else "inhibit") : dev.control() called without an open graphics device Calls: ... call_block -> block_exec -> eng_r -> chunk_device -> dev.control

...as well as the following warnings:

In addition: Warning messages: 1: In grSoftVersion() : unable to load shared object '/Library/Frameworks/R.framework/Resources/modules//R_X11.so': dlopen(/Library/Frameworks/R.framework/Resources/modules//R_X11.so, 0x0006): Library not loaded: '/opt/X11/lib/libSM.6.dylib' Referenced from: '/Library/Frameworks/R.framework/Versions/4.2/Resources/modules/R_X11.so' Reason: tried: '/opt/X11/lib/libSM.6.dylib' (no such file), '/Library/Frameworks/R.framework/Resources/lib/libSM.6.dylib' (no such file), '/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/server/libSM.6.dylib' (no such file) 2: In cairoVersion() : unable to load shared object '/Library/Frameworks/R.framework/Resources/library/grDevices/libs//cairo.so': dlopen(/Library/Frameworks/R.framework/Resources/library/grDevices/libs//cairo.so, 0x0006): Library not loaded: '/opt/X11/lib/libXrender.1.dylib' Referenced from: '/Library/Frameworks/R.framework/Versions/4.2/Resources/library/grDevices/libs/cairo.so' Reason: tried: '/opt/X11/lib/libXrender.1.dylib' (no such file), '/Library/Frameworks/R.framework/Resources/lib/libXrender.1.dylib' (no such file), '/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/server/libXrender.1.dylib' (no such file) 3: In (function (filename = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", : failed to load cairo DLL

on a PC running R studio and Win 11, no such issues arise & it knits very well. I've changed names, dates, bib location, etc. but not modified the code.

# knitr::opts_chunk$set(cache=FALSE, dev='pdf')
knitr::opts_chunk$set(cache=F,
                      fig.path = 'figs/',
                      cache.path='cache/',
                      warning=F,
                      message=F)

knitr::opts_chunk$set(
                  fig.process = function(x) {
                      x2 = sub('-\\d+([.][a-z]+)$', '\\1', x)
                      if (file.rename(x, x2)) x2 else x
                      }
                  )

advdate <- function(obj, adv) {
 tmon <- obj + 7*(adv-1)
 tfri <- obj + 4 + 7*(adv-1)
 tmon <- format(tmon, format="%m/%d")
 tfri <- format(tfri, format="%m/%d")
 zadv <- sprintf("%02d", adv)
 tmp <- paste("Week ",zadv,sep='',", ", tmon," - ",tfri)
 return(tmp)
}

options(scipen=999)
library(tidyverse)
library(stevemisc)
#library(ggpmisc)
#library(anonymizer)
# library(ggcal)
#library(stringr)
#library(kfigr)
#library(broom)
library(lubridate)

# library(RefManageR)
# library(knitcitations)
# library(rcrossref)
#bib <- ReadBib("/Users/henrysivak/Mylibrary.bib")
#myopts <- BibOptions(bib.style = "authoryear", style="latex", first.inits=FALSE, max.names = 20)

# Create a calendar for your syllabus ----
# Source: http://svmiller.com/blog/2020/08/a-ggplot-calendar-for-your-semester/

# 1) what is the first Monday of the semester?
# Any number of ways to identify dates in R, but we'll use {lubridate} and the ymd() function here.
# Format: YYYYMMDD. In this example, 4 January 2021.

mon <- ymd(20230109)

# What are some dates you won't be here? In this example, I had a conference on 7 January 2021.
# Spring Break was 15 March 2021 to 19 March 2021.
not_here_dates <- c(
  # AAG 2023,
  ymd(20210107),
  # Spring Break
  seq(ymd(20230305),ymd(20230512), by=1))

# You can adjust this as you see fit. Basically: add assignment types (e.g. papers, quizzes).
# My intro class was fairly simple: just exams.
exam_dates <- c(ymd(20210218), ymd(20210401), ymd(20210429))

# What are the full dates of the semester? Here, I'll exclude exam week as I like to do.
# In this case: 6 January to 23 April
semester_dates <- seq(ymd(20210106), ymd(20210423), by=1)

# Custom function for treating the first day of the month as the first week 
# of the month up until the first Sunday (unless Sunday was the start of the month)
wom <- function(date) {
    first <- wday(as.Date(paste(year(date),month(date),1,sep="-")))
    return((mday(date)+(first-2)) %/% 7+1)
  }

# Create a data frame of dates, assign to Cal
tibble(date = seq(ymd(20210101), ymd(20210430), by=1))  %>%
  mutate(mon = lubridate::month(date, label=T, abbr=F), # get month label
         wkdy = weekdays(date, abbreviate=T), # get weekday label
         wkdy = fct_relevel(wkdy, "Sun", "Mon", "Tue", "Wed", "Thu","Fri","Sat"), # make sure Sunday comes first
         semester = ifelse(date %in% semester_dates, 1, 0), # is date part of the semester?
         exams = ifelse(date %in% exam_dates, 1, 0), # is it an exam?
         not_here = ifelse(date %in% not_here_dates, 1, 0), # is it a day off?
         day = lubridate::mday(date), # get day of month to add later as a label
         # Below: our custom wom() function
         week = wom(date)) -> Cal

# Create a category variable, for filling.
# I can probably make this a case_when(), but this will work.

Cal %>%
  mutate(category = NA,
         category = ifelse(semester == 1, "Semester", category),
         category = ifelse(semester == 1 & wkdy %in% c("Tue", "Thu"), "Class Day", category),
         category = ifelse(exams == 1, "Exams", category),
         category = ifelse(is.na(category) | (semester == 1 & not_here == 1), "NA", category)) -> Cal 

Cal %>% 
  ggplot(.,aes(wkdy, week)) +
  # custom theme stuff below
  # theme_steve_web() + 
  theme_bw() +
  theme(panel.grid.major.x = element_blank()) +
  # geom_tile and facet_wrap will do all the heavy lifting
  geom_tile(alpha=0.8, aes(fill=category), color="black", size=.45) +
  facet_wrap(~mon, scales="free", ncol=3) +
  # fill in tiles to make it look more "calendary" (sic)
  geom_text(aes(label=day),family="Open Sans") +
  # put your y-axis down, flip it, and reverse it
  scale_y_reverse(breaks=NULL) +
  # manually fill scale colors to something you like...
  scale_fill_manual(values=c("Class Day"="steelblue", 
                             "Semester"="lightsteelblue",
                             "NA" = "white", # I like these whited out...
                             "Exams"="indianred4"),
                    #... but also suppress a label for a non-class semester day
                    breaks=c("Class Day","Exams")) +
  labs(fill = "", x="", y="",
       caption = "Notable dates: SPSA 2021 (7 January), Spring Break (15-19 March)")  -> class_cal
svmiller commented 1 year ago

I wasn't able to reproduce it on Linux and the errors that I see suggest a re-install of XQuartz might do it. It's definitely an OS issue.

Condamine commented 1 year ago

I wasn't able to reproduce it on Linux and the errors that I see suggest a re-install of XQuartz might do it. It's definitely an OS issue.

Thank you! XQuartz and a new project (w/ only one file open at a time) seems to have solved this.