2

I have a large R script I am trying to source in a LaTex document and then reference output from specific dataframes created in the source code in text in a LaTex paragraph.

When I compile the PDF I receive an error message that object num_stations is not found.

A small example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\begin{document}
You can type R commands in your \LaTeX{} document and they will be properly run and the output printed in the document.

<<>>=
opts_chunk$set(echo=F,message=F, warnings=F)

#need com_dredge, table 2.1 and num_stations from this source code
source(file="S:/Adv/Scallop Central/2018 Scallop RSA/Industry Report/Industry Report.R")

#num_stations dataframe needed for text below
num_stations
@
This is a value $\Sexpr{num_stations$Number}$.

\end{document}

When I run the code chunk I dont think the source line is working. I have read a couple different options: 1. The source file needs to be in the same folder as the knitr file. I tried this an got the error message cannot open the connection. 2. Type out the path in the source code.
I also tried that and got the same message. 3. Use

  opts_knit$set(root.dir = '/Users/username/path_to_project').

I also tried this in the code chunk in the latex file and got the same message.

I would appreciate any help.

  • Are you looking for verbatim environment? – Sigur Feb 13 '19 at 21:09
  • I'm guessing this isn't the problem, but spaces in path names are occasionally problematic. Could you try a directory without any spaces? Could you also add a minimal R file that reproduces the problem? – Teepeemm Feb 13 '19 at 21:21
  • I wasnt sure how to provide a R file that reproduces the example because its a source file. Do you have any suggestions? I did try a directory with no spaces and didnt have any luck. – user41509 Feb 14 '19 at 14:23
2

foo.R (in the working directory):

# ---- test  ----

bah <- data.frame(a=c(1,2,3),b=c(4,5,6))

test.Rnw (in the same working directory):

\documentclass{article}
\usepackage{booktabs}
\begin{document}
<<mychuck, cache=F,echo=F>>=
read_chunk('foo.R')
@
<<test,echo=F>>=
@
<<mydata,result="asis",echo=F>>=
kable(bah, caption="my data", booktabs = TRUE)
@
The variable \verb|bah$a| is \Sexpr{combine_words(bah$a)}.
\end{document}

To compile the above file use Rstudio or see How to build Knitr document from the command line.

mwe

  • When I run your example I get the following errors: Line 5 Missing $ inserted, Line 12 Missing $ inserted and the PDF doesnt compile. Also, my question was related to getting values from a dataframe into test using $\Sexpr{num_stations$Number}$. – user41509 Feb 14 '19 at 14:34
  • @user41509 Sorry, I wrote wrongly test.tex instead of test.Rnw, but it should be a obvious mistake if you are used to compile knitr files. If not, I suggest use Rstudio. Once you loaded the chunk of the external script is irrelevant if you call the whole data frame in a table of another chunk or only a variable in a \Sexpr{}. It should work anyway. See my updated answer. – Fran Feb 14 '19 at 17:44
  • Thank you - I was able to get my source code to work with my document. This is my first time using Latex so am not familiar with the different ways to compile files. I am using R studio. – user41509 Mar 1 '19 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.