RMarkdown: "Error: path for html_dependency not found:" - r

I am a Chemist who uses R and R Studio to analyze mass spectrometry data. I have automated scripts that process my data, because there are a lot of files. The scripts call rmarkdown using render() to import data, manipulate the data frame, save the processed data frame as a .csv and generate an html of plots. I have a problem rendering rmarkdown files since I recently updated to R v3.5.1 and R Studio v1.1.463. The error I receive is: Error: path for html_dependency not found: which I get if I use the knit button in R Studio, or if I use render(). The script inside the rmarkdown runs to completion, as the resulting objects are present in the environment window in R Studio, but the html file doesn’t render, and I get the error which stops the loop of render() I use to process all the files in a specific folder. This is a process I have done many times before updating.
I ran traceback() and got the following results:
8: stop("path for html_dependency not found: ", file, call. = FALSE)
7: FUN(X[[i]], ...)
6: lapply(dependencies, validate_html_dependency)
5: dependency_resolver(all_dependencies)
4: html_extras_for_document(knit_meta, runtime, dependency_resolver,
format_deps)
3: base(...)
2: output_format$pre_processor(yaml_front_matter, utf8_input, runtime,
knit_meta, files_dir, output_dir)
1: render("Processing and QA Template_INT_FINAL_MFAssignR.rmd",
output_file = paste0(substr(file_list[i], 1, nchar(file_list[i]) -
4), ".html"), output_dir = folder, params = list(data = file_list[i], path = folder))
I have uninstalled and reinstalled packages several times. I even removed my library folder entirely once, then reinstalled. My packages are up to date, but I had problems installing packages since the update, including rmarkdown. I had to use install.packages(“package”, type=”binary”) to install the dependencies for rmarkdown in order to get it to install. Normally I can just use the Install button in R Studio.
This is a work PC (Windows 10, 64 bit) I do not have administrator access to. I have to uninstall/install through IT at my university, which is a hassle, so I want to come to them with a plan. My package library defaults to a network drive that I can read/write to, and I have limited access to the hard disks; either way I can’t seem to change where packages are installed in R Studio. I don’t know if I can re-install an older version of R and R Studio or if it would help. A lot of the packages I use are developed for the current version of R and I was having some other issues, which was why I updated in the first place. The exact same scripts and data files run properly on another PC (my personal laptop, which is also up to date on R, R Studio and packages); the only changes I made are to the working directory so the data loads properly. I also had no trouble installing packages.
Here is an example of my code; since my scripts are very long, complicated and data specific, I have prepared a more simplified version of what they do as an example. I really don’t think there is anything wrong with the scripts themselves, as I mentioned before I have run them many times before the update. I suspect something is wrong with either the install of R, R studio or rmarkdown.
The main script, which calls render() from rmarkdown:
setwd("D:/Working Directory")
library("rmarkdown")
folder=paste0(getwd(),"/")
file_list=list.files(path=folder, pattern="*_MF.csv")
for (i in 1:length(file_list)){
render("Processing and QA Template.rmd",
output_file = paste0(substr(file_list[i],1, nchar(file_list[i])-4),".html"), output_dir = folder,
params=list(data=file_list[i], path=folder))
}
The rmarkdown, named “Processing and QA Template.rmd” which is in "D:/Working Directory":
---
title: "Example Processing and QA"
author: "Matt Brege "
date: "2018-12-12"
output: html_document
params:
data: x
path: x
---
```{r, echo=FALSE, message=FALSE}
library(dplyr)
library(ggplot2)
library(tidyr)
file_name <- substr(params$data, 1, nchar(params$data)-7)
folder <- params$path
input <- tbl_df(read.csv(paste0(file_name, "_MF.csv"), stringsAsFactors = FALSE))
```
```{r, echo=FALSE}
#...a series of long and complicated data manipulations later…
write.csv(input7, paste0("Output/", file_name, "_QAd.csv"), row.names=FALSE, na="")
```
```{r,r, echo=FALSE, warning=FALSE, message=FALSE}
#...plotting section…
# these are just examples
p1 <-ggplot(diamonds, aes(x=carat, y=price)) + geom_point()
print(p1)
p2<- ggplot(diamonds, aes(x=carat, y=price, color=clarity)) + geom_point()
print(p2)
p3<- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point()
print(p3)
```
Finally, I have found the following similar posts about what appears to be the same issue, but they are old and from different versions of R, and it doesn’t seem like it was resolved in those instances anyway:
RMarkdown cannot knit: html_dependency not found
RNotebook cannot output due to html_dependency not found
R looks in the wrong place for html dependency
One suggestion from the last link was to clear the cache: “If you cached the chunk containing HTML widgets, you may need to invalidate the cache after you update R packages. – Yihui Xie Dec 6 '17 at 19:00” but I am not exactly sure what that means or how to do it. I generally run cat("\014") and rm(list=ls()) at the beginning of each script, but I don’t know if that’s what the suggestion means, and it has not helped.

I got it to work by:
Clear Knitr Cashe... (see the little arrow next to the Knit-icon in RStudio).
I also had to restart R (.rs.restartR() ).

Related

Why does R Markdown not want to Knit with spss file?

I performed a multilevel analysis in R (following Kay Chansiri's tutorial but using my df). It worked as it should, but then I wanted to do it again in R Markdown so as to make it into a report. The code works fine in R Markdown, but it will not knit it. Everything seems to revolve around the spss file I am using.
Here is the code from the first chunk:
```{r MLM_deJong}
MLM_deJong<-read_sav("dataset_MA2.sav")
View(MLM_deJong)
```
And the next chunk:
```{r}
MLM_deJong = MLM_deJong %>% replace_with_na(replace = list(business = 999))
mean(MLM_deJong$business, na.rm = T)
MLM_deJong$business[is.na(MLM_deJong$business)] <- mean(MLM_deJong$business, na.rm = T)
```
and so on...
R imports the df and performs all the operations. But when I want to knit it, here is what the R Markdown tab in the console writes:
Line 46Error in read_sav("dataset_MA2.sav) : could not find function "read_sav" calls: ... handle -> withCallHandlers -> withVisible -> eval -> eval.
Needless to mention, no file is knit.
I have of course installed the 'haven' package. The same occurs when I tried read.spss through the 'foreign' package.
Even though you have installed the haven package, it is not automatically loaded when rmarkdown knits.
Option 1: Load the haven package before you use it.
library(haven)
MLM_deJong<-read_sav("dataset_MA2.sav")
Option 2: Prepend haven:: to read_sav
MLM_deJong<-haven::read_sav("dataset_MA2.sav")

Error there is no package called RevoUtilsMath with R Markdown

I am running R 3.4.3 and 3.5.1 (non-Microsoft version) and RStudio version 1.1.456. I am trying to knit some code into RMarkdown. However, I get the following error:
Error in library(p, character.only = TRUE) : there is no package called 'RevoUtilsMath'Calls: <Anonymous> ... suppressPackageStartupMessages -> withCallingHandlers -> library
Execution halted
The package RevoUtilsMath is part of the MKL install with Microsoft R. I cannot install it as a supplemental package with 'regular R'. The script itself runs fine, it just does not work in R Markdown.
The following libraries are loaded:
```{r loadLibraries, echo=FALSE, warning=FALSE}
library(RODBC)
library(dplyr)
library(markovchain)
library(DT)
library(reshape2)
library(knitr)
library(ggplot2)
library(scales)
library(PerformanceAnalytics)
library(plotly)```
The missing package error happens when executing the code below in Markdown. It is called using this code.
```{r histogram1, echo=FALSE, cache=TRUE}```
The histogram1 code is below (very standard ggplot).
g <- ggplot(dataClean, aes(x = IncSnapshotDay, fill = Represent)) +
geom_histogram(bins=70, alpha = .8) +
scale_fill_manual(values = colors) +
scale_x_continuous(labels = comma, limits = c(0,40000)) +
facet_wrap(~SnapshotDay) +
ylim(0,4000) +
theme_bryan()
g
I thought that maybe one of these packages has a dependency, so I ran the following to find out.
library(tools)
> dependsOnPkgs('RevoUtilsMath')
It returns character(0) which indicates that none of the packages depend on it. I did a test of the function on ggplot2, and it works
dependsOnPkgs('ggplot2')
[1] "dendextend" "GGally" "ggthemes" "plotly" "viridis" "caret" "crosstalk"
[8] "DT"
So why does R Markdown/knitr generate this error since the code itself runs fine outside of Markdown, and how do I fix this?
I think you are experiencing a problem similar to one I just had though, without more detail, it is hard for me to know.
In my case, the problem was caused by knitr caching the list of packages used by a previous author (using Microsoft R). The immediate solution was to simply clear the knitr cache (via the "Knit" drop down menu in RStudio) before attempting to knit the code.
I still don't really understand why this is happening or how to avoid it in future situations, but this at least provides a way to create the document even in the face of this behavior.

Error in R. Error in gsub("(?<=\n)(?=.|\n)", continue, x, perl = TRUE) :

I am encountering an error in R that I cannot seem to figure out. I am creating an R markdown document where I read in an a csv table using this code.
iati <- read.csv(file="/filepath/IATI_NGOS.csv",head=TRUE,sep=",")
and then using ggplot2 I create a plot using the following code.
figure_one <- ggplot(iati, aes(iati$reporting.org))+
geom_bar(fill="blue")+
ylab("Total Activities")+
xlab("NGO Reporting Organizations in IATI")+
ggtitle("Total Number of Activities compared to each NGO Reporting Organization in IATI")+
coord_flip()
When I try to call figure_one in the R markdown I get the following error:
Quitting from lines 44-55 (NGO_IATI.Rmd)
Error in gsub("(?<=\n)(?=.|\n)", continue, x, perl = TRUE) :
input string 1 is invalid UTF-8
Calls: <Anonymous> ... paste -> comment_out -> line_prompt -> paste -> gsub
In addition: Warning message:
In grep("\n", message) : input string 1 is invalid in this locale
Execution halted
When I run this code in a regular R script I have absolutely no issues. I have search for some answers but can't figure it out.
Thanks!
I ended solving my issue by just doing a fresh install of R and Rstudio on my local machine. I think the recent update to Yosemite on my local environment created a lot of issues with the TeX plugin I had installed for R markdown.
I get the same question when I knit my rmarkdown document and find **encoding is the cause.
When you use functions like read.csv, fread or read_csv, you will read the column name.
If column names are in other languages, like Chinese, the problem will easily happen.
Or you rmarkdown works on Windows, but the encoding bug happens on Mac, a different environment.
The temporal solution is to rename the column name in English and resave the data files.
Here is the pseudocode in R to show my idea.
library(data.table)
library(tidyverse)
fread('yourfile.csv',encoding = 'UTF-8') %>%
purrr::set_names(c('x1','x2','x3')) %>%
write_excel_csv('yourfile_2.csv')
Here the new file yourfile_2.csv is fine to rmarkdown knit without encoding problems happening.

slidify + data.table( ggplot2) error

when i try to add (data.table/ggplot2)code in slidify, i got error.
Here is my code in slidify:
## data.table
```{r}
library(data.table)
DT = data.table(x = 1:5, y=6:10)
setkey(DT, x)
DT[J(1)] # Error: No J function
```
---
## ggplot2
```{r}
library(ggplot2)
a = b = shape = 1:5
ggplot(data=DT, aes(a, b, col=shape)) + geom_point() # Error: can not find object a
```
All the code can run outside slidify, so i guess there is something about variable namespace wrong with slidify.
I also find this link:data.table error when used through knitr, gWidgetsWWW which might be similar with my problem, but still don't know how to fix.
Just to add an answer to follow up on the comments on the question. The dev version of data.table fixed it and is now on CRAN (data.table v1.9.4). But this broke kable() in knitr which knitr v1.7 (on CRAN too) fixes.
So basically, upgrade to latest CRAN versions of knitr and data.table and you should be fine. Please let us know if not.
More detail for the curious ...
I've made another change to data.table in v1.9.5 to make it more robust for packages that evaluate user code (like knitr, slidify and gWidgetsWWW) but don't know about data.table themselves. So that they don't need to know in future. Here is the item :
knitr::kable() works again without needing to upgrade from knitr v1.6 to v1.7. Packages which evaluate user code and don't wish to import data.table need to be added to data.table:::cedta.pkgEvalsUserCode and now only the eval part is made data.table-aware (the rest of such package's code is left data.table-unaware). data.table:::cedta.override is now empty and will be deprecated if no need for it arises.
And here is the item in v1.9.4 that was a little too over-reaching and broke knitr::kable in knitr v1.6 and knitr v1.7 fixes (but shouldn't have needed to) :
Added shiny, rmarkdown and knitr to the data.table whitelist. Packages which take user code as input and run it in their own environment (so do not Depend or Import data.table themselves) either need to be added here, or they can define a variable .datatable.aware <- TRUE in their namepace, so that data.table can work correctly in those packages. Users can also add to data.table's whitelist themselves using assignInNamespace() but these additions upstream remove the need to do that for these packages.

plot.MCA() not included in Sweave

I don't seem to be having a problem with any other method of including a plot via Sweave. However, plot.mca(), a method from the FactoMineR package seems to not have it's plot pulled through. It does create an Rplot.pdf file - but for whatever reason it's not renamed into "RnwFilename-00X.pdf" and not included in the resulting PDF when you compilePdf() it in RStudio.
Here's a trivial example, try it for yourself.
Note you may have to: install.packages("FactoMineR")
\documentclass[a4paper]{article}
% PREAMBLE
\begin{document}
\begin{center}
<<echo=false,fig=true>>=
library(FactoMineR)
x <- data.frame(
A=sample(letters[1:3],100,rep=T),
B=sample(letters[1:4],100,rep=T),
C=sample(letters[1:3],100,rep=T))
fit.mca <- MCA(x, graph=FALSE)
plot(fit.mca, invisible="ind")
#
\end{center}
\end{document}
Update - more details on the error message:
LaTeX errors:
!pdfTeX error: C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\pdflatex.EXE (file
R:/.../RnwFilename-010.pdf): PDF inclusion: required page do
es not exist <0>
It works for me if I tell plot.MCA not to create a new device:
plot(fit.mca, invisible="ind",new.plot = FALSE)
Editorializing a bit, this seems like sub-optimal behavior for a plotting function, which most users (and other code, clearly) will expect to rely on R's default action to open a new device automatically. A plot function should only open a new device if the user has explicitly told it to (either by calling png, pdf etc or by actually setting new.plot = TRUE). Opinions may differ on this, though.

Resources