2

The title of this post is pretty self-explanatory, so I will not waste your time reading more into this question.

I have tried to clean the r memory, to restart my laptop, delete the rmarkdown file, make a new file, and then knit to pdf.

The result is still the same: r code output is the different from pdf output.

---
title: "Mockup"
date: "`r format(Sys.time(), '%m/%d/%Y')`"
output: 
  pdf_document:
    fig_caption: yes
    fig_crop: no
    highlight: zenburn
    keep_tex: yes
    number_sections: false
    toc: yes
    toc_depth: 2
header-includes:
- \usepackage{example}
- \usepackage{nicematrix}
- \usepackage{hyperref}
- \usepackage{mathtools}
- \usepackage{amssymb}
- \usepackage{lettrine}
- \usepackage{amsmath}
- \usepackage{yfonts,color}
- \usepackage {titling}
- \usepackage{blkarray}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[R,R]{Mockup}
- \fancyhead[L,L]{Mockup}
- \fancyfoot[C,C]{Mockup}
- \fancyfoot[L,R]{\thepage}
---

```{r setup, include=FALSE, message=FALSE}
library(knitr)
library("DAAG")
library(tree)
library(MASS)
knitr::opts_chunk$set(echo = TRUE, fig.pos= "h", comment = NA, tidy=TRUE, tidy.opts=list(width.cutoff=37))
data(spam7, data ="DAAG")
Spam <- spam7[,c("crl.tot","dollar","bang",
                 "money","n000","make","yesno")]
Spam$yesno <- as.factor(Spam$yesno)
attach(Spam)
set.seed(1234)
sample_size <- floor(0.8*nrow(Spam))
ind <- sample(seq_len(nrow(Spam)),size = sample_size)
Spam.training <- Spam[ind,]
Spam.test <- Spam[-ind,]
```

```{r,echo = FALSE}
q1.tree <- tree(yesno ~., data = Spam.training)
par(cex = .7)
plot(q1.tree)
text(q1.tree, pretty = 0)
```

R code output (within the Rmarkdown file): R output PDF Output: pdf output

Any tip on how to fix this?

Thank you!

r2evans
  • 141,215
  • 6
  • 77
  • 149
Jo Mckenzie
  • 95
  • 1
  • 7
  • 3
    Please provide a [mre]. Otherwise we can only guess. – Ralf Stubner Nov 12 '19 at 22:21
  • @RalfStubner I just did. Thank you. – Jo Mckenzie Nov 12 '19 at 22:25
  • 1
    You are thinking about it wrong. A document made for a markdown shouldn't rely on any object in your work space. What is concrete when working with data is the code, not the temporarily objects saved in your workspace. What might be happening is that you set a chunk option to not save it's values. This might help: https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf – André Oliveira Nov 12 '19 at 22:26
  • 1
    In this case, I would also like a bit more description---*how* are the results different? – Gregor Thomas Nov 12 '19 at 22:26
  • 1
    Do you have anything in your yaml header? What options are you using when you call `rmarkdown::render`? What exactly is different between results on the R console and in the PDF output? – r2evans Nov 12 '19 at 22:28
  • 1
    @AndréOliveira I'm curious what objects in your workspace you are referring to? OP's example seems completely self-contained. – Gregor Thomas Nov 12 '19 at 22:32
  • @r2evans I just edited my post. Did I answer your question about YAML header? I am a newbie sorry. – Jo Mckenzie Nov 12 '19 at 22:33
  • 1
    Judging by the images, it seems the random seed is picking different values on the markdown. Try saving an object in each approach and comparing it – André Oliveira Nov 12 '19 at 22:34
  • 1
    @Gregor He didn't post the images before my comment - perhaps a chunck had a eval = FALSE, which is a common source of problem when you run a code inside and outside markdown concurrently. – André Oliveira Nov 12 '19 at 22:35
  • @AndréOliveira Hello, none of my chunk has eval = FALSE. – Jo Mckenzie Nov 12 '19 at 22:37
  • 1
    Might be this https://stackoverflow.com/questions/47897435/how-to-use-set-seed-globally-in-r-markdown https://stackoverflow.com/questions/41523204/knitr-html-output-showing-incorrect-strange-results-inline-code-and-modifying-o – André Oliveira Nov 12 '19 at 22:37
  • 1
    Two comments on your code: (a) You have a typo in `data` command, you need `package = "DAAG"`, not `data = "DAAG"`. If this is a typo in your actual code, it *could* be related to the problem if you are knitting from the command line rather than from the hotkey (as the knit session will have access to your global environment). (b) I'd **strongly** recommend not using `attach()`. You want to make sure, e.g., the `yesno` you give to your model is actually from the `data = Spam.training`, not the attached full version. `attach` causes many bugs. – Gregor Thomas Nov 12 '19 at 22:40
  • 1
    Jo, I cannot reproduce this. Granted: R-3.5.3, tree-1.0.39, and I trimmed out `header-includes` from your yaml header. – r2evans Nov 12 '19 at 22:41
  • I have tried everything you all recommended... – Jo Mckenzie Nov 12 '19 at 22:44
  • I have never encountered such a situation before. – Jo Mckenzie Nov 12 '19 at 22:49
  • 1
    What is the output of `RNGkind()` in both situations? – Ralf Stubner Nov 12 '19 at 22:56
  • @RalfStubner [1] "Mersenne-Twister" "Inversion" "Rejection" – Jo Mckenzie Nov 12 '19 at 22:59
  • GUYS, thank you all so much. My solution is to take the screen shot of the R output, and then use knitr::image() to include it in my PDF. The next guy who is gonna read my post will get agitated for sure. T_T – Jo Mckenzie Nov 12 '19 at 23:04
  • I tried to reproduce the issue, but no luck. I get your PDF results in both RStudio and PDF. I would try "Restart R and Run all Chunks" in RStudio. – Ralf Stubner Nov 13 '19 at 10:56

1 Answers1

0

The sample function doesn't return the same ind in Rmd and PDF. Both case I used set.seed(4).

From Rmd:

> ind <- sample(seq_len(nrow(Spam)),size = sample_size)
> print(ind)
   [1] 2696   42 1351 1276 3741 1197 3329 4163 4359  336 3465 1313  460 4378 1907 2088 4453 2678 4410 3491 3274 4565 2319
  [24] 2243 2972 3802 2206 3851 2350 2423 2593 1092 4012 2990 2203 4434 2101 2840 1773   31 4280 1105 2579  825 4122  385
  [47] 4105 4061 3294 2572 1769 3394 4074 3680 3722 1915  804  790 4055 3377 2548  324 3875 4147 1023 2852  314 2335 3651
  [70] 4405 1554 2861 1858 1566 3735 3123 1451 1997 1185  603 4117 3206 2585 4143 4549  265  207 4464  929 4192  877  572
  [93] 2433 1228 1594  379 3449 2002  163 3158 1141 2834 1199 2395 2106 2583 3005  954 4384 3636 1237 1793 1304 2213 3396
 [116] 2291 1966 3547 4086  319  640 1761 3836 1184  424 3243 4388  898 2471 1545 1826  841 4472  821 3633 3938 3129 1234
 [139] 1929   58 2512 2992 1399 4225  753 1963 2547 4342 3406 1973 1584 4503 2643 4277 1124 3767  946 1235 2130 4591 4002
 [162] 4189 1844 1483 1255  321 4535  705  669 3334 4477 2709  890 2229 4510   13 2850 1067 4529 3164  446 2683  939 3953

...

From PDF enter image description here

Bill Chen
  • 1,699
  • 14
  • 24
  • Hi Bill, I got the same result when using set.seed(4) in both r code and PDF. But when I used set.seed(1234), I got the issue back again. However, I understand your point. – Jo Mckenzie Nov 12 '19 at 22:58
  • @JoMckenzie, are you sure `sed.seed(4)` solved your problem? I double-checked, it didn't for me. I think when rendering the pdf, the system use a different seed. Anyway, if it works for you, I am happy! – Bill Chen Nov 12 '19 at 23:08
  • What I did was to change the seed (It is getting annoying I am sorry) for a few time. First, I set the seed to 4, then 1200, and then 1234, it worked... – Jo Mckenzie Nov 12 '19 at 23:08