daqana / tikzDevice

A R package for producing graphics output as PGF/TikZ code for use in TeX documents.
https://daqana.github.io/tikzDevice
132 stars 26 forks source link

bug in anyMultibyteUTF8Characters() #156

Closed jszhao closed 6 years ago

jszhao commented 7 years ago

The bug is in the following line

  if(Encoding(string) == "unknown")
    Encoding(string) <- encoding

here is a mini example that demo the bug:

> A <- "模式"
> Encoding(A)
[1] "unknown"
> Encoding(A) <- "UTF-8"
> A
[1] "ģʽ"
> enc2utf8(A)
[1] "ģʽ"

I think the above two lines could be remove, since string <- enc2utf8(string) does all needed.

jszhao commented 7 years ago

Alternative way is to use stringi package, something like:

require(stringi)
curr_enc <- stri_enc_get()
string <- stri_conv(string, from = curr_enc, to = "UTF-8") #now string is in UTF-8
yihui commented 7 years ago

Your solution seems to be correct to me. Could you send a pull request? Thanks!

jszhao commented 7 years ago

Because I am not familiar with git, I don't know how to pull request. I will try later.

jszhao commented 7 years ago

Because I don't know how to pull request on Github, I just paste the diff here, so someone can help. Thanks!

--- tikzDevice-master/R/deviceUtils.R   2016-06-21 14:18:12.000000000 +0800
+++ tikzDevice/R/deviceUtils.R  2017-04-04 11:28:27.741878200 +0800
@@ -231,9 +231,9 @@

   mb <- FALSE

-  # Set the encoding of the string if it is not explicitly set
-  if(Encoding(string) == "unknown")
-    Encoding(string) <- encoding
+# # Set the encoding of the string if it is not explicitly set
+# if(Encoding(string) == "unknown")
+#   Encoding(string) <- encoding

   # convert the string to UTF-8
   string <- enc2utf8(string)
yihui commented 7 years ago

That is okay. Much appreciated!

If you want to try Github pull requests anyway, it is actually pretty simple and here is how: go to https://github.com/yihui/tikzDevice/blob/master/R/deviceUtils.R, hit the edit button (the button on the left side of the trash button), remove the lines you mentioned, scroll to the bottom of the page, write a message, and Github will guide you to create a pull request. I think it will be fun to send your first pull request :)

jszhao commented 7 years ago

@yihui thanks for the guide. I have created a pull request https://github.com/yihui/tikzDevice/pull/158.