rkrug / plantuml

R package to build UML graphs using plantuml
https://rkrug.github.io/plantuml/index.html
GNU General Public License v3.0
76 stars 8 forks source link

Register as knitr engine #10

Closed retorquere closed 4 years ago

retorquere commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

retorquere commented 4 years ago

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:

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

How can I pull this dev branch into my notebook?

rkrug commented 4 years ago

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 commented 4 years ago

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 https://github.com/rkrug/plantuml/commit/6599cc84d67a8ac0a210973f35ede21df604da7c

retorquere commented 4 years ago

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

rkrug commented 4 years ago

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 commented 4 years ago

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

rkrug commented 4 years ago

Exactly.

Von meinem iPad gesendet

Am 17.06.2020 um 21:41 schrieb Emiliano Heyns notifications@github.com:

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

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub, or unsubscribe.

retorquere commented 4 years ago

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 commented 4 years ago

Could you try to add “plantuml.format = ‘png’” ?

Von meinem iPhone gesendet

Am 17.06.2020 um 23:22 schrieb Emiliano Heyns notifications@github.com:

ï»ż 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

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub, or unsubscribe.

rkrug commented 4 years ago

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

@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](https://user-images.githubusercontent.com/487172/84987858-419cb100-b141-11ea-9faa-46492b934f41.png)
rkrug commented 4 years ago

I installed from master using

devtools::install_github("rkrug/plantuml")

before.

retorquere commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

rkrug commented 4 years ago

No - I could do that, but that would make knitr an imported package, and not only a suggested.

On 18 Jun 2020, at 13:05, Emiliano Heyns notifications@github.com wrote:

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

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/rkrug/plantuml/issues/10#issuecomment-645946566, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADW6BAT3KPJLPIB43XBYJLRXHYHTANCNFSM4N7YRA7A.

-- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Orcid ID: 0000-0002-7490-0066

Department of Evolutionary Biology and Environmental Studies University of ZĂŒrich Office Y34-J-74 Winterthurerstrasse 190 8075 ZĂŒrich Switzerland

Office: +41 (0)44 635 47 64 Cell: +41 (0)78 630 66 57 email: Rainer.Krug@uzh.ch Rainer@krugs.de Skype: RMkrug

PGP: 0x0F52F982