I am able to knit the following code to PDF. However, it always fails when trying to knit the output to Word (win 10, Rstudio 1.2.5033, R 3.6.2, papaja 0.1.0.9942). I had to delete the papaja header for posting (too much code).
```{r setup, include = FALSE}
library("papaja")
```
# Methods
```{r figure}
plot(cars)
```
The error message reads
Error running filter D:/Boelte/R_library/papaja/rmd/docx_fixes.lua:
[string "--[[..."]:227: Constructor for Emph failed: [string
"--[[..."]:258: attempt to index a nil value (local 'x')
stack traceback: [C]: in function 'error'
..."]:227: in field 'Emph' D:/Boelte/R_library/papaja/rmd/docx_fixes.lua:14: in function 'Image'
Fehler: pandoc document conversion failed with error 83
Is there any way to correct this error? It is a papaja or a pandoc error?
This is a papaja error related to post-processing of the document (in this case styling figure captions) via the docx_fixes.lua-filter. I will try to fix this as soon as possible. For the time being, you should be able to resolve this problem by specifying a figure caption in the chunk options.
```{r setup, include = FALSE}
library("papaja")
```
# Methods
(ref:fig-cap) This is the figure caption.
```{r figure, fig.cap = "(ref:fig-cap)"}
plot(cars)
```
Related
I am writing a document with fairly resource intensive R code. I want to prevent execution of one block of R code in knitr which is giving me document timeout error in Overleaf.
In R studio, this can be done using eval = FALSE. I want to recreate this in knitr. So far, the only way I have found is to suppress errors using <<setup, include=FALSE, cache=FALSE>>= muffleError <- function(x,options) {} but it only works on the entire document.
I specifically want to prevent evaluation but show the R code.
Is this what you want to do, or have I misunderstood? The eval = FALSE is in one code chunk and the second chunk still plots.
---
title: "A Test Knit"
output: html_document
---
## Show code but don't run
```{r, eval = FALSE}
summary(cars)
```
## Run and render plot
```{r}
plot(pressure)
```
I want to load the package names hflights. I think I have load it correctly. And the code at line 64 and 65 can be run, when I the hint the knit button it keeps reminding me the problem is at there. But it can run, the result is shown belown. I don't know why I can not knit my rmd file.
Use eval= TRUE to evaluate the chunk while knitting. According to here
eval: Whether to evaluate a code chunk.
```{r, echo = FALSE, eval = TRUE}
library(hflights)
data(hflights)
```
```{r getdata, cache = TRUE}
dim(hflights)
names(hflights)
```
-output
I want to include error messages in an R markdown pdf report. This works well:
---
output: pdf_document
---
This will be knitted and show the error message in the pdf.
```{r, error = TRUE}
stopifnot(2 == 3)
```
However, if I try the same approach with an error that comes from testthat, my document does not knit anymore.
---
output: pdf_document
---
This will not knit
```{r, error = TRUE}
library(testthat)
expect_equal(2, 3)
```
Why is that? And what can I do to include error messages from testthat's expect_something functions without wrapping them up in a test?
I think this must be possible since Hadley Wickham includes many error messages in his book R packages that come directly from expect_something-functions.
This is related, but not answered in Include errors in R markdown package vignette and How to skip error checking at Rmarkdown compiling?
Create a test:
```{r, error = TRUE}
library(testthat)
test_that("Test A", {
expect_equal(2, 3)
})
```
I don't understand the reason for the behavior (good question!), but this could be a workaround:
---
output: pdf_document
---
This will knit
```{r, error = TRUE}
library(testthat)
# expect_equal(2, 3)
# skip_if_not(2, 3)
assertthat::assert_that(2 == 3)
```
Why in Rmarkdown, if the expression inside try fails, the error message is not printed, even though in chunk error = TRUE. Code is below, which does not print anything:
```{r, error = TRUE}
try(log("a"), silent = FALSE)
```
Use the below code to get the printed output
```{r}
try(log("a"))[1]
```
It will look like this in pdf
Paste the below code in your .rmd file at the start to get the errors and warnings generated in the r chunks to html output or pdf.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE, warning = TRUE)
```
This is not chunk specific its for all chunks inside .rmd file, this will print all the errors or warnings if any any at all in the r chunks written.
After adding the above chunk, you can knit html or pdf that will also show you the errors and/or warnings if any at all in r chunk.
I am trying to use RMarkdown (Knit) for the first time to produce pdf. The default file (File > New File > R Markdown) works well, it shows the generated pdf when compiled. For example, the following code runs,
```{r cars}
summary(cars)
```
However, if I just change cars with "myData," it does not compile and shows,
Error in object[[i]] : object of type 'closure' is not subsettable
Calls: <Anonymous> ... withVisible -> eval -> eval -> summary -> summary.default
Execution halted
I have "myData" loaded in the global-environment and can do other operations in original R script. Can someone please provide some guideline. Thank you very much for your time.
Running an Rmarkdown file starts a new R session.
Within the new session, you can load the data.frames that are stored in the data package, but other datasets must be loaded from within the Rmarkdown document.
To get myData to show up in your Rmarkdown document,
save the file somewhere with save in your current R session
then in your Rmarkdown document, use load to open up the data set
So, in your current R session:
save(myData, file="<path>/myData.Rdata")
and in your Rmarkdown file:
```{r myDataSummary}
load("<path>/myData.Rdata")
summary(myData)
```
If your data is stored as a text file, and you don't wish to store a separate .R file, use read.csv or friend directly within your Rmarkdown file.
```{r myDataSummary}
myData <- read.csv("<path>/myCSV.csv")
summary(myData)
```
This is the error you get when you try to subset (= via x[i]) a function. Since this error is caused by summary(cars) in your code, we may surmise that the cars object refers to a function in the scope in which the document is knit.
You probably forgot to load your data, or you have a function with the same name defined in the current scope.
As #Imo explained, the basic problem is the new session. So, the answer would be adding the script in the rMarkdown. However, it will create few more hiccups. Here is how I handled few of them,
```{r global_options, include=FALSE}
source(file = "C:\\Path\\to\\my\\file.R")
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
echo=FALSE, warning=FALSE, message=FALSE)
```