R markdown Knit to HTML : Error in library(dplyr) - r

I am working on R markdown script (RStudio). Individual code chunk works but when i try to Knit to HTML , it throws error as "Error in library(dplyr) : there is no package called 'dplyr' Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> library"
code
##install library
```{r install pkg,include=FALSE,cache=TRUE}
install.packages("kableExtra",repos = "http://cran.us.r-project.org")
install.packages("dplyr", repos = "http://cran.us.r-project.org")
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
```
```{r setup, include=FALSE}
library(knitr)
library(dplyr)
library(ggplot2)
```

Restart R
You should manually install packages and then knit to HDML. Click Tools > Install Packages and then type in the packages

Related

Unable to install packages from RMarkdown

I'm working on a project in RStudio, and all the code runs well. but when I try to get a report in R Makdown, I constantly recieve this error on a chunk which contains installing needed packages:
```{r installing packages, include=FALSE}
install.packages("tidyverse")
library(tidyverse)
install.packages("lubridate")
library(lubridate)
install.packages("ggplot2")
library(ggplot2)
install.packages("dplyr")
library(dplyr)
```
this is the error message: (line 21-32 means all the mentioned chunk.)
Quitting from lines 21-32 (report.Rmd)
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror
Calls: <Anonymous> ... eval_with_user_handlers -> eval -> eval -> install.packages -> contrib.url
Execution halted
any recommendation??
I got the error on first chunk, but each chunk runs perfectly with no error when I run them separately.

Unable to load flextable

It shows there is no package called 'flextable' even after installing the package.
install.packages("flextable")
library(flextable)

Knitting error when converting to pdf on rstudio

I keep getting this error when I try to knit to PDF using R Markdown. It has worked before, and I tried installing the package contrib.url but it says it is not available for my version. Any suggestions?
Error in contrib.url(repos, "source") : trying to use CRAN without setting a mirror calls : <Anonymous> ... with visible -> eval -> eval -> install.packages -> contrib.url Execution halted
The error usually comes when you don't set a mirror in the install.packages command. You probably have a call to install.packages somewhere in your markdown document.
Try the following:
install.packages("aPackage",repos = "http://cran.us.r-project.org")
So just add the repos part. You could of course also just install the package before usage and remove the install.packages line completely from your rmarkdown.

R file won't knit, saying readxl package doesn't exist

I'm using RStudio, and trying to knit my file to hand in my assignment. I installed the package "readxl" to read in a couple of excel files, and it worked fine in the actual coding of the assignment. But when I go to knit the file, I keep getting the error:
Error in library(readxl) : there is no package called 'readxl'
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval ->
library
Execution halted
This is the code in question:
install.packages("tidyverse",repos = "http://cran.us.r-project.org")
library(tidyverse)
install.packages("readxl",repos = "http://cran.us.r-project.org")
library(readxl)
This has been frustrating me for over an hour and I just can't fix it. I can see the readxl package to the right of the console, in the "packages" tab. Any help will be much appreciated

knitr unable to load showtext / sysfonts

I've been using knitr along with the showtext library to produce HTML files that use some Google fonts. It has worked fine until I upgraded R to 3.4. When I knit the document now, I get the following error:
Error: package 'sysfonts' could not be loaded Execution halted
This is reproducible by knitting:
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(showtext)
```
It runs fine without the library(showtext).
Then, when I try running it with library(sysfonts) using the same template:
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(sysfonts)
```
I get this error:
Error: package or namespace load failed for 'sysfonts': .onLoad failed in loadNamespace() for 'sysfonts', details: call: dyn.load(file, DLLpath = DLLpath, ...) error: unable to load shared object '~/Library/R/3.4/library/sysfonts/libs/sysfonts.so': dlopen(~/Library/R/3.4/library/sysfonts/libs/sysfonts.so, 6): Library not loaded: /opt/X11/lib/libfreetype.6.dylib Reference from: ~/Library/R/3.4/library/sysfonts/libs/sysfonts.so Reason: image not found
It seems like an issue with the sysfonts library. However, I can load showtext and sysfonts just fine from the console. So I don't know if this is a sysfonts issue or a knitr issue. I'm running El Capitan 10.6 and again, recently upgraded R to 3.4. I tried downgrading back to 3.3, but still produced the same error.
A late reply, which yet might come useful to others (if working on a Mac):
1) remove all relevant R-packages
remove.packages("sysfonts")
remove.packages("showtext")
2) install XQuartz
3) reinstall and load all packages again
install.packages("sysfonts")
install.packages("showtext")
This solution provided by tcarnus on GitHub solved the issue for me.

Resources