JuliaInterop / JuliaCall

Embed Julia in R
https://non-contradiction.github.io/JuliaCall/index.html
Other
267 stars 36 forks source link

RBookdown: Output not displayed after Julia code chunk #123

Closed likefrankie closed 4 years ago

likefrankie commented 5 years ago

In RBookdown, I cannot figure out how to display the output after the Julia code chunk (it works fine for R/Python code chunks). Here is a simple example:

a = sqrt(2)

I have a R code to compile the book: library(knitr) library(bookdown) library(JuliaCall) julia_setup()

fn <- "bookdown-demo.Rmd" if (file.exists(fn)) file.remove(fn)

bookdown::render_book("index.Rmd", "bookdown::gitbook") browseURL(paste('file://', getwd(),'_book','index.html', sep='/'))

Non-Contradiction commented 4 years ago

Hi, thank you for the feedback! Does the thing work for the Knitr button? Is there a minimal example so I could try?

My current guess is that in the R script you provided, the RMarkdown display facility is not activated automatically by JuliaCall because JuliaCall is not being initialized in the RMarkdown environment. But I cannot verify that because of the lack of an example.

If you are using new versions of JuliaCall, you could consider using JuliaCall::julia_notebook_setup() to do the setup instead.

likefrankie commented 4 years ago

You're more than welcome. And thank you for the prompt response. Please find attached the minimal example that you requested. Please unzip the file, add your directory to line 9 in the file "00-compile_book.R", and then run the script below in the command line after navigating to the "minimal-example" folder. Let me know if you have questions on running this example.

Rscript 00-compile_book.R

minimal-example.zip

I tried running with julia_notebook_setup() and received the following error message - my JuliaCall version is 0.17.0: Error: 'julia_notebook_setup' is not an exported object from 'namespace:JuliaCall' Execution halted

Thank you in advance for your help!

Non-Contradiction commented 4 years ago

Thanks for providing the example! My previous guess is correct. Running in the command line does not activate JuliaCall RMarkdown mechanism, so the result of julia code is not correctly displayed in the RMarkdown document. The julia_notebook_setup() is provided in the Github JuliaCall master, which can be used instead of julia_setup() in your code to deal with the issue. You can get it by devtools::install_github("Non-Contradiction/JuliaCall"), or you can wait for me to release a new version of JuliaCall, which should be quite soon.

Non-Contradiction commented 4 years ago

For the Plots problem, currently JuliaCall does not support PyPlot directly. So with julia_notebook_setup() and your current example, I get ## Figure(PyObject <Figure size 640x480 with 1 Axes>), which is a text representation for the plot in julia. If possible, you can try to use Plots.jl in Julia, which is supported by JuliaCall. Supporting PyPlot directly in JuliaCall should not be too difficult. I will try to investigate into it when I have time.

Non-Contradiction commented 4 years ago

Now the new release of JuliaCall 0.17.1 is on CRAN. Replace the julia_setup with the new julia_markdown_setup function should deal with the problem that output is not displayed.

likefrankie commented 4 years ago

Thanks for the update. I was able to update to the new version of JuliaCall 0.17.1 and ran julia_markdown_setup. I reran my minimal example and got the expected output for the first code chunk (square root of 2). But the figure in the second chunk did not display, instead it produced:

## Figure(PyObject <Figure size 640x480 with 1 Axes>)

I tried a few variations, but was not successful. I am transferring content from notebooks to RBookdown and would like to avoid converting all PyPlots to Plots. Many thanks!

likefrankie commented 4 years ago

Now the new release of JuliaCall 0.17.1 is on CRAN. Replace the julia_setup with the new julia_markdown_setup function should deal with the problem that output is not displayed.

Non-Contradiction commented 4 years ago

Thanks for the feedback! I will try to investigate how to support PyPlots next week. Hope it is not too difficult.

JMLuther commented 10 months ago

I'm having a similar issue with the new TidierPlots package (a Julia version of ggplot), so that the plot opens in interactive mode in a plotting pane. It also does this during rendering/knitting to an html file, so that no figure is displayed in the html file. The standard Julia plots work fine (interactive and knit to html); also the python matplotlib plots work fine.

I suspect that if I tweaked the block header it could fix it, but I have not been able to figure it out. I also tried saving plots to a file then using inline code to display but the figure didn't display either. any help is appreciated- this may be my inexperience rather than any JuliaCall issue.

Example:

my chunk header is: {julia echo=TRUE, fig.height=4, fig.width=4, out.width="50%", fig.align='center'}

using Plots
using RDatasets
using TidierPlots
mtcars = dataset("datasets", "mtcars");
ggplot(mtcars, aes(x=:Disp, y= :MPG)) +
  geom_point(color=:steelblue) +
  theme_minimal()