如何在复制/粘贴到Word时保留rstudio的格式?

时间:2012-04-12 17:30:07

标签: formatting ms-word rstudio

R,Rstudio和SO的新手 - 我提前为任何失礼道歉。

我想在Word 2010中重现我的代码以完成家庭作业。这些脚本是用rstudio编写的,我想在粘贴到Word时保留rstudio的格式。原则上,我喜欢rstudio使用的字体颜色和间距。我发现当我从SAS粘贴到Word时,格式化会被保留,但这里没有骰子。

我通常会寻找复制特殊/粘贴特殊选项来做到这一点,但我找不到任何。当我尝试将特殊粘贴到单词时,仅显示未格式化的文本选项。我宁愿不逐行重新格式化文本,因为我觉得它在rstudio中看起来很不错。

我想过尝试将rstudio中的脚本保存为某种格式,以保留其格式,但我找不到任何方法来执行此操作。有没有人知道如何解决这个问题?

提前致谢

9 个答案:

答案 0 :(得分:30)

目前还不完全清楚你是从RStudio的脚本编辑器(有4或5种颜色)粘贴还是从RStudio中的R控制台(脚本+输出)粘贴(只有2种颜色)。

如果您从控制台粘贴 - 请再次选中“粘贴特殊”。应该有一个“HTML格式”选项可以满足您的需要(尽管您可能需要调整字体大小以使一切正常,具体取决于页边距)。

如果您是从脚本编辑器粘贴的,那么您对直接复制粘贴解决方案感到不满意。但是有一个复制粘贴和复制粘贴解决方案......

一种解决方案可能是使用Notepad++。从RStudio,保存您的脚本(扩展名为“.R”),然后在Notepad ++中打开脚本。 (或者从RStudio复制并粘贴到Notepad ++,但请确保将文件的语言 - 从“语言”菜单 - 设置为R)。在Notepad ++中正确突出显示脚本后,请转到“插件> NppExport>将HTML复制到剪贴板”菜单以复制打开的文件。然后可以将其粘贴到HTML格式的MS Word中。

答案 1 :(得分:17)

Just in case someone else looks for this question...

Another way to have all the source code in a word document with a good-looking format using RStudio is to use the File/Compile Notebook option, choosing MS Word as the output format.

Using this option, a .docx document will be generated with the output of your script as well as the original source code. The script will be executed, though.

If you don't want your code to be evaluated (you just want a simple copy-paste), you can add #+eval=FALSE at the beginning of your script and then the source code will be reproduced in the word document without being evaluated.

This approach relies on knitr. Here is an example if anyone wants to start playing with this.

#' ---
#' title: "My homework"
#' author: John Doe
#' date: June 15, 2015
#' output: word_document
#' ---

# The header above sets some metadata used in the knitr output

# Conventional comments are formatted as regular comments

# Comments starting with "#+" control different knitr options.

#+echo=FALSE,message=FALSE,warning=FALSE
library(ggplot2)


#+echo=TRUE
#' Comments with a "+" sign are used to tell knitr what should be
#' done with the chunk of code:
#'
#'  - echo: Show the original code or not
#'  - eval: Run the original code or not
#'  - message: Print messages
#'  - warning: Print warnings
#'  - error: Print errors
#'  ...

#' Comments with an apostrophe "'" will be printed as regular text.
#' This is very useful to explain what you are actually doing!

# Regular comments can be used to document the code as usual
# Figures are printed:
ggplot(mpg, aes(x=cty, y=hwy)) + geom_point(aes(color=class))

#' Formatting **options** are possible.
#' Even [links](http://stackoverflow.com/questions/10128702/how-to-preserve-formatting-from-rstudio-when-copy-pasting-to-word)
#'


#' This will show all the packages and versions used to generate this document.
#' It can be used to make sure that your teacher has all he needs to run your script
#' if he/she wants to.
sessionInfo()

Word document example

答案 2 :(得分:8)

假设您有互联网访问

  1. 复制并粘贴到gist.gisthub.com
  2. 选择“R”作为语言 - 这应该提供颜色
  3. 点击创建(秘密或公开)要点
  4. 从要点复制并粘贴到文字处理器。
  5. 与记事本++解决方案相比:

    • 对您的代码进行在线备份,并记录您剪裁的时间。
    • 您不必安装任何其他软件,如果您是使用公共计算机的学生,则非常有用。

答案 3 :(得分:1)

我同意zeehio使用Knitr可能是最好的选择。但另一种方法是使用Pretty R tool和“打开文档文本”步骤here。基本上只需将代码复制并粘贴到漂亮的R中,然后将输出(而不是html)复制并粘贴到打开的文档中。

答案 4 :(得分:1)

如果您只需要格式化的代码:

第一步:只需在代码开头添加#+ eval = FALSE。

步骤2:然后转到“文件”->“编织文档”。以msword / PDF / Html格式编译文件。

OR

只需在代码开头添加#+ eval = FALSE。

按CTRL + SHIFT + K,然后以msword / PDF / Html格式编译文件。

如果您需要带输出的代码,请不要在代码开头输入add#+ eval = FALSE并直接执行第2步。

答案 5 :(得分:0)

从Rstudio控制台窗口复制并粘贴到Word文档后,您需要突出显示所有刚刚复制的文本,并将字体更改为Courier New。这将为您提供与Rstudio控制台窗口中相同的间距和阵容。

答案 6 :(得分:0)

复制将代码从Rstudio编辑器粘贴到“ Visual Studio代码”,然后再次从那里复制到文字处理器中。 为此,您必须首先在Visual Studio代码中安装R扩展。 “ Visual Studio代码”本身就是一个IDE,也可以用于R语言,但现在我强调使用它来回答上述问题。

答案 7 :(得分:0)

在 R 中,我使用 Monaco 编辑器字体。要将 R consol 的输出复制粘贴到 Microsoft Word 中,我选择了 consol 的输出,右键单击并复制并粘贴到我的 Word 文档中。将输出粘贴到 word 中后,我选择它并将其放入 Word 的 Monaco 字体中,并在必要时减小字体大小。

这很好地完成了这项工作,并且完美地保留了 R 控制台的输出样式,以及编写的代码块。

答案 8 :(得分:0)

如果您想在处理来自 R 控制台的选择时保留格式,您将需要安装旧版本的 R Studio。版本 1.2.5042。它在较新的版本中不起作用