seminr icon indicating copy to clipboard operation
seminr copied to clipboard

Lambda symbol not rendered appropriately

Open francischuah opened this issue 4 years ago • 14 comments

I use the "save_plot" syntax to save the model plot. It seems like the "lambda" (loadings) symbol cannot be properly rendered. I attached two examples. One is in PNG form and another one is a screenshot from PDF. The symbol "lambda" (loadings) is appropriately displayed in R though. Image1 Image 2

I provide the code that I use to run the model. I would like to note that these two items were a product of "knit" in rmarkdown. The data used was the mobi dataset

measurements <- constructs(
  composite("Image",         multi_items("IMAG", 1:5)),
  composite("Expectation",   multi_items("CUEX", 1:3)),
  composite("Satisfaction",  multi_items("CUSA", 1:3)),
  composite("Complaints",    single_item("CUSCO")),
  composite("Loyalty",       multi_items("CUSL", 1:3))
)

structural <- relationships(
  paths(from = "Image",            to = c("Expectation", "Satisfaction")),
  paths(from = "Expectation",      to = c("Satisfaction", "Complaints")),
  paths(from = "Satisfaction",     to = "Complaints"), 
  paths(from = "Complaints",       to = "Loyalty")
)

# estimating the model
model_mobi <- estimate_pls(
  data = mobi,
  measurement_model = measurements,
  structural_model = structural
)

# bootstrapping the model
boot_model_mobi <- bootstrap_model( seminr = model_mobi,
                                  nboot = 1000, cores = 2
)

plot(boot_model_mobi, title = "Bootstrapped Model")
save_plot("mymodel.pdf")

francischuah avatar Jul 12 '21 12:07 francischuah

@francischuah I regret to say that I cannot reproduce the issue. The saved PDF file renders the greek letters well for me.

Could I ask you to run the code again and afterwards run sessionInfo()? Please report the results here.

My results, for example, are:

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.4

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   base     

other attached packages:
[1] seminr_2.1.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         rsvg_2.1.2         visNetwork_2.0.9   digest_0.6.27     
 [5] V8_3.4.2           DiagrammeRsvg_0.1  jsonlite_1.7.2     magrittr_2.0.1    
 [9] rlang_0.4.11       curl_4.3.1         rstudioapi_0.13    DiagrammeR_1.0.6.1
[13] RColorBrewer_1.1-2 tools_4.1.0        glue_1.4.2         htmlwidgets_1.5.3 
[17] yaml_2.2.1         parallel_4.1.0     compiler_4.1.0     htmltools_0.5.1.1 

I'm wondering if there is an issue with how encodings are set on your machine.

soumyaray avatar Jul 12 '21 14:07 soumyaray

@Sumidu bringing this to your attention as well.

soumyaray avatar Jul 12 '21 14:07 soumyaray

@soumyaray

A brief checking suggest that perhaps it is my locale i.e. encoding as you mentioned

R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Malaysia.1252  LC_CTYPE=English_Malaysia.1252    LC_MONETARY=English_Malaysia.1252 LC_NUMERIC=C                      LC_TIME=English_Malaysia.1252    

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

other attached packages:
[1] seminr_2.1.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         rsvg_2.1.2         visNetwork_2.0.9   digest_0.6.27      V8_3.4.2           DiagrammeRsvg_0.1  jsonlite_1.7.2     magrittr_2.0.1     rlang_0.4.11      
[10] curl_4.3.1         rstudioapi_0.13    DiagrammeR_1.0.6.1 RColorBrewer_1.1-2 tools_4.1.0        glue_1.4.2         htmlwidgets_1.5.3  yaml_2.2.1         parallel_4.1.0    
[19] compiler_4.1.0     htmltools_0.5.1.1 

francischuah avatar Jul 12 '21 14:07 francischuah

I think this may be due to how Windows uses a different encoding for symbols.

Until we have a fix, you can set the plot.specialcharacters variable in the theme to FALSE. SEMinR will then spell out "lambda" instead of using the symbol. Here's how you do it:

thm <- seminr_theme_create(plot.specialcharacters = FALSE)
plot(model, theme = thm)

Hope this helps 🙂

Sumidu avatar Jul 12 '21 21:07 Sumidu

You could also try to enforce UTF-8 encoding by calling options(encoding = 'UTF-8') in the setup chunk.

Sumidu avatar Jul 12 '21 22:07 Sumidu

Thank you for the quick reply.

francischuah avatar Jul 13 '21 01:07 francischuah

@Sumidu where should be encoding options be specified precisely? (not sure which setup chunk)

soumyaray avatar Jul 13 '21 02:07 soumyaray

@francischuah did you get this working?

soumyaray avatar Jul 26 '21 01:07 soumyaray

@francischuah did you get this working?

Hi Prof Soumya, i use the plot.specialcharacters as suggested. It seems like this has been a common problem in Rstudio

francischuah avatar Jul 26 '21 02:07 francischuah

Thanks @francischuah -- I'm guessing its because non-UTF8 encoding is less supported nowadays.

@Sumidu you can decide whether to close this issue (or perhaps add to the documentation)

soumyaray avatar Jul 26 '21 04:07 soumyaray

I am experiencing the same or similar issue. The lambda character renders fine in the RStudio but does not render with save_plot().

I am not very familiar with character encoding, but since I am in the United States and my encoding indicates UTF-8, I don't think this is the issue. I set my options to options(encoding = 'UTF-8') and this did not fix the issue.

Thank you for the workaround with plot.specialcharacters though, this will help me for now.

JakeNel28 avatar Feb 21 '23 21:02 JakeNel28

In my case if I use

library(DiagrammeRsvg)
library(xml2)
library(rsvg)
library(magrittr)
plot(pls_model) %>%
  export_svg() %>%
  charToRaw() %>%
  rsvg_pdf("graph.pdf")

then the lambda can't show but if i use

library(DiagrammeRsvg)
library(xml2)
library(rsvg)
library(magrittr)
plot(pls_model) %>%
  export_svg() %>%
  read_xml() %>%
  write_xml("graph.svg")

then lambda appear.

I started learning R since yesterday because there isn't a good alternative for this library in Python, so I couldn't pinpoint the specific problem.

Also, why can't we use the character '\U03B2'? Visually, it represents $\beta$, and in my case, it can be displayed. Since I'm not very familiar with R, after installing this library via conda, it seems that I don't have a direct way to modify the source code. Perhaps it would be better to provide users with an interface to modify the character to be used?

singledoggy avatar May 09 '24 09:05 singledoggy

R version 4.3.3 (2024-02-29)
Platform: x86_64-conda-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default

locale:
[1] C

time zone: America/Chicago
tzcode source: system (glibc)

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

other attached packages:
[1] DiagrammeRsvg_0.1 magrittr_2.0.3    rsvg_2.6.0        xml2_1.3.6       
[5] seminr_2.3.2     

loaded via a namespace (and not attached):
 [1] crayon_1.5.2       vctrs_0.6.5        cli_3.6.2          rlang_1.1.3       
 [5] jsonlite_1.8.8     glue_1.7.0         V8_4.4.2           htmltools_0.5.8.1 
 [9] IRdisplay_1.1      IRkernel_1.3.2     fansi_1.0.6        visNetwork_2.1.2  
[13] evaluate_0.23      fastmap_1.1.1      base64enc_0.1-3    lifecycle_1.0.4   
[17] DiagrammeR_1.0.11  compiler_4.3.3     RColorBrewer_1.1-3 htmlwidgets_1.6.4 
[21] Rcpp_1.0.12        rstudioapi_0.16.0  pbdZMQ_0.3-11      digest_0.6.35     
[25] repr_1.1.7         utf8_1.2.4         pillar_1.9.0       curl_5.1.0        
[29] uuid_1.2-0         tools_4.3.3       

It's my sessionInfo() for you to reproduce my environment.

singledoggy avatar May 09 '24 10:05 singledoggy

@francischuah @JakeNel28 Can your problem also be solved using my method above? If so, I can take some time to investigate how to fix this bug when I have the opportunity (although it may not be too soon).

singledoggy avatar May 11 '24 11:05 singledoggy