我可以在R Markdown / Knitr文档中使用以前准备好的变量吗?
例如,我首先在RStudio中创建 $sql = "SELECT id, nivel, tipo, titulo, texto, ativa FROM account.quests_faq WHERE ativa='YES' ORDER BY nivel DESC";
// look this string contains SQL query. so, the variable is named $sql
$stmt = $conn->query($sql);
// in the next line we are getting a statement object from the function query()
// this is why variable called $stmt
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
// and now we can start iterating this statement.
// statement, Carl. Not connection to database
// which is called $conn, if you get an idea
。它在全局环境/值中显示为列表。然后我想从R Markdown中调用它,例如:
g <- ggplot(df,...
我希望有一个很好的R Markdown文档,没有大量代码。
答案 0 :(得分:2)
使用RData的一个选项:准备ggplot并将其保存为RData,然后以降价方式加载。
PreparePlot.R
String accessToken;
accessToken = accessToken.substring(13,accessToken.lastIndexOf("&"));
myReport.Rmd
library(ggplot2)
myPlot <- ggplot(mtcars,aes(mpg,cyl)) + geom_point()
save.image("myPlot.RData")
另一种选择是使用---
title: "Untitled"
output: html_document
---
```{r GGPlot}
library(ggplot2)
load("C:/Path/to/myPlot.RData")
myPlot
```
:
myPlotSource.R
source
myReport.Rmd
library(ggplot2)
myPlot <- ggplot(mtcars,aes(mpg,cyl)) + geom_point()
print(myPlot)