0

I'm following the following guide: https://joshldavis.com/2014/04/12/beginners-tutorial-for-knitr/

I've installed the knitr package and run it in R, and I'm trying to copy and paste the example from the link above:

From ISLR: Chapter 3, Problem 14.

Using a created simulated data, answer the questions regarding simple
linear regression.

<<>>=
# Ensure consistent values
set.seed(1)

# Create uniform distribution for first input
x1 <- runif(100)

# Normal distribution for second input
x2 <- 0.5 * x1 + rnorm(100) / 10

# Our Linear Model
y <- 2 + (2 * x1) + (.3 * x2) + rnorm(100)
@

However, when I try to run it, I get the following error in Latex:

! You can't use `macro parameter character #' in horizontal mode.
l.30 #
       Ensure consistent values
? 

I'm not quite sure what I'm doing wrong here? Is there any package I'm supposed to run in Latex as well?

WoeIs
  • 1,083
  • 1
  • 15
  • 25
  • 1
    You don't compile knitr code with latex directly. You use knitr to knit the document. – Dason Aug 30 '18 at 19:13
  • @Dason What exactly am I missing to make it work? – WoeIs Aug 30 '18 at 19:16
  • Use knitr to build your document directly instead of trying to compile with latex directly – Dason Aug 30 '18 at 19:27
  • @Dason I'm not sure I understand what you mean by "use knitr". I just want to be able to use Texmaker and implement some R codes in it while compiling Latex. – WoeIs Aug 30 '18 at 19:30
  • Woels, the `<<>>=` denoting a code block is specific to Sweave and not understood by LaTeX (and therefore TeXMaker). This means the file needs to be pre-processed with `knitr` in order to produce the actual file that LaTeX will work on. Some refs: https://www.johndcook.com/blog/2012/12/20/basics-of-sweave-and-pweave/ and (more recently) https://stat.ethz.ch/R-manual/R-devel/library/utils/doc/Sweave.pdf – r2evans Aug 30 '18 at 19:54
  • Since you're using TeXMaker, this suggests that you have specific needs for editing LaTeX directly. If you don't, however, might I suggest you look at using RMarkdown (one ref: https://rmarkdown.rstudio.com/), and use a non-TeX editor such as RStudio, emacs, or anything that understands the markdown format. This does not preclude the need to preprocess with `knitr`, it just means working in markdown vice latex. – r2evans Aug 30 '18 at 19:56
  • And it's been a while since I've used Texmaker but I'm sure there is a way to call an external command to compile the document. It sounds like you're relatively new to this though. Are you interested in including the actual R output? Or do you just want to include well formatted code in your document? – Dason Aug 30 '18 at 20:04

1 Answers1

0

Going from (A) R code and/or latex "code" to (B) an HTML or PDF document is a multi-part process. First, to compile your document, you need to run your R code and turn the results (and possible the original R code) into something understood by a Latex engine. Then, you need to compile that Latex code as you would a normal Latex document. The R package knitr will handle step 1; something like pdflatex will handle step 2. RStudio can automate the process so that both steps 1 and 2 happen with just the click of one button.

Right now, you are trying to do step 2 without first doing step 1, and this is because Texmaker doesn't know that some of the things you have typed are R code.

One "fix" is to use an "R Sweave" document in RStudio. Open RStudio and create a new R Sweave document:

enter image description here

Then write your document as you would a normal latex document, but you can also add R code between <<>>= and @. And when you're done, click "Compile PDF" and RStudio will take care of both (knitr/pdflatex) steps for you.

enter image description here

The RStudio website has a good tutorial for doing this with RMarkdown. You want to do the exact same thing, but with latex instead of markdown, but I imagine this tutorial will be helpful nonetheless.

If you run into errors: go to Tools > Global Settings > Sweave and ensure that the first two dropdown menus are set to what you want (most likely "knitr" for the first dropdown and "pdfLaTeX" for the second.

DanY
  • 5,920
  • 1
  • 13
  • 33
  • Thank you for the answer. My problem is that R isn't my "main area" and not something I use often, which is why I'd rather just use Texmaker as my standard text editing program since most of the times I won't be using any R coding. However the few times I do need some R statistics, I figured it would be nice to be able to incorporate an R script into Texmaker directly to make things a little more easy! – WoeIs Aug 30 '18 at 20:25
  • I see - the guide your following is almost certainly doing this with RStudio's Sweave documents. You might want to emphasize your need to stay with Texmaker in the question. – DanY Aug 30 '18 at 20:32