3

問題を示すコードは次のとおりです。

myPath = getwd()
cat("abcd", append = T, file =paste(myPath,"temp1.html", sep = "\\")) # This is fine
cat("<BR/><BR/><BR/>", append = T, file =paste(myPath,"temp1.html", sep = "\\")) # This is fine
cat("שלום", append = F, file =paste(myPath,"temp1.html", sep = "\\")) # This text gets garbled when the html is opened using google chrome on windows 7.
cat("שלום", append = F, file =paste(myPath,"temp1.txt", sep = "\\")) # but if I open this file in a text editor - the text looks fine

# The text in the HTML folder would look as if I where to run this in R:
(x <- iconv("שלום", from = "CP1252", to = "UTF8") )
# But if I where to try and put it into the file, it wouldn't put anything in:
cat(x, append = T, file =paste(myPath,"temp1.html", sep = "\\")) # empty

編集: 私も次のエンコーディングを使用してみました(成功しませんでした)

ff <-file(paste(myPath,"temp1.html", sep = "\\"), encoding="CP1252")
cat("שלום", append = F, file =ff)
ff<-file(paste(myPath,"temp1.html", sep = "\\"), encoding="utf-8")
cat("שלום", append = F, file =ff)
ff<-file(paste(myPath,"temp1.html", sep = "\\"), encoding="ANSI_X3.4-1986")
cat("שלום", append = F, file =ff)
ff<-file(paste(myPath,"temp1.html", sep = "\\"), encoding="iso8859-8")
cat("שלום", append = F, file =ff)

助言がありますか?ありがとう。

4

3 に答える 3

1

問題はRではありません(RはUTF-8でエンコードされた出力を正しく生成しています)…明示的に指定されたエンコードがない場合、Webブラウザーが間違ったエンコードを想定しているだけです。代わりに、(Rの内側から)次のスニペットを使用してください。

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
    </head>
    <body>
        שלום
    </body>
</html>

これにより、正しいエンコーディング(UTF-8)が指定されるため、ブラウザは次のテキストを正しくスレッド化します。

于 2011-09-20T10:45:29.013 に答える
1

あなたのコードは少し冗長です。temp1.txt5行目はタイプミス(.html)ですか?とにかく、おそらくタグ内に文字セットを設定する必要があります。<meta>

これを例として取り上げます。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
cat("abcd")
cat("<BR/><BR/><BR/>")
cat("שלום")
cat("שלום")
(x <- iconv("שלום", from = "CP1252", to = "UTF8") )
cat(x)
-%>
</body>
</html>

これはbrewコードなので、先に進んでbrewいくと正しい応答が得られます。簡単に言うと、キーワードは文字セットでした。

于 2011-09-20T10:51:30.553 に答える
1

この方法で試してください

cat("abcd", file = (con <- file("temp1.html", "w", encoding="UTF-8"))); close(con)
于 2011-09-20T12:30:44.923 に答える