Error in loadNamespace(x) : there is no package called ‘JuliaCall’ - julia

I just started to learn Julia in Quarto but when I run the following code in a Julia chunk in Quarto:
---
title: "Julia in Quarto"
editor: visual
format: html
---
```{julia}
# generating vectors
# x-axis
x = 1:10
# y-axis
y = rand(10)
# simple plotting
plot(x, y)
```
It returns:
Error in loadNamespace(x) : there is no package called ‘JuliaCall’
The error still appears even when adding:
using Pkg
Pkg.add("JuliaCall")
To the code chunk. Does anyone knows how to run the following Julia code in Quarto?
Please note: I use Rstudio

JuliaCall is an R package. Quarto executes Julia code using the IJulia Jupyter kernel. To use it, specify jupyter: julia_version in the YAML header. A Quarto installation guide is available.
To render Quarto documents that contain julia chunks
start julia
Enter package mode by pressing ].
run Add IJulia
Go back to the REPL (backspace), the default prompt after you start julia
run using IJulia
run notebook(). Use Ctrl+c or quitting julia to stop the Jupyter kernel.
If you don't have Jupyter installed at this point, this will be installed, refer to the above guide for details. This may take a while and prompt some input.
Now you should be able to render a document via the Quarto Cli from a shell and1 RStudio. For example, the following example.qmd should render and print a matrix.
---
title: "Bla"
jupyter: julia-1.8
---
```{julia}
[1 2 3]
```
This may a slow. Refer to the above guide on installing Revise.jl and using Jupyter Cache to speed things up. I personally experience speedup in using VSCode + Quarto extension over RStudio.
If not, the following allows R users to run julia code from within R and RStudio,
in R, use install.package("JuliaCall")
in R, run JuliaCall::julia_setup()
This takes care of some PATH variable and comes with the installJulia optional argument.

Related

Using plot within a script in julia

I've been prototyping some julia code in a Jupyter notebook, but it's outgrown the notebook format and I want to put it in a script in order to organise it properly. However, I can't work out how to get Plots to work in a script, and can't find any documentation about it.
Here's a minimal not-working example:
using Plots
gr()
display(plot([1,3,2]))
println("here")
This code takes 20-30 seconds to import Plots, then opens a window but immediately closes it again, prints "here", and exits. I can't use ctrl-C while the import process is happening.
So, I have three questions:
How do I prevent the plot window from closing as soon as it opens? What I want is for the script to block or (ideally) enter an event loop until the window is closed, and terminate after that.
Can the extremely long import time be reduced somehow?
Does any documentation exist for using Plots outside of a Jupyter environment?
If it makes a difference, I'm using julia 1.1.1 on a Mac.
The most natural way to achieve the workflow you're looking for in the first bullet is to use Juno or VS Code. Suppose in your working directory you have the following code in a file script.jl:
using Plots
gr()
display(plot([1,3,2]))
hello = "world"
println("here")
Now fire up Juno or VS Code and launch the integrated Julia REPL. From the Julia REPL, you can use include to source your script:
julia> include("script.jl")
Plot{Plots.GRBackend() n=1}
here
Now your script has been completely run and the plot is available for viewing in the plot pane (this is true for both Juno and VS Code). Now you can continue to include other files in the REPL or run other code interactively. Note that variables defined in script.jl are available for use:
julia> hello
"world"
EDIT:
If you run a script using the -i option (for interactive), the plot GUI will stay open and a REPL will launch after the script is done running:
$ julia -i script.jl
The various options to the Julia executable can be found here, or can be found by typing $ julia --help in a terminal.
The import time can be eliminated by compiling the Plots.jl package ahead of time and baking it into your julia executable using the PackageCompiler.jl package. Here is a link to the package website
https://julialang.github.io/PackageCompiler.jl/dev/
See the tutorial on how to do this, the first tutorial is about compiling a syntax highlighting package. That one replaces your default 'sysimage'. The second tutorial is about creating a loadable 'sysimage' where Plots.jl is compiled, and starting Julia with this 'sysimage' so it doesn't have to complie Plots.jl again.
However I coudln't get the second 'loadable sysimage' working so I used the first tutorials way even for Plots.jl.
After this change, using Plots and plot() are basically instant.

How to create Julia notebook in Juno similar to IJulia or R Notebook

Is it possible to create a Julia notebook in Atom/Juno similar to IJulia (Jupyter Notebook) or R Notebook (RStudio IDE)?
What I am looking for is a format that can save the code input together with the calculated ouput and the output printed in the console in one chunk. I also would like to mix in Markdown between code chunks.
The reason for me not wanting to use IJulia is that I don't want to save my Julia code in JSON format. I want it to be as pure as possible.
Have a look at Weave.jl, which promises:
Current features
Noweb, markdown or script syntax for input documents.
Execute code as terminal or "script" chunks.
Capture Plots, Gadfly and PyPlot figures.
Supports LaTex, Pandoc, Github markdown, MultiMarkdown, Asciidoc and
reStructuredText output
Publish markdown directly to html and
pdf using Julia or Pandoc markdown.
Simple caching of results
Convert to and from IJulia notebooks

compiling legacy Sweave with RStudio 0.99.902

I'm having multiple problems with the “sos” vignette in the “sos” package that has been on CRAN since 2009. I recently noticed that it fails in multiple ways, and I can't figure out how to fix several of the problems. "Compile PDF" on “sos.Rnw” in RStudio with R 3.3.0 under Mac OS X 10.11.5 produced an XQuartz pop-up as follows:
>Sweave Noweb Syntax?
>>It seems you are using the Sweave-specific syntax in line(s)
>>9, 15, <snip>; you may need Sweave2knitr("sos.Rnw") to
>>convert it to knitr'.
I did the following, as it asked:
Sweave2knitr('sos.Rnw')
This produced a file 'sos-knitr.Rnw'. I opened this file in RStudio and clicked 'Compile PDF'. I got one line only:
>Loading required package: knitr"
If I got anything else, I don't know where it went.
Any suggestion on how I can get past this?
Thanks, Spencer Graves
RStudio is using knitr by default instead of sweave to generate the code. Revert this decision by doing the following:
RStudio:
Tools -> Global Options -> Sweave -> Weave Rnw files using: Sweave

Access function from current package when using R CMD check with vignette

I'm putting together a package; for simplicity with one function and one vignette illustrating its use.
I was able to run R CMD check packagename with no difficulties before I tried adding the vignette.
The package has a functionfoo.R in the R directory of packagename (it makes a plot with base graphics).
The vignette, in the vignettes directory, (an .Rnw file) calls function foo like this:
<<fig1, fig=true, echo=true, include=true>>=
df0 <- data.frame(x1=rnorm(10))
foo(df0)
#
I'm tying to play 'by the rules' but running R CMD check packagename as usual gives:
When sourcing 'foo.R':
Error: could not find function "foo"
Execution halted
I've tried adding the following to the .Rnw file, which didn't help:
\begin{document}
\VignetteDepends{packagename}
I've also tried this with no success:
<<fig1, fig=true, echo=true, include=true>>=
df0 <- data.frame(x1=rnorm(10))
source("foo.R")
foo(df0)
#
Note that the NAMESPACE file already contains the following:
export(foo)
Questions:
Do I need to add a specific source() command in the .Rnw file every time I call a function from the package? If so, how do I specify the path (i.e. where is R CMD check starting from when checking the vignette?)
Or should I take the easy way out by adding the following to the DESCRIPTION file:
BuildVignettes: False
(As I'm able to build a .pdf from the existing .Rnw file).
I'm trying to follow the advice in Writing R extensions.
Your vignette needs to have library("mypkg") at the top so that your own functions, like foo, can be found. I believe this is because the vignette building runs in a clean environment so it doesn't know about your package or any other for that matter unless you bring it up.
If you have such a line already, put a minimal example of your vignette into your question, and include your sessionInfo() as we may need that to figure it out.

What exactly does R CMD Sweave --pdf do?

I noticed this in the changes of R 2.14:
R CMD Sweave now has a --pdf option to produce a PDF version of the processed Sweave document.
Trying it out, I noticed that it not only ran pdfLaTeX on the resulting tex but also correctly included bibTeX references and cleaned up afterwards. Seems like a very very nice way of using Sweave now (not to mention how easy it now is to implement the whole routine in editors).
But what exactly is this now running? I couldn't find any more details on it. It seems Sweave -> pdflatex -> bibtex -> pdflatex -> pdflatex at least?
Thanks for the question. I had wondered myself about the code behind that 'automagical' process.
R CMD Sweave --pdf ultimately calls tools::texi2dvi, which:
Run[s] latex and bibtex until all cross-references are resolved and
create[s] either a dvi or PDF file.
(See here for more texi2dvi details).
Here is the chain of events set into motion by an R CMD Sweave --pdf call:
The source file rcmdfn.c has code that instructs R CMD Sweave to run utils:::.Sweave() --args" through Rterm.exe.
If --pdf is set, utils:::.Sweave() calls tools::texi2pdf() to process the Sweave file.
texi2pdf() in turn calls tools::texi2dvi().
Finally, texi2dvi() looks at the environment to learn which tools are available to it, and does the work described in the help file linked above.
You could try to perform the conversion from Rnw to pdf manually en see how many times the respective steps are needed to get the same result as R CMD Sweave.
I don't know much about the interior workings, but I know the development version of RStudio will let you select between knitr and Sweave as well as between pdflatex and xelatex, and gives bibtex as an option.

Resources