创建 Quarto 或 R Markdown 文档的代码块,其中源代码存储为向量中的元素。

huangapple go评论63阅读模式
英文:

Create Quarto or R Markdown Document Code Chunk with Source Code Stored as Element in a Vector

问题

I'm wondering if someone might be able to help figure out how (if it's possible) to take source code stored in a vector in R and pass it to a code chunk or code block in Quarto or Markdown. Essentially what I'm trying to do is the same thing as reading the source for a code chunk from an external data file using the file= option for a code chunk, but instead of pointing it to an external file containing my source, I wanted to pull the source from a vector element.

So for example, if I had a file called myfile.R that contained the following R code:

x <- rnorm(100)
hist(x)

And I wanted to pull this code into a code block and render it in Quarto or R, I could simply use:

```{R, echo=TRUE, file="myfile.R"}
```

And this would create a Quarto/Markdown document that prints the contents of myfile.R and then produces a histogram. However, what if the code is not stored in an external file but in an element of a vector in R? For example, say, I have the same code that was stored in myfile.R, only it's stored as a character variable inside source_code_vector, along with possibly some other elements containing source code as well:

source_code_vector <- c("x <- rnorm(100) hist(x)", "y<-rpois(100, 5) hist(y)")

How can I access this code in the first element of source_code_vector and pass it into the code chunk? I'd imagine it would be something along the lines of this (but this is obviously not right since the code isn't stored in a file but in a vector):

```{R, echo=TRUE, file=source_code[1]}
```

I realize I could always write out the value of the element to a text file and then read back in the text file into the code chunk, but this seems very inefficient, and I imagine there's a much better way to do this?

Thanks.

英文:

I'm wondering if someone might be able to help figure out how (if it's possible) to take source code stored in a vector in R and pass it to a code chunk or code block in Quarto or Markdown. Essentially what I'm trying to do is the same thing as reading the source for an code chunk from an external data file using the file= option for a code chunk, but instead of pointing it to an external file containing my source, I wanted to pull the source from a vector element.

So for example, if I had a file called myfile.R that contained the following R code:

x <- rnorm(100)
hist(x)

And I wanted to pull this code into a code block and render it in Quarto or R, I could simply use:

```{R, echo=TRUE, file="myfile.R"}
```

And this would create a Quarto/Markdown document that prints the contents of myfile.R and then produces a histogram. However, what if the code is not stored in an external file but in an element of a vector in R? For example, say, I have the same code that was stored in myfile.R, only it's stored as a character variable inside source_code_vector, along with possibly some other elements containing source code as well:

source_code_vector <- c("x <- rnorm(100) hist(x)", "y<-rpois(100, 5) hist(y)")

How can I access this code in the first element of source_code_vector and pass it into the code chunk? I'd imagine it would be something along the lines of this (but this is obviously not right since the code isn't stored in a file but in a vector):

```{R, echo=TRUE, file=source_code[1]}
```

I realize I could always write out the value of the element to a text file and then read back in the text file into the code chunk, but this seems very inefficient, and I imagine there's a much better way to do this?

Thanks.

答案1

得分: 2

你可以使用code选项来代替file选项:https://yihui.org/knitr/options/#code-chunk

```{r, code=source_code_vector[1]}
``` 
英文:

You can use the chunk option code instead of file: https://yihui.org/knitr/options/#code-chunk

```{r, code=source_code_vector[1]}
```

huangapple
  • 本文由 发表于 2023年2月16日 04:00:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定