2018년 8월 15일 수요일

맥 R에서 한글, 일어 깨짐 해결

shell에서 R을 이용해 plot을 하면 일본어와 한글이 깨진다.
Xquartz에서 설정이 되어 있지 않기 때문이란다.
extrafont package를 사용하는 방법으로는 해결이 되지 않았다.
아래 코드를 ~/.Rprofile 파일에 저장하는 것으로 해결 되었다.
위는 일본어 깨짐을 위한 것이고, 아래는 한글 깨짐을 위한 것.
plot(1,1, main="테스트")
plot(1,1, main="テスト")
로 확인할 수 있다.

setHook(packageEvent("grDevices", "onLoad"),
    function(...){
        if(.Platform$OS.type == "windows")
            grDevices::windowsFonts(sans ="MS Gothic",
                                    serif="MS Mincho",
                                    mono ="FixedFont")
        if(capabilities("aqua"))
            grDevices::quartzFonts(
              sans =grDevices::quartzFont(
                c("Hiragino Kaku Gothic Pro W3",
                  "Hiragino Kaku Gothic Pro W6",
                  "Hiragino Kaku Gothic Pro W3",
                  "Hiragino Kaku Gothic Pro W6")),
              serif=grDevices::quartzFont(
                c("Hiragino Mincho Pro W3",
                  "Hiragino Mincho Pro W6",
                  "Hiragino Mincho Pro W3",
                  "Hiragino Mincho Pro W6")))
        if(capabilities("X11"))
            grDevices::X11.options(
                fonts=c("-kochi-gothic-%s-%s-*-*-%d-*-*-*-*-*-*-*",
                        "-adobe-symbol-medium-r-*-*-%d-*-*-*-*-*-*-*"))
        grDevices::pdf.options(family="Japan1GothicBBB")
        grDevices::ps.options(family="Japan1GothicBBB")
        }
)

attach(NULL, name = "JapanEnv")
assign("familyset_hook",
       function() {
            winfontdevs=c("windows","win.metafile",
                          "png","bmp","jpeg","tiff")
            macfontdevs=c("quartz","quartz_off_screen")
            devname=strsplit(names(dev.cur()),":")[[1L]][1]
            if ((.Platform$OS.type == "windows") &&
                (devname %in% winfontdevs))
                    par(family="sans")
            if (capabilities("aqua") &&
                devname %in% macfontdevs)
                    par(family="sans")
       },
       pos="JapanEnv")
setHook("plot.new", get("familyset_hook", pos="JapanEnv"))
setHook("persp", get("familyset_hook", pos="JapanEnv"))

# Winでは Rdevga を触ると, fontfaceに影響するので, より正しくは上記の
# 方法が現在理想とされる設定かもしれません.
# このシステムの開発が終わる事は無いでしょう.
# https://ameblo.jp/kazu-nakamata/entry-11551319335.html


# 한글깨짐을 위한 코드
setHook(packageEvent("grDevices", "onLoad"),
        function(...){
        if(capabilities("aqua"))
            grDevices::quartzFonts(
              sans =grDevices::quartzFont(rep("AppleGothic",4)),
              serif=grDevices::quartzFont(rep("AppleMyungjo",4)))
        grDevices::pdf.options(family="Korea1")
        grDevices::ps.options(family="Korea1")
        }
)
attach(NULL, name = "KoreaEnv")
assign("familyset_hook",
       function() {
            macfontdevs=c("quartz","quartz_off_screen")
            devname=strsplit(names(dev.cur()),":")[[1L]][1]
            if (capabilities("aqua") &&
                devname %in% macfontdevs)
                    par(family="sans")
       },
       pos="KoreaEnv")
setHook("plot.new", get("familyset_hook", pos="KoreaEnv"))
setHook("persp", get("familyset_hook", pos="KoreaEnv"))


# from http://prohannah.tistory.com/25

댓글 없음:

댓글 쓰기