Using knitr to create HTML slides and separate R code file - r

Following on from this question... I am not sure where to set knitr option if I want to output a separate file of R code. The following does not provide the expected additional .R files in my working directory.
---
output: ioslides_presentation
---
```{r setup, include=FALSE}
library("knitr"); purl("myfile.rmd")
#library("knitr"); knit("test_tangle.Rmd", tangle = TRUE)
#opts_knit$set(tangle=TRUE)
```
## Slide with Plot
```{r, echo=TRUE}
plot(cars)
```
but an error message...
Quitting from lines 6-7 (myfile.rmd)
Error in readLines(if (is.character(input2)) { :
cannot open the connection
Calls: <Anonymous> ... withVisible -> eval -> eval -> purl -> knit -> readLines
Execution halted

I recommend you to use the hook_purl function instead. The function purl() (or equivalently, knit(tangle = TRUE)) may fail to work in certain cases, and the hook function hook_purl() is more reliable. See ?hook_purl for more information.
---
output: ioslides_presentation
---
```{r setup, include=FALSE}
library("knitr")
knit_hooks$set(purl = hook_purl)
```
## Slide with Plot
```{r, echo=TRUE}
plot(cars)
```
Then as you knit the document, the R script will be automatically generated.

Related

Execution halted when knitting in RStudio

I´m new at R. I was trying to knit a Rmd file in RStudio into a pdf, but it presents the following error:
Quitting from lines 17-20 (projeto_final_20220429.Rmd)
Error in .External2(C_dataviewer, x, title) : unable to start data viewer
Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> View
Execution halted
The code written in those lines was this:
library(readr)
despesas_municipios_saude_educacao <- read_csv("despesas_municipios_saude_educacao.csv")
View (despesas_municipios_saude_educacao)
Is there anyone who can help me?
On MacOS, knitr attempts to load the X11 library in order to view the data frame, which fails because knitr isn't designed to interact with an end user. Regardless of the underlying operating system, since utils::View() is meant to work in a windowing environment (e.g. RStudio, RGui, R Tools for Visual Studio, etc.), it can't be used to print a data frame in an R Markdown document.
---
title: "Error with View()"
output: html_document
date: "`r Sys.Date()`"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## A header
Here is a reproducible version of the stackoverflow question [Execution Halted when Knitting](https://stackoverflow.com/questions/72068679/execution-halted-when-knitting-in-rstudio).
```{r mtcars}
data(mtcars)
View(mtcars)
```
...generates the following error:
To display a subset of data in a markdown file, one can use head() instead of View().
---
title: "Error with View()"
output: html_document
date: "`r Sys.Date()`"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## A header
Here is a reproducible version of the stackoverflow question [Execution Halted when Knitting](https://stackoverflow.com/questions/72068679/execution-halted-when-knitting-in-rstudio).
```{r mtcars}
data(mtcars)
head(mtcars)
```
...and the output in HTML:
We can improve the look of the output by turning off echo on the R chunk used to print the table, and use knitr:kable() to format the data frame.
We use the following R Markdown code, including the "pipe" argument to appropriately space the columns in the output.
We can make the output look nicer with `knitr::kable()`.
```{r kableVersion,echo=FALSE}
library(kableExtra)
kable(head(mtcars),"pipe")
```
...and the output renders like this in HTML:

Display Block of R Code in Knitr With Evaluation Turned Off

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)
```

How to run a julia chunk in RMarkdown

I am trying to run a Julia chunk in RMarkdown. I am using the package JuliaCall. Here are the steps I've completed:
Downloaded Julia
Installed JuliaCall
Run the code julia_setup(JULIA_HOME = "C:/Users/James/Documents/Julia 1.5.1/bin")
Run julia <- julia_setup()
Here is a minimal example of my RMarkdown file:
---
title: "julia_eg"
author: "James"
date: "9/23/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
this is a julia example
```{julia}
a = sqrt(17)
a
```
When I try and knit this, it tells me it cannot find Julia - I get this error:
Error in julia_locate(JULIA_HOME) : Can not find the Julia installation in the default installation path 'C:\Users\James\AppData\Local' Calls: <Anonymous> ... withVisible -> eval -> julia_setup -> julia_locate
So clearly my running of julia_setup in step 3 above didn't have the desired effect - even though it did run this for sometime and told me that it had completed that task.
Is there a more straightforward way of getting it to find Julia?
Rmarkdown is only aware of any code that is run in the immediate session, to avoid creating documents that will be impossible to compile by itself.
As such you'll have to add the code to the initial code chunk
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
Julia_setup(JULIA_HOME = "C:/Users/James/Documents/Julia 1.5.1/bin")
julia <- julia_setup()
```

Including rmarkdown text in flexdashboard (includeMarkdown does not work) (R)

I would like to include external markdown file; however, I could not handle it on RStudio so far. If I manually copy/paste the content of about.md into main.R, there is no issue. I mean setting up everything related with flexdashboard is fine. On the other hand, I have tried to reinstall rmarkdown package and import it by library("rmarkdown"). This is not fair enough because flexdashboard has already its internal one. So it should not be related with whether the rmarkdown installed or not apart from flexdashboard installation. Any suggestions ?
Thanks
I have prepared minimal code with its output as below:
'about.md'
test1
=======================================================================
**testttt**
testttt
**testttt2**
testttt
main.R
---
title: "test"
author: "test"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
```
```{r}
includeMarkdown('about.md')
```
The output:
Quitting from lines 17-18 (minimal.Rmd)
Error in includeMarkdown("about.md") :
could not find function "includeMarkdown"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
The function includemarkdown is from the package htmltools. So you have to load the library or use:
```{r}
htmltools::includeMarkdown('about.md')
```

RMarkdown conversion

---
title: "Twitter Data processing"
author: "Ankur"
date: "7 September 2016"
output: pdf_document
---
```{r setup, include=FALSE}
library(knitr)
library(SnowballC)
library(tm)
library(ggplot2)
library(RColorBrewer)
library(wordcloud)
```
"SnowballC" is required for stemming the document in this particualr verion. You may alternatively need "Snowball".
```{r Turning into Corpus, include=TRUE, warning=FALSE}
myCorpus<-tm_map(myCorpus, stemDocument)
writeLines(strwrap(myCorpus[[250]]$content,60))
```
I am having issues while knitting the RMD Document. the code works fine but it is not building the RMD file. I have the "myCorpus" variable in other file, butr it is loaded into an environment. I am having the following error:
Error in tm-map(myCorpus, stemDocument) : object ;myCorpus' not found
calls:<Anonymous> ... withCallingHandlers -> withVisible -> eval-> eval ->tm_map Execution Halted
There are 2 ways to load the data "myCorpus" to your .RMD file:
library(knitr)
knit('your_file.Rmd')
This will take your recent environment into account and the error should be gone.
Store your "myCorpus" as "myCorpus.RData" and load it manually in your RMD file
```{r load myCorpus, include=FALSE}
load("myCorpus.RData")
If you do it this way you can use the "knit" button from RStudio.
I hope one of this ways is a good solution for you.

Resources