0

the codes in rmarkdown is:

rnorm(1)

Assume the result is 0.23. I want to save this 0.23 to my word document.

After cache=TRUE was set, the result every time I knit to word is the same but not equal to the current output 0.23.

How do I fix the current output and knit it to word?

Please do not use set.seed(). Because the rnorm is just a simple example for a procedure which has different outputs every time it runs and set.seed may not work.

Please do not cite variables in rmarkdown to fix an output.Citing does't work when the output in rmarkdown can't be cited such as a summary in a model. And all you have is a summary output and you can't cite it. In lm(linearmodel) you can cite every element in it while in many other models there is no such attributes thus you can't cite.

So fixing the current output is very important. Is it possible in rmarkdown?

adafdwwf
  • 162
  • 3
  • 12
  • If `set.seed` does not handle it for a stochastic process (that may call various different base-R random functions), then there is something else going on that is either (a) drawing external randomness (web?); or (b) using non-R random sources (some linked libraries including some `Rcpp` randomness do not honor R's `set.seed`, though there are ways even there to do it right). To my knowledge, there is no way to manage randomess without `set.seed`. Caching is an interesting thing, especially if one block caches and another does not ... inherently volatile. I suggest cache nothing in this case. – r2evans Jan 03 '19 at 06:03
  • It would be helpful if you provided a reproducible question that demonstrates the behavior you are talking about here. I suggest you make it a minimal working example, meaning reduce your code as much as you can while still mis-behaving. This also includes sample data (if applicable). Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Jan 03 '19 at 06:05
  • To make my question reproducible and simplify it as possible, I use a `rnorm` to generate different outputs and emphasized `set.seed()` can't be used. – adafdwwf Jan 03 '19 at 11:09
  • That's what I guessed from your question, but that does not help us. If we cannot see a simple example of things not working correctly, how can we get fix it? By "mwe" I mean literally give us a small rmd document in your question that fails as you suggest. – r2evans Jan 03 '19 at 15:11
  • Since you say you want to control randomness but exclude the use of `set.seed`, why not just hard-code the random number you got at one time? You can certainly show (not evaluate) a block showing random generating code, and evaluate (not show) code that uses your constants. (If you do this, is suggest you make a note as such in your paper, otherwise it looks strongly like fishing for a favorable outcome.) – r2evans Jan 03 '19 at 15:15
  • BTW, just in case, I apologize if you knew this: when you set `cache=TRUE`, it does not cache the previous run, it caches the next run. So if you previously ran it, observed `0.23`, then set `cache=TRUE`, your next run will most likely not be the same. Why? No attempt is made to save the cache of a block until `cache=TRUE` is set, so it cannot be saved until the next run, after which it will be available (unless/until you make a change to the code chunk, that is). – r2evans Jan 03 '19 at 15:58
  • That'a where the problem is. I run the chunk I get an output but the cache only save the next output. I want to save the current one because I can see it and I can say something based on the output I truly observed. – adafdwwf Jan 04 '19 at 04:08
  • I still cannot help much because I don't have a reproducible example. Please just provide a small *but complete* rmd document in your question. It can matter, really, I'm not just being difficult. – r2evans Jan 04 '19 at 13:33

1 Answers1

0

I may be oversimplifying this, but is something like the following .Rmd not what you're after?

---
title: "Saving RNorm"
author: "John Doe"
date: "3 January 2019"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Setting the value

```{r}
random <- rnorm(1)

plot(random)
```

## Using the value

As you can see in the chart above, the random number I produced is `r random`.

You can assign your random value to a variable, use it in whatever analysis or plot you need to, and then cite it in the text body by wrapping it in back quotes (prefixed with r).

The output this produces is below.

RMarkdown Microsoft Word example

g_t_m
  • 704
  • 4
  • 9
  • Citing a variable in rmarkdown is not what I want. It does't work when the output in rmarkdown can't be cited such as a summary in a model. And all you have is a summary output and you can't cite it. In `lm`(linearmodel) you can cite every element in it while in many others there is no such attributes thus you can't cite. So fix the current output is very important. – adafdwwf Jan 03 '19 at 11:01
  • Can you give an example of an element you can't cite? I am able to cite from the summary of a model. – g_t_m Jan 03 '19 at 13:46