I am practicing with R Markdown and knitr. I am trying to cache a chunk that takes a little while to compute.
When I run the script without caching that chunk, it does not produce any errors. However, after caching that chunk and re-knitting, I come across an error in a following chunk that calls a variable produced within that previously cached chunk.
I have tried using cache.lazy = FALSE, but this still produces the same error.
Can y'all offer some help on what I am missing?
Here are the chunks that I have set up:
```{r setup}
#tidyverse and friends
library(tidyverse)
library(lubridate)
library(stringr)
library(magrittr)
library(plotly)
library(ggridges)
#parallel Processing
library(doParallel)
library(foreach)
#Modeling
library(MASS) # LDA QDA (this library masks dplyr::select)
library(class) # KNN
library(boot) #CV #bootstrap
library(leaps) #subset selection(probably wont use)
library(glmnet) #Ridge Regression Lasso
library(pls) #PCR PLS
#Miscellaneous
library(jpeg)
game <- read_csv("appstore_games.csv",
col_types = cols(`Original Release Date` = col_date(format = "%d/%m/%Y"),
`Current Version Release Date` = col_date(format = "%d/%m/%Y")))
```
```{r data_Cleanup}
cols <- str_replace_all(names(game), "[ -]", "_")
game %<>% rename_at(vars(names(game)), ~ cols)
```
The problem cached chunk is this one:
```{r image_read, cache = TRUE, cache.lazy = FALSE}
ID_Jpeg <- map_chr(game$ID, ~ str_c("App Icons/", ., ".jpg", sep = ""))
getmode <- function(x){
uniqx <- unique(x)
uniqx[which(tabulate(match(x, uniqx)) == max(tabulate(match(x, uniqx))))]
}
comb <- function(x, ...){
lapply(seq_along(x),
function(i) c(x[[i]], lapply(list(...), function(y) y[[i]])))
}
Icon_Is_Colored <- map_lgl(ID_Jpeg, ~ length(dim(readJPEG(.))) == 3)
ncores <- detectCores(logical = FALSE)
cl <- makeCluster(ncores)
registerDoParallel(cl)
Icon_Color_Measures <- foreach(i=1:length(ID_Jpeg), .combine = "comb", .multicombine = TRUE, .init = list(list(),list()), .packages = c("jpeg", "stringr") )%dopar% {
x <- readJPEG(ID_Jpeg[i]) * 255
if(length(dim(x)) == 3){
R <- as.numeric(x[,,1])
G <- as.numeric(x[,,2])
B <- as.numeric(x[,,3])
}else{ # for grayscale, R = G = B
R <- as.numeric(x)
G <- as.numeric(x)
B <- as.numeric(x)
}
RGB <- str_c(R, G, B, sep = "")
list(getmode(RGB), length(unique(RGB))/length(RGB))
}
stopCluster(cl)
names(Icon_Color_Measures) <- c("Icon_Pixel_Mode", "Icon_Pixel_Ratio")
game %<>% mutate(Icon_Is_Colored = Icon_Is_Colored, Icon_Pixel_Mode = Icon_Color_Measures$Icon_Pixel_Mode,
Icon_Pixel_Ratio = as.numeric(Icon_Color_Measures$Icon_Pixel_Ratio))
```
The following chunk produces the error: object 'Icon_Pixel_Ratio not found
```{r Icon_Ratio}
game %>%
ggplot(aes(as.character(Average_User_Rating), Icon_Pixel_Ratio)) +
geom_violin(aes(fill = as.character(Average_User_Rating), group = as.character(Average_User_Rating))) +
theme(legend.position = "none")
```