読者です 読者をやめる 読者になる 読者になる

Rの結果をPDFに出力する(文字化け対応)

R

Rの結果をテキストファイルとpngファイルに出力する。
それらをWordで受け取ってPDFファイルに出力する。
Rのコード

data(iris)
mydata <- iris

比較 <- c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")

counter=1
for (hikaku in 1:length(比較)){
		
		セトサ <- subset(mydata,Species=="setosa")[[比較[hikaku]]]
		バージーカラー <- subset(mydata,Species=="versicolor")[[比較[hikaku]]]

		if (mean(セトサ)>mean(バージーカラー)) side <-"greater"
		if (mean(セトサ)<mean(バージーカラー)) side <-"less"
		if (mean(セトサ)==mean(バージーカラー)) side <-"two.sided"

		sink(paste("R結果1_",counter,".txt",sep=""))
		cat(比較[hikaku])
		cat("\n")
		cat("\n")
		cat("セトサ\n")
		print(summary(セトサ))
		cat("\n")
		cat("バージーカラー\n")
		print(summary(バージーカラー))
		sink()

		sink(paste("R結果2_",counter,".txt",sep=""))
		print(t.test(セトサ,バージーカラー,alternative=side))
		print(wilcox.test(セトサ,バージーカラー,alternative=side))
		sink()

		png(paste("R図_",counter,".png",sep=""),width = 360, height = 360)
		boxplot(セトサ,バージーカラー,names=c("セトサ","バージーカラー"))
		dev.off()
		counter <- counter+1
	}

shell.exec("PDF.docm")

Word VBAのコード

Option Explicit

Private Sub Document_Open()
        
        Dim current_path As String
        Dim picture_path As String
        Dim output_path As String
        
        current_path = ThisDocument.Path
        output_path = current_path & "/" & "検定結果.pdf"
        
        Dim counter As Integer
        Dim myFileName As String
        
        myFileName = Dir(ThisDocument.Path & "/" & "R図_" & "*.png")
        counter = 0
        Do Until myFileName = ""
            counter = counter + 1
            myFileName = Dir()
        Loop
        
        Dim i As Integer
        For i = 1 To counter
            Dim FSO As Object, TextFile As Object, buf As String
            Dim FSO2 As Object, TextFile2 As Object, buf2 As String
            Set FSO = CreateObject("Scripting.FileSystemObject")
            Set FSO2 = CreateObject("Scripting.FileSystemObject")
            Set TextFile = FSO.OpenTextFile(current_path & "/" & "R結果1_" & i & ".txt")
            Set TextFile2 = FSO2.OpenTextFile(current_path & "/" & "R結果2_" & i & ".txt")
            buf = TextFile.ReadAll
            buf2 = TextFile2.ReadAll

            Selection.InsertAfter (buf)
            Selection.EndKey unit:=wdStory
            
            picture_path = current_path & "/" & "R図_" & i & ".png"
            Selection.InlineShapes.AddPicture FileName:=picture_path
            Selection.EndKey unit:=wdStory
            
            Selection.InsertAfter (buf2)
            Selection.EndKey unit:=wdStory
        
            Set TextFile = Nothing
            Set FSO = Nothing
            
            Set TextFile2 = Nothing
            Set FSO2 = Nothing           
        
            If i < counter Then
               Selection.InsertBreak Type:=wdPageBreak
            End If
        Next i

        ActiveDocument.ExportAsFixedFormat _
        OutputFileName:=output_path, _
        ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=True, _
        Range:=wdExportAllDocument
        
        Dim d As Integer
        For d = 1 To counter
            Kill current_path & "/" & "R結果1_" & d & ".txt"
            Kill current_path & "/" & "R結果2_" & d & ".txt"
            Kill current_path & "/" & "R図_" & d & ".png"
        Next d
        
        Application.Quit (wdDoNotSaveChanges)
End Sub

「InsertFile」を使うと文字ばけするようである。

            Selection.InsertFile FileName:=current_path & "/" & "R結果1_" & n & ".txt"

そのため以下の方法に変更したら問題なかった。

            Dim FSO As Object, TextFile As Object, buf As String
            Set FSO = CreateObject("Scripting.FileSystemObject")
            Set TextFile = FSO.OpenTextFile(current_path & "/" & "R結果1_" & i & ".txt")
            buf = TextFile.ReadAll

            Selection.InsertAfter (buf)

これでknitrを勉強しないで済むのかな?