Latex and R code

Hi All,

I am very new to R.I am looking to create a pdf document by integrating LATEX and R(.Rnw)
My data is in a csv file.PFB:
ID Name
1 A
2 B

I need to generate 2 pdfs since there are only two ids in this case(but the data can have N number of records).
PDF_FOR_ID_1

ID : 1
Name : A

PDF_FOR_ID_2
ID : 2
Name : B

The pdf genration should be dynamic based on the number of ids in the data csv.

I was trying with LATEX AND R but not able to print the value from csv file adjcent to the label

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\SweaveOpts{concordance=TRUE}
\begin{tabular}{l}
    \TextField{Study Name:} 
<<echo=FALSE>>=
d <- read.csv("D:\\Users\\Documents\\Data.csv")
m <-d$ID
print(m)
@
\TextField{Form:}
\end{tabular}
\end{document}

When i try to compile the pdf it is returning the error
! LaTeX Error: Something's wrong--perhaps a missing \item.

Kindly help.

Hi,

You might want to look into creating Sweave files in RStudio.

Here is another tutorial: RPubs - Introduction to Sweave (R and LaTeX)

Hope this helps,
PJ

Thank you.
I have tried with Sweave but the problem is the R script embedded is working if i run it alone.But when i compile the pdf with the latex and R code it is throwing error.

It seems to be a LaTeX error, not an R error. Did you try to compile the LaTeX part by itself?

Could it be that there is no separation (newline) between the \TextField and chunk or something along these lines (so you're effectively trying to put 3 elements inside a single tabular cell)?

2 Likes

I want the value to be printed to the side of the text like a form.We need to create the form template with all the labels and the corresponding values read from a csv file for each label.

Try compiling this:

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\SweaveOpts{concordance=TRUE}


You can include the fields without a table:

\noindent
\TextField{Study Name:}
<<echo=FALSE>>=
    m <- as.character(1:10)
    m.formatted <- paste(m, collapse="|")
    print(m)
@
\TextField{Form:}

\vspace{10 mm}

\hrule

\vspace{10 mm}

Or if you want to include it in the table, you can use \verb@\Sexpr@ to include the preformatted text. I don't think you can directly put a code chunk inside a tabular cell, this solution comes from \href{https://latex.org/forum/viewtopic.php?t=8727}{here}.

\begin{tabular}{l}
\TextField{Study Name:}\\
\Sexpr{m.formatted}\\
\TextField{Form:}
\end{tabular}
\end{document}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.