juba / rainette

R implementation of the Reinert text clustering method
https://juba.github.io/rainette/
53 stars 7 forks source link

Clustering’s dendrogram with rainette_plot() displayed as table instead of plot #35

Open gabrielparriaux opened 2 months ago

gabrielparriaux commented 2 months ago

Hi,

Using Quarto, I usually have no problem to display the result of my clustering with rainette_plot() and to integrate the plot in the html rendering.

For code management reasons, I decided to externalise portions of my code in an external .R file.

So I have in the external .R file containing this:

rainette_dendrogram <- rainette_plot(
  res, dfm, k = 25,
  n_terms = 20,
  free_scales = FALSE,
  measure = "chi2",
  show_negative = TRUE,
  text_size = 8
)

And in my Quarto main document, first I have one chunk to source the .R file:

```{r}
#| label: import-reinert-clustering
#| include: false

# do the import
source("../includes/06_reinert_clustering.R", local = knitr::knit_global())

And after that one, I have another chunk like this:

````q
```{r}
#| label: fig-display-clustering
#| output: true
#| echo: false
#| fig-cap: "Dendrogram of clusters produced by Reinert's Clustering"

# plot dendrogram
rainette_dendrogram


Instead of rendering the plot of the dendrogram, it displays a table like that:

TableGrob (3 x 25) "arrange": 26 grobs
    z         cells    name           grob
1   1 ( 1- 1, 1-25) arrange gtable[layout]
2   2 ( 2- 3, 1- 1) arrange gtable[layout]
3   3 ( 2- 3, 2- 2) arrange gtable[layout]
4   4 ( 2- 3, 3- 3) arrange gtable[layout]
5   5 ( 2- 3, 4- 4) arrange gtable[layout]
6   6 ( 2- 3, 5- 5) arrange gtable[layout]
7   7 ( 2- 3, 6- 6) arrange gtable[layout]
8   8 ( 2- 3, 7- 7) arrange gtable[layout]
9   9 ( 2- 3, 8- 8) arrange gtable[layout]
10 10 ( 2- 3, 9- 9) arrange gtable[layout]
11 11 ( 2- 3,10-10) arrange gtable[layout]
12 12 ( 2- 3,11-11) arrange gtable[layout]
13 13 ( 2- 3,12-12) arrange gtable[layout]
14 14 ( 2- 3,13-13) arrange gtable[layout]
15 15 ( 2- 3,14-14) arrange gtable[layout]
16 16 ( 2- 3,15-15) arrange gtable[layout]
17 17 ( 2- 3,16-16) arrange gtable[layout]
18 18 ( 2- 3,17-17) arrange gtable[layout]
19 19 ( 2- 3,18-18) arrange gtable[layout]
20 20 ( 2- 3,19-19) arrange gtable[layout]
21 21 ( 2- 3,20-20) arrange gtable[layout]
22 22 ( 2- 3,21-21) arrange gtable[layout]
23 23 ( 2- 3,22-22) arrange gtable[layout]
24 24 ( 2- 3,23-23) arrange gtable[layout]
25 25 ( 2- 3,24-24) arrange gtable[layout]
26 26 ( 2- 3,25-25) arrange gtable[layout]

What did I do wrong? How can I manage to externalise the production of the plot and to call the result in my Quarto document?

Thanks a lot for helping again,

Gabriel
juba commented 2 months ago

Did you try to simply use print(rainette_dendrogram) ? Otherwise, it could be better to create a function that generates the plot and return rainette_dendrogram, then call the function from your quarto document.

gabrielparriaux commented 2 months ago

Thanks a lot for your answer!

Yes, I tried to just print(rainette_dendrogram) and the result is the same.

I tried with a function, as you propose.

This is the function I built in my .R external file:

plot_dendrogram <- function() {
  rainette_dendrogram <- rainette_plot(
    res, dfm, k = 25,
    n_terms = 20,
    free_scales = FALSE,
    measure = "chi2",
    show_negative = TRUE,
    text_size = 8
  )
  return(rainette_dendrogram)
}

It’s nearly working, but now, in a chunk of my Quarto document, when I call the function plot_dendrogram(), it produces both the table and the plot! I’m not sure to understand why…

Is there something I can do to have only the plot and not the table version?

Thanks again for helping!

juba commented 2 months ago

That's a bit strange indeed. Does it work with invisible(plot_dendrogram()) ?

gabrielparriaux commented 2 months ago

Yes, this way it works with invisible(plot_dendrogram()) correctly!

So I have this function in my external .R file returning the object produced by rainette_plot():

plot_dendrogram <- function() {
  rainette_dendrogram <- rainette_plot(
    res, dfm, k = 25,
    n_terms = 20,
    free_scales = FALSE,
    measure = "chi2",
    show_negative = TRUE,
    text_size = 8
  )
  return(rainette_dendrogram)
}

In my main Quarto file, I call the function with invisible(), so

invisible(plot_dendrogram())

And it displays correctly the plot from rainette and not the table.

Thanks a lot!