tilltnet / egor

R Package for importing and analysing ego-centered-network data.
http://egor.tillt.net
GNU Affero General Public License v3.0
23 stars 4 forks source link

NULL when printing egor object #80

Closed raffaelevacca closed 1 year ago

raffaelevacca commented 1 year ago

When printing an egor object no data head is displayed, just NULL. This happens even though the egor object can be correctly analyzed. It seems to happen for any egor object, including those created in the main egor vignette.

Reproducible:

library(egor)

# Create egor
e1 <- egor(alters = alters32,
           egos = egos32,
           aaties = aaties32,
           ID.vars = list(
             ego = ".EGOID",
             alter = ".ALTID",
             source = ".SRCID",
             target = ".TGTID"))

# Prints NULL
e1

# The object is created correctly and can be analyzed
summary(e1)

# Filtering also prints NULL
e1[e1$ego$age.years > 35, ]
e1 %>% 
  filter(income > 36000)

Print output in my console:

Screen Shot 2022-12-02 at 4 34 30 PM

Session info:

R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.5.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/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  
[7] base     

other attached packages:
[1] egor_1.22.5  tibble_3.1.8 dplyr_1.0.9 

loaded via a namespace (and not attached):
 [1] pillar_1.8.1     compiler_4.1.2   tools_4.1.2     
 [4] digest_0.6.29    evaluate_0.16    lifecycle_1.0.1 
 [7] lattice_0.20-45  pkgconfig_2.0.3  rlang_1.0.4     
[10] igraph_1.3.4     Matrix_1.4-0     tidygraph_1.2.2 
[13] DBI_1.1.1        cli_3.3.0        rstudioapi_0.13 
[16] yaml_2.2.1       xfun_0.32        fastmap_1.1.0   
[19] knitr_1.40       srvyr_1.1.1      generics_0.1.3  
[22] vctrs_0.4.1      mitools_2.4      grid_4.1.2      
[25] tidyselect_1.1.2 glue_1.6.2       R6_2.5.1        
[28] fansi_0.5.0      survival_3.2-13  rmarkdown_2.16  
[31] bookdown_0.28    purrr_0.3.4      tidyr_1.2.0     
[34] magrittr_2.0.3   ellipsis_0.3.2   htmltools_0.5.2 
[37] splines_4.1.2    assertthat_0.2.1 utf8_1.2.2      
[40] survey_4.1-1     crayon_1.4.2    
mbojan commented 1 year ago

As far as I can tell print.egor() uses a deprecated way of printing tibbles. I'm looking into fixing it.

raffaelevacca commented 1 year ago

Thank you @mbojan! Printing works well now.

raffaelevacca commented 1 year ago

@mbojan the ANSI code style string for coloring "active" in green in print.egor() output is giving me problems when rendering to pdf an Rmarkdown with egor print output. It's the \033[32m and \033[39m part in this line: https://github.com/tilltnet/egor/blob/e5980a565c7805bfc59b7151668b75b36c5a96ef/R/egor.R#L337

When rendering Rmarkdown to pdf for some reason those strings create an invalid character that Latex can't render to pdf regardless of the latex engine (pdflatex, xelatex or lualatex). The result is the Rmd can't be rendered to pdf due to invalid character error (Text line contains an invalid character). It's similar to the issue described here, except no latex engine works: https://bookdown.org/yihui/rmarkdown-cookbook/latex-unicode.html

Can I remove that part (\033[32m and \033[39m) i.e. remove the green coloring from print.egor() output? Or do you want to look into that first? Note there is no green coloring of "active" in the tidygraph print output.

Reproducible:

---
title: "Test"
output: 
  pdf_document:
    latex_engine: xelatex
date: '2022-12-05'
editor_options: 
  chunk_output_type: console
---

```{r}
library(tidyverse)
library(egor)

# Create egor
e1 <- egor(alters = alters32,
           egos = egos32,
           aaties = aaties32,
           ID.vars = list(
             ego = ".EGOID",
             alter = ".ALTID",
             source = ".SRCID",
             target = ".TGTID"))

# Print egor
e1

# Note no green coloring in tidygraph print output
library(tidygraph)
as_igraph(e1) |> 
  pluck(1) |> 
  as_tbl_graph()
mbojan commented 1 year ago

I'll be happy to get rid of it all together, but it's a baby of @tilltnet ;) imho we need to either do some sort of output-specifc-printing (i don't remember how pillar does it) or get rid of it.

raffaelevacca commented 1 year ago

@tilltnet what do you think? Do you see any other solution (other than getting rid of it) to avoid that error when rendering to pdf?

Also note that the \033 character is disregarded and green coloring is not rendered in htlm, see for example the egor vignette: https://cran.r-project.org/web/packages/egor/vignettes/using_egor.html#base-r

Screen Shot 2022-12-05 at 9 51 17 PM
tilltnet commented 1 year ago

If we want to keep the coloring, the cli package could be used for colored print output. crayon is deprecated but works.

@mbojan Yes, please go ahead and remove the coloring or feel free to look into how to do it with cli or crayon. Thank you so much either way for looking into this!

raffaelevacca commented 1 year ago

I removed the coloring string for now (b6155ca), it's easy to put it back in if needed, or we can look into other coloring solutions. That should probably be a separate issue though, so I'm closing this issue. Pdf rendering works now, thank you @mbojan and @tilltnet.