【问题标题】:Passing values from an Rnw file to an R script将值从 Rnw 文件传递​​到 R 脚本
【发布时间】:2014-12-15 17:05:05
【问题描述】:

由于我是 knitr 的新手,因此我正在阅读和修改 knitr 网站中给出的示例。引起我注意的一种方法是在 Rmw 文件中调用 R 脚本块。在编译和修改了几个示例之后,我想知道是否可以在 Rmw 文件上设置变量并将其传递给 R 脚本。

这是一个例子

\documentclass{article}
\begin{document}
<<set-options, echo=FALSE, cache=FALSE>>=
options(replace.assign=TRUE)
opts_chunk$set(cache=TRUE, fig.show='asis')
read_chunk('simple_example.R')
@

\title{Example}
\author{Somebody}
\maketitle

\section{Print variable}

<<Print-data, echo=TRUE>>=
inp=2
@

\end{document}

# Simple Example

## ---- Print-data ----

inp=inp+2
print(inp)

输出结果为 ip=2 和错误消息“object inp not found”。

非常感谢

【问题讨论】:

    标签: r knitr


    【解决方案1】:

    代码块Print-data 覆盖了Rnw 文件中的代码块,因此inp=2 未被执行,因此出现错误。

    看来你想embed之后 Print-data:

    <<>>=
    inp=2
    <<Print-data>>
    @
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2021-06-08
      • 1970-01-01
      • 2020-08-10
      • 2018-02-27
      • 1970-01-01
      相关资源
      最近更新 更多