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

dub*_*ing 29 formatting ms-word rstudio

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

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

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

我想到尝试将rstudio中的脚本保存为某种格式,以保留其格式,但我找不到任何方法来执行此操作.有没有人知道如何把它拉下来?

提前致谢

A5C*_*2T1 31

您是从RStudio的脚本编辑器(有4或5种颜色)粘贴还是从RStudio中的R控制台(脚本+输出)粘贴(仅有2种颜色)并不完全清楚.

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

如果您从脚本编辑器粘贴,那么您将无法使用直接复制粘贴解决方案.但是有一个复制粘贴和复制粘贴解决方案......

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


zee*_*hio 20

以防其他人寻找这个问题......

将所有源代码放在具有使用RStudio的漂亮格式的word文档中的另一种方法是使用该File/Compile Notebook选项,选择MS Word作为输出格式.

使用此选项,.docx将生成包含脚本输出和原始源代码的文档.但是,脚本将被执行.

如果您不希望评估您的代码(您只需要一个简单的复制粘贴),您可以#+eval=FALSE在脚本的开头添加,然后源代码将在word文档中复制而不进行评估.

这种方法依赖于knitr.这是一个例子,如果有人想开始玩这个.

#' ---
#' 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()
Run Code Online (Sandbox Code Playgroud)

Word文档示例


sfu*_*fuj 8

假设你有互联网接入

  1. 复制并粘贴到gist.gisthub.com
  2. 选择"R"作为语言 - 这应该提供颜色
  3. 点击创建(秘密或公共)要点
  4. 从要点复制并粘贴到文字处理器.

与notepad ++解决方案相比:

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

  • 不再起作用了,他们似乎已经删除了选择语言的选项 (2认同)