How to run a julia chunk in RMarkdown - r

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

Related

R markdown knitting errors with repeated chunks

I included a check to install an R package if it's not already installed by the following syntax:
```{r setup-packages, results=FALSE, message=FALSE, warning=FALSE}
if(! require("readxl")) install.packages("readxl")
```
which returns this error:
processing file: Testing.Rmd
Error in parse_block(g[-1], g[1], params.src, markdown_mode) :
Duplicate chunk label 'setup-packages', which has been used for the chunk:
if(! require("readxl")) install.packages("readxl")
Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block
Execution halted
The knitting works if I change {r setup-packages, results=FALSE, message=FALSE, warning=FALSE} to {r}.
I want to reuse this chunk {r setup-packages, results=FALSE, message=FALSE, warning=FALSE} for each package but it only works once. Can someone explain or provide a solution to make it work with other packages?
knitr is erroring due to the reuse of a chunk name. Each chunk must be unnamed, as you found works with just {r}, or uniquely named.
You can fix it in any of these ways:
Put the lines for each of your package checks in a single chunk.
Rename each chunk to have a unique name, like setup-packages-readxl.
Set the option options(knitr.duplicate.label = "allow") in your Rprofile, though this is not a recommended use of this power according to the knitr documentation.

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

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

Using knitr to create HTML slides and separate R code file

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.

Resources