R Eps Export and Import into Word 2010

R eps export and import into Word 2010

This worked for me... following advice in the postscript help page:

 postscript("RPlot.eps", height = 4, width = 4, horizontal = FALSE, onefile = FALSE,
paper = "special")
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth()
p
#geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to #change the smoothing method.
#Warning message:
#In grid.Call.graphics(L_polygon, x$x, x$y, index) :
# semi-transparency is not supported on this device: reported only once per page
dev.off()
#quartz
# 2

The funny stuff at the end puts you on notice that this is only a Mac-tested solution, so far anyway.

Edit: I just tested it with R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows": Platform: i386-pc-mingw32/i386 (32-bit) and MS Word 2007 in Win XP and it worked. Commands were Insert/Picture.../select eps format/select file.

Edit2: There is another method for saving besides directly using the postscript device. The savePlot method with an "eps" mode is available in Windows (but not in the Mac). I agree that the fonts are not as smooth as they appear on a Mac but I can discern no difference in quality between saving with savePlot and using save as from an interactive window.

savePlot(filename = "Rplot2", type = "eps", device = dev.cur(), restoreConsole = TRUE)

savePlot calls (.External(CsavePlot, device, filename, type, restoreConsole))

R .eps export and import into Word 2010

R eps export and import into Word 2010
Problem solved by Marco Sandri with exporting .eps files from R and importing into Word 2010 on Windows using the colormodel="rgb" option (defaults to "srgb") of the postscript command. Thanks

Word 2010 Interop PDF Export missing border line

It turned out to be a very subtle (and yet to be identified) class conflict issue. After moving the converter to a separate class library, it is working fine.

I am posting my code here, in case someone is interested.

    public static void ConvertToPdf(string fileName, string outputFileName)
{
Application word = null;
try
{
word = new Application();

Object tempName = fileName;
// Cast as Object for word Open method
Object confirmConversions = false;
Object readOnly = true;
Object addToRecentFiles = false;
Object visible = false;
Object openAndRepair = true;
Object format = WdOpenFormat.wdOpenFormatXML;
object oMissing = Missing.Value;
// Use the dummy value as a placeholder for optional arguments
Document doc = word.Documents.Open(
ref tempName,
ref confirmConversions,
ref readOnly,
ref addToRecentFiles,
ref oMissing, //PasswordDocument
ref oMissing, //PasswordTemplate
ref oMissing, //Revert
ref oMissing, //WritePasswordDocument
ref oMissing, //WritePasswordTemplate
ref oMissing, //Format
ref oMissing, //Encoding
ref visible, //Visible
ref openAndRepair, //OpenAndRepair
ref oMissing, //DocumentDirection
ref oMissing, //NoEncodingDialog
ref oMissing //XmlTransform
);
doc.Activate();

const WdExportFormat exportFormat = WdExportFormat.wdExportFormatPDF;
const bool openAfterExport = false;
const WdExportOptimizeFor optimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
const WdExportRange range = WdExportRange.wdExportAllDocument;
const WdExportItem item = WdExportItem.wdExportDocumentWithMarkup;
const bool includeDocProps = false;
const bool keepIrm = false;
const WdExportCreateBookmarks createBookmarks = WdExportCreateBookmarks.wdExportCreateHeadingBookmarks;
const bool docStructureTags = false;
const bool bitmapMissingFonts = false;
const bool useIso190051 = false;

doc.ExportAsFixedFormat(
outputFileName,
exportFormat,
openAfterExport,
optimizeFor,
range,
0,
0,
item,
includeDocProps,
keepIrm,
createBookmarks,
docStructureTags,
bitmapMissingFonts,
useIso190051
);

// Close the Word document, but leave the Word application open.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document) doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
}
catch (Exception ex)
{
Logger.Fatal(ex);
}
finally
{
if (word != null)
{
((_Application) word).Quit();
word = null;
}
}
}


Related Topics



Leave a reply



Submit