Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register as knitr engine #10

Closed
retorquere opened this issue Jun 16, 2020 · 28 comments
Closed

Register as knitr engine #10

retorquere opened this issue Jun 16, 2020 · 28 comments
Assignees
Milestone

Comments

@retorquere
Copy link

I've tried registering plantuml as a knitr engine using

knitr::knit_engines$set(plantuml = function(options) {
  code <- paste(options$code, collapse = "\n")
  if (options$eval) plot(x = plantuml(code), vector=TRUE) else code
})

with vector=TRUE it seems to work but I see a warning "don't know how to handle 'plantuml' engine output". With vector=FALSE I'm getting no output.

@rkrug
Copy link
Owner

rkrug commented Jun 17, 2020

Never thought of that. Sounds VERY interesting.

OK - looking onto this:

The problem is the return of an image. Here one can find the answer.

https://stackoverflow.com/a/54700702/632423

I put an Rmd file as a gist which works as expected.

Any suggestions to improve the knit function? I would like to include this in the next release if it is OK with you (obviously with attribution).

@rkrug
Copy link
Owner

rkrug commented Jun 17, 2020

Oh - I am using the plantuml package version from GitHub (master branch).

@retorquere
Copy link
Author

retorquere commented Jun 17, 2020

Yours looks heaps better than what I could cobble together; I'm not really an R expert. I found that SO answer, but never got where you got -- AAMOF, I asked there in a comment whether I could have the in-line image shown, and got "nope, not possible"

OK with me to include it 😆 I'd be super happy, attribution or not. I would propose one, maybe two changes:

  • make the path of the images, or at least the directory they appear in, configurable. I use git for pretty much anything and I want to exclude generated assets
  • I think it'd be OK to just have format rather than plantuml.format. I know options is fairly polluted, but this seems safe enough to me, and it's less typing.

my current implementation was:

knitr::knit_engines$set(plantuml = function(options) {
  code <- paste(options$code, collapse = "\n")
  if (options$eval) plot(x = plantuml(code), vector=FALSE, file=options$path)
  knitr::engine_output(options, out = list(knitr::include_graphics(options$path)))
})

@retorquere
Copy link
Author

Ooh -- I also have

if (!file.exists(file.path(system.file("jar", package = "plantuml"), "plantuml.jar"))) {
  updatePlantumlJar()
}

because I'm bringing colleagues into the RStudio fold who approach RMarkdown with some trepidation, so anything I can make "just work" for them is a huge help.

@rkrug
Copy link
Owner

rkrug commented Jun 17, 2020

Concerning your last comment: This should be done automatically when plot.plantumll() is called - so no need for that. I am planning to change the implementation anyway to be able to submit it to CRAN, so this likely will not be working Ain the first "stable" release.

Inline image: Yes - that is not possible (I have to trust them on that). I guess you wanted to have the image in the knitted document, and inline in the context of knitting and RStudio means "in the source document visible". But it would be great, if that would be possible!

Good points.

  • I think it'd be OK to just have format rather than plantuml.format. I know options is fairly polluted, but this seems safe enough to me, and it's less typing.

It seems to be a convention in the open space of these options, to preface it with the name of the module. So I would prefer, to leave it as it is, as the other knitting engines follow the same convention.

  • make the path of the images, or at least the directory they appear in, configurable. I use git for pretty much anything and I want to exclude generated assets

Good point. I will add a plantuml.path to the respected options.

I will upload the new version to the dev branch later today.

@retorquere
Copy link
Author

Inline image: Yes - that is not possible (I have to trust them on that). I guess you wanted to have the image in the knitted document, and inline in the context of knitting and RStudio means "in the source document visible".

That is correct, this is my main goal.

It seems to be a convention in the open space of these options, to preface it with the name of the module. So I would prefer, to leave it as it is, as the other knitting engines follow the same convention.

Understood.

Good point. I will add a plantuml.path to the respected options.

I will upload the new version to the dev branch later today.

Awesome!

@retorquere
Copy link
Author

How can I pull this dev branch into my notebook?

@rkrug
Copy link
Owner

rkrug commented Jun 17, 2020

OK - it is in the dev branch, running tests at the moment. Will push it to the master branch once the test are finished successfully.

You can install the dev branch by running

devtools::install_github("rkrug/plantuml", ref = "dev")

@rkrug
Copy link
Owner

rkrug commented Jun 17, 2020

OK - pushed it to master. You can install it via

devtools::install_github("rkrug/plantuml", ref = "dev")

I will close this issue - please re-open it if you have problems or file a new issue if you have suggestions for improvements.

Fixes 6599cc8

@rkrug rkrug closed this as completed Jun 17, 2020
@rkrug rkrug self-assigned this Jun 17, 2020
@rkrug rkrug added this to the v0.3 milestone Jun 17, 2020
@retorquere
Copy link
Author

Awesome. How will I know the version has made it into a release?

@rkrug
Copy link
Owner

rkrug commented Jun 17, 2020

You can always run the install_github() which will either update t o a new version, or te lol you that no new version is there.

Anyway - I plan on releasing it to CRAN, possibly some time next week.

@retorquere
Copy link
Author

Wait -- if it's on master, a regular install_github (or in my case, p_load_gh from pacman) should suffice, right?

@rkrug
Copy link
Owner

rkrug commented Jun 17, 2020 via email

@retorquere
Copy link
Author

I have updated from master, but with this:

{plantuml mapping-flow, plantuml.path="images"}
@startuml
left to right direction
scale 2.0

object "bron fysiek" as bron {
  Het fysieke datamodel
  van CATS/VLS/Alluris
}

object "bron UI/logisch" as ui {
  Het logische datamodel
}

note bottom {
  Dit is gelijk aan wat de gui van het bronsysteem laat zien.
  FB kan hiermee de juistheid van queries toetsen.
}

object "logica herstellen" as logic #lightgreen {
  het fysieke datamodel
  terugbrengen naar het model
  van de gebruikersinterface
}

object "mapping naar CDM" as map #lightgreen {
  vertalen van bron-entiteiten
  naar canonieke entiteiten
}

object "CDM" as cdm {
  Het canonieke datamodel
}

bron --> logic
logic--> ui
ui --> map
map --> cdm
@enduml

I just get the text displayed

@rkrug
Copy link
Owner

rkrug commented Jun 18, 2020 via email

@rkrug
Copy link
Owner

rkrug commented Jun 18, 2020

Works for me as given:

---
title: "Untitled"
author: "Rainer M Krug"
date: "6/18/2020"
output: html_document
---

```{r setup, include=FALSE}
library(plantuml)
knitr::knit_engines$set(plantuml = plantuml_knit_engine)
```

## Example Plot

```{plantuml mapping-flow, plantuml.path="images"}
@startuml
left to right direction
scale 2.0

object "bron fysiek" as bron {
  Het fysieke datamodel
  van CATS/VLS/Alluris
}

object "bron UI/logisch" as ui {
  Het logische datamodel
}

note bottom {
  Dit is gelijk aan wat de gui van het bronsysteem laat zien.
  FB kan hiermee de juistheid van queries toetsen.
}

object "logica herstellen" as logic #lightgreen {
  het fysieke datamodel
  terugbrengen naar het model
  van de gebruikersinterface
}

object "mapping naar CDM" as map #lightgreen {
  vertalen van bron-entiteiten
  naar canonieke entiteiten
}

object "CDM" as cdm {
  Het canonieke datamodel
}

bron --> logic
logic--> ui
ui --> map
map --> cdm
@enduml
```

produces the following image

mapping-flow

@rkrug
Copy link
Owner

rkrug commented Jun 18, 2020

I installed from master using

devtools::install_github("rkrug/plantuml")

before.

@retorquere
Copy link
Author

Still the same; I installed using

if(!require(pacman)){
  install.packages("pacman")
  library(pacman)
}
p_load_gh("rkrug/plantuml")
p_update()
devtools::install_github("rkrug/plantuml")

(I don't usually have devtools::install_github("rkrug/plantuml") in my setup but I added it for diagnosis here).

if I run the following before my plantuml chunk (which replaces the registered engine) it does work:

knitr::knit_engines$set(plantuml = function(options) {
  code <- paste(options$code, collapse = "\n")
  
  if (is.null(options$plantuml.path)) {
    path <-  "."
  } else {
    path <- options$plantuml.path
  }
  
  dir.create(path, showWarnings = FALSE, recursive = TRUE)
  fig <- paste0(options$label, ".", "png")
  fig <- file.path(path, fig)
  if (options$eval) plot(x = plantuml(code), vector=FALSE, file=fig)
  knitr::engine_output(options, out = list(knitr::include_graphics(fig)))
})

@rkrug
Copy link
Owner

rkrug commented Jun 18, 2020

Could you

  1. post the output from sessionInfo()
  2. Try the Markdown file I send love and confirm if that one is giving the right output (i.e. an html file with the graph)?

@retorquere
Copy link
Author

sessionInfo():

R version 4.0.1 (2020-06-06)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.5

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] plantuml_0.3          DiagrammeR_1.0.6.1    datamodelr_0.2.2.9002 odbc_1.2.2            knitr_1.28            DBI_1.1.0            
 [7] keyring_1.1.0         rstudioapi_0.11       rmdformats_0.3.7      pacman_0.5.1         

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6       compiler_4.0.1     RColorBrewer_1.1-2 tools_4.0.1        digest_0.6.25      bit_1.1-15.2       jsonlite_1.6.1     evaluate_0.14     
 [9] pkgconfig_2.0.3    png_0.1-7          rlang_0.4.6        yaml_2.2.1         xfun_0.14          vctrs_0.3.1        htmlwidgets_1.5.1  hms_0.5.3         
[17] bit64_0.9-7        grid_4.0.1         grImport_0.9-3     glue_1.4.1         R6_2.4.1           XML_3.99-0.3       rmarkdown_2.2      bookdown_0.19     
[25] blob_1.2.1         magrittr_1.5       htmltools_0.5.0    assertthat_0.2.1   tinytex_0.23       visNetwork_2.0.9  

the document in the gist knits to an HTML file with <pre class="plantuml"><code> rather than the image: https://gist.github.com/d14ead3067c3b37344d94edec86b573b

@rkrug
Copy link
Owner

rkrug commented Jun 18, 2020

OK - try to add eval = TRUE to the code chunk:

```{plantuml mapping-flow, eval = TRUE, plantuml.path="images"}
...
```

It seems, that your document has somewhere set the default value of eval to FALSE.

@retorquere
Copy link
Author

It looks like you're right, adding eval=T to the gist document works, but in my own document I don't see where I would be setting eval to false globally (and all non-plantuml chunks do eval), and even when I add an explicit {plantuml mapping-flow, eval=T, plantuml.path="images"} I get text output.

Super weird, but not caused by the the plantuml engine it seems.

@rkrug
Copy link
Owner

rkrug commented Jun 18, 2020

If you post your document (you can send it privately, or trim it down), I could take a look at it.

By the way: using T instead of TRUE works in R, but it is not preferred as it is easier to have typos.

@rkrug
Copy link
Owner

rkrug commented Jun 18, 2020

Do you probably have echo = TRUE in the chunk or as a default option? because these two do not work together at the moment and will produce the code only and a line with the file name?

@retorquere
Copy link
Author

I did have echo = TRUE as a default, but removing that did not help.

I don't see any structural difference between your gist and this: https://gist.github.com/ad22113f885dadd181f9467669e0d458 , but mine just displays text.

@rkrug
Copy link
Owner

rkrug commented Jun 18, 2020

I missed it for at least 30 minutes - you are not registering any knit engine for plantuml!

Just add

knitr::knit_engines$set(plantuml = plantuml::plantuml_knit_engine)

to the setup chunk.

You Donn't even have to load the package plantuml.

@retorquere
Copy link
Author

Oh I thought packages would register the engine itself as part of the library init. Works!

@rkrug
Copy link
Owner

rkrug commented Jun 18, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants