Knitr cannot find img files in /static/ folder - r

I have a hugo-academic website (methods101.com) that has been working well for last year.
I just went to edit some pages and I've started getting a new error.
The code that seems to be creating the problems is the knitr::include_graphics() function.
This is an example of the text that produces an error:
{r, echo=FALSE, out.width=600,
fig.cap="Newspaper article in Word document, next to same article on internet.",
fig.align='center'}
knitr::include_graphics("/img/soc224_qual_analysis_eg_figure_1.png")
This is the error message:
Rendering content/docs/SOC224_qual_analysis_eg.Rmd
Quitting from lines 80-81 (SOC224_qual_analysis_eg.Rmd)
Error in knitr::include_graphics("/img/soc224_qual_analysis_eg_figure_1.png") :
Cannot find the file(s): "/img/soc224_qual_analysis_eg_figure_1.png"
Calls: local ... withCallingHandlers -> withVisible -> eval -> eval -> <Anonymous>
Execution halted
<simpleError in render_page(f): Failed to render 'content/docs/SOC224_qual_analysis_eg.Rmd'>
I get the same problem on different computers, and after fresh re-installing/downloading the website contents.
The image file is definitely inside the folder:
/static/img/

You may see the help page ?knitr::include_graphics. In your case, you need
knitr::include_graphics("/img/soc224_qual_analysis_eg_figure_1.png", error = FALSE)

We managed to solve this problem by rolling back to the earlier version of Knitr.
This was the code we used:
remove.packages("knitr")
packageurl <- "https://cran.r-project.org/src/contrib/Archive/knitr/knitr_1.27.tar.gz"
install.packages(packageurl, repos=NULL, type="source")
We had no problems on 5th Feb, but noticed the new version of knitr came out on 6th Feb, and so thought this could be causing the problems.
We don't get the problems with the old version of knitr.
Not sure what the underlying cause is.

Related

Error in R Markdown: could not find function "split_chain"

Hello I am using R markdown and have ran into trouble.
When I try to knit this section of my document (the "example" variable is a data frame with a column called "text" which contains strings):
library(qdap)
frequent_terms <- freq_terms(example$text, 4)
frequent_terms
I get this error message:
Error in split_chain(match.callQ), env = env) :
could not find function "split_chain"
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> %>%
I have updated Rstudio and the relevant packages(such as magrittr) but I am still running into this issue, one I have never encountered.
How do I fix this error, or how to I interpret the error message, I am at a loss here. Thank you.
According to this issue, your problem should be solved if you add qdap first, and magrittr / tidyverse afterward because gdap uses an old version of the pipe and masks the most recent magrittr version.
However, it is not entirely clear since we do not have a complete reproducible example

Getting error: "Error in table(x0) : object 'PONDICE' not found" while trying to knit

Apologies if this has been answered already. I tried to find a solution that would help, but I was not lucky. I have several data set which I took from the CD that came with my textbook. When I run my code in R, there are no problems. When I try to knit (word, pdf, and HTML), I get the error message: Quitting from lines 20-26 (hw1math424_3.Rmd)
Error in table(x0) : object 'PONDICE' not found
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> tab1 -> table
Execution halted.
library(epiDisplay)
tab1(PONDICE$icetype, sort.group = "decreasing", cum.percent = TRUE)
This produces a nice bar graph in R, but wont knit. I have all the files saved to a desktop folder. Please Help! These files came from Regression Analysis (7th edition) Please ask if you need more info!

Knitr error in loading in-built data

I am trying to run a code in R using knitr compiler. It for some reason generates this error:
Error in str(Oats) : object 'Oats' not found
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> str
Execution halted
Here is the code I am using:
```{r}
data(Oats)
str(Oats)
plot(Oats)
sp.oats <- within(Oats, nitroF <- factor(nitro))
model1=lm(yield~Variety*nitro,data=Oats)
summary(model1)
model2=lme(yield~Variety*nitro,data=Oats,random=~1|Block/Variety/nitro)
summary(model2)
coef(model1)
coef(model2)
plot(ranef(model2))
plot(model2)
```
Please suggest what I should do to resolve this issue. Thanks!
I think you're looking for
data(Oats,package="nlme")
Quotation marks are optional around the data set name (Oats, "Oats") but mandatory for the package name ("nlme").
But
library(nlme)
data(Oats)
will also work, and since you're going to be using functions from nlme anyway, you might as well do it that way.
Adding comment as an answer. I thought it might be a duplicate (ansd still suspect it might be, but I couldn't find it in a search, so maybe it will be useful in subsequent searches.:
It is in the nlme-package which is not loaded by default, but it is shipped with every copy of R since its priority is "recommended". #MAPK should add a line that says data(Oats, pac=nlme) before he tries to access it, and hpesoj626 should try that at his console. Of course, that will then possibly lead to another error since the lme-function might not be there. So I think the final solution might be
```{r}
library(nlme)
data(Oats)`
....
as a starting point (inside the knitted section).

Trying to Knit but can't because read excel function is not being recognized in R

My whole program is working but when I try to knit to a document the same error is being thrown.
Quitting from lines 3-78 (Untitled.spin.Rmd)
Error in eval(expr, envir, enclos) : could not find function "read_excel"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
I don't under stand why. Where are the first 5 lines of my code.
Dataset_Stats1Project <- read_excel(Users/jamiestokes16/Desktop/Dataset_WomeninGov5.xlsx)
#Descriptive stats for important variables
summary(Dataset_WomeninGov$Wcongress)
summary(Dataset_WomeninGov$Wgov)
summary(Dataset_WomeninGov$Wleg)
Any help would be appreciated.
When you knit a document, knitr essentially creates a new R session for the code to run in. So any packages loaded session will not be accessible unless you load them directly within the .Rmd file.
I always find it easiest to include a chunk at the start of the document loading any packages used:
```{r LoadPackages, include = FALSE}
library(readlx)
# add other packages here
```

Knitting returns parse error

In attempting to knit a PDF. I'm calling a script that should return two ggplots by calling the chunk:
```{r, echo=FALSE}
read_chunk('Script.R')
```r
But receive the error
processing file: Preview-24a46368403c.Rmd
Quitting from lines 9-12 (Preview-24a46368403c.Rmd) Error in
parse(text = x, srcfile = src) : attempt to use zero-length
variable name Calls: <Anonymous> ... <Anonymous> -> parse_all ->
parse_all.character -> parse Execution halted
The script on its own runs and returns the two plots, but won't return them when knitted.
Similarly attempted to use source()
But got a similar error
Quitting from lines 7-10 (Preview-24a459ca4c1.Rmd) Error in
file(filename, "r", encoding = encoding) : cannot open the
connection Calls: <Anonymous> ... withCallingHandlers -> withVisible
-> eval -> eval -> source -> file Execution halted
While this does not appear to be a solution for you, this exact same error message appears if the chunk is not ended properly.
I experienced this error and traced it to ending chunk with `` instead of ```. Correcting the syntax of the chunk solved the problem I experienced with the same error message as you.
Are you sure that knitr is running from the directory you think it is? It appears that it is failing to find the file.
use an absolute path, if that fixes it, you've found your problem
once you've done that, you can use opts_knit$set(root.dir = "...") -- don't use setwd(.) if you want it (the cwd) to be maintained.
Knitr's default is the directory of the .Rmd file itself.
It may have to do with the "r" at the end of the triple backquotes demarcating your code chunk. There should be nothing after the triple backquotes, but I think the problem is specifically that the letter is "r".
The issue stems from the fact that R markdown processes backquoted statements starting with r as inline code, meaning it actually runs whatever is between the backquotes.
I had similar issues writing a problem set in an Rmd with this statement, which had backquoted text intended to be monospace but not run as inline code:
Use sapply or map to calculate the probability of a failure rate over r <- seq(.05, .5, .025).
When I knit the document, I got opaque error messages saying I had an improper assignment using <-. It was because instead of just displaying the backquoted statement in monospace, r <- seq(.05, .5, .025) was actually processed as R inline code of <- seq(.05, .5, .025)...thus the improper assignment error. I fixed my error by changing the variable name from r to rate.
The actual text of the error message in your question might refer to whatever follows your code chunk, as the knitting process is probably trying to run that as code. In this case, just removing that stray r at the end of the code chunk should fix the error.
You should use the following similar syntax, I had the same exact issue but got it fixed:
```{r views}
bank.df <- read.csv("C:/Users/User/Desktop/Banks.csv", header = TRUE) #load data
dim(bank.df) # to find dimension of data frame
head(bank.df) # show first six rows
```
the ``` has to be in the end of the line.
In my case was that I finished the code with four comas, not three . Check this and If you finished with four comas too, try to delete one of them.

Resources