JuliaCall icon indicating copy to clipboard operation
JuliaCall copied to clipboard

Add support for Rnw latex files in JuliaCall knitr engine

Open konstunn opened this issue 8 years ago • 9 comments

First of all, thanks for your work. I much appreciate the ability to use Julia with knitR.

I'd like to open this issue as a feature request. The feature under consideration is support for Rnw latex files in JuliaCall knitr engine.

We had a short discussion here, where I reported a problem I faced when I knit Rnw file with pure Julia chunks.

konstunn avatar Mar 26 '18 12:03 konstunn

Thank you for the feedback! It turns out the problem is caused by some small error in eng_juliacall. This small error doesn't have any effect in Rmd but causes bugs in Rnw. The master should fix this problem, and the following example should work:

\documentclass{article}

\begin{document}

<<mychunk, engine='julia'>>==
using Plots
pyplot()
x = linspace(0.,10.)
y = sin.(x)
plot(x, y)
@

\end{document}

Although using PyPlot directly still doesn't give plots which is one of current limitation of JuliaCall as I have said.

Non-Contradiction avatar Mar 26 '18 14:03 Non-Contradiction

The example worked except for I did not get the plot in the resulted document.

Tell me please, why does julia_setup() force the result output .tex file to be in utf8 encoding? I use cp1251. So I \usepackage[cp1251]{inputenc} and \usepackage[russian]{babel} in my .cls file. My pdflatex-compilation is going to be broken.

I knit my document with the following shell-command:

R -e "library(knitr); Sys.setlocale('LC_ALL', 'ru_RU.cp1251'); knit('main.Rnw')"

If I comment line julia_setup() the document builds successfully.

P. S. I would be able to put this in a separate issue, if you like.

konstunn avatar Mar 29 '18 17:03 konstunn

The example works for me with the plot included no matter from RStudio Compile pdf button or knitr::knit function. There should be a figure folder generated with a graph in the folder, the folder will be in the current folder using knitr::knit or in the same folder with the Rnw file using RStudio Compile pdf and the generated tex and pdf should include graph from the figure folder.

What is the problem with the example? No figure folder generated? No graph in the folder? Or no includegraphics in the tex file? Does the julia code work in julia?

Non-Contradiction avatar Mar 29 '18 19:03 Non-Contradiction

Currently julia_setup doesn't have to be called explicitly. And I'm not sure why julia_setup results utf8 encoding. Maybe because julia_setup prints out something in utf8? How about julia_setup(verbose = FALSE)?

Open a separate issue on this is greatly welcomed.

Non-Contradiction avatar Mar 29 '18 19:03 Non-Contradiction

I've tried to call julia_setup() explicitly to fine-control the process, I thought that would help me to workaround some problems.

There is a pdf-file in figure directory generated, but it is empty, 0 bytes. And there is no includegraphics in the tex file.

konstunn avatar Mar 30 '18 04:03 konstunn

I have changed knitting rule as follows:

LC_ALL="ru_RU.cp1251" R -e "library(knitr); knit('main.Rnw')"

And now it works. Although I can't get plots from Julia and syntax highlighting of Julia code chunks. But I can obviously retrieve data from Julia to R via julia_eval() and make plots in R.

konstunn avatar Mar 30 '18 05:03 konstunn

Then I have one possible theory for the empty graph problem. The problem may be that knitr tells JuliaCall to generate pdf graphs, and JuliaCall gives commands for Plots to generate pdf graphs, but there is some error in julia generating pdf graphs, and then JuliaCall abandons the process because of the error, thus there is also no includegraphics in the tex file. So I think the solution would be checking generating pdf graphs in julia, as I remember there should be some julia dependency for that, and also there should be dependency for png graphs and so on, or using another graph type in knitr that can be successfully generated in julia.

Non-Contradiction avatar Mar 30 '18 13:03 Non-Contradiction

Then I think the problem may be that in the previous failed knitting rule, the locale for julia is not correctly set, but with the new way, the locale is set correctly. I will investigate into how to set julia locale correctly, but I think even I work out the way, it may not as robust as your new knitting rule. So the new way is strongly recommended.

Non-Contradiction avatar Mar 30 '18 13:03 Non-Contradiction

Syntax highlighting is totally handled by knitr instead of JuliaCall. In current version of JuliaCall, eng_juliacall just gives the julia code and the results of the code to knitr and knitr will take care of how to display the code and results. This is different from the previous version of JuliaCall, which takes care of everything. The latter way requires huge efforts and the result may not be as ideal as knitr already provides. The former way makes the julia chunk looks native and consistent with R chunk. So I think the former way is more preferable.

And I think the highlight other that R language in knitr is handled by some R packages like highlight I think? And as I remember, it will require other dependency because the package is only a wrapper for some other external tool, maybe highlight the same name? The highlight of other languages in knitr is handled by highr::hi_andre, which is a wrapper for Andre Simon's Highlight, so it requires Highlight to work.

Non-Contradiction avatar Mar 30 '18 13:03 Non-Contradiction