Error in compiling RStudio calculated table using xtable function

Asked 2 years ago, Updated 2 years ago, 120 views

I'm a super beginner.I started studying TeX in order to easily print out the tables I made with RStudio.
(In TeXlive 2017, RStudio is Version 1.0.136)
I use RStudio.Output the table calculated by R using the xtable function and
by TeX I thought about compiling it.After compiling the following code by outputting the TeX code and pasting it to the source as shown in 2.

The font size command\normal size is not defined:

Two errors and

Environmental table undefined
begin { document } is ended by \end {table }
This file needs 'pLaTeX2e'

said he.
R Studio's Global Option Sweave item type set is XeLaTeX, but
There is no option for pLaTeX2e.
What should I study first to solve this problem?

1.R Code

>ans<-table(newvariable$bq6_2)
>p.ans<-round(ans*100/sum(ans), 1)
>cbind(ans, p.ans)
asp.ans
hh_under 1865921.2
hh_over 6585727.6
hh_others 159251.2
>table1<-cbind(ans, p.ans)
>tb1<-xtable(table1)
>print(tb1)
% latex table generated in R 3.4.0 by xtable 1.8-2 package
% Wed Jun 28 21:25:29 2017
\begin {table} [ht]
\centering
\begin {tabular} {rrr}
\hline
&ans & p.ans\\ 
\hline
US>hh\_under 18&659.00&21.20\\ 
US>hh\_over 65&857.00&27.60\\ 
US>hh\_others&1592.00&51.20\\ 
\hline
\end {tabular}
\end {table}
> 

2.TeX Code

\ documentclass [11pt] {jsarticle}
\begin { document }
% latex table generated in R 3.4.0 by xtable 1.8-2 package
% Wed Jun 28 21:25:29 2017
\begin {table} [ht]
\centering
\begin {tabular} {rrr}
\hline
&ans & p.ans\\
\hline
US>hh\_under 18&659.00&21.20\\
US>hh\_over 65&857.00&27.60\\
US>hh\_others&1592.00&51.20\\
\hline
\end {tabular}
\end {table}
\end { document }

r latex

2022-09-30 13:59

2 Answers

What should I study first to solve this problem?

If this is the purpose of your question, the answer is simple.I think it's better to learn how to use LaTeX, XeLaTeX, LuaLaTeX, etc. that corresponds to Japanese before entering the R code.

If you can sort out whether it's an R/RStudio problem, LaTeX general problem, or a problem specific to Japanese LaTeX, you'll be more likely to find your own answer, and even if you don't know it yourself, you'll be more likely to get it.It lowers the hurdle of the respondent and makes it easier to reach the right community.

documentclass is a problem specific to RStudio, but compiling using platex+dvipdfmx is troublesome if you want to complete it on RStudio, so you should avoid the jsarticle class.

Output the table calculated by R using the xtable function and
by TeX I thought about compiling it.Print out the TeX code below and paste it into Source as shown in 2. Compile

There is a better way to copy tables generated by R to LaTeX sources.

In any case, you will learn about the knitr package.If you don't need the full power of LaTeX composition, you can learn how to write R Markdown by referring to here.Output to other formats such as HTML output is also supported.

Personally, I use RSweave (Rnw format) when I use complex compositions, and R Markdown when I use simple compositions (often so) while looking up the pandoc options.

In order to put a table generated by R in a Japanese document in R Markdown, create a file similar to the following and save it with the extension .Rmd.If you open it in RStudio, you should see the Knit button, so pressing it should generate a mixed PDF of Japanese and R output (http://ipafont.ipa.go.jp if the IPAex font is not installed).

is not installed.
---
title:sample
author —Name.
date: '`r format(Sys.time(), '%Y/%m/%d')`'
output:
  pdf_document:
    config_caption:yes
    keep_tex —no
    latex_engine —xelatex
    number_sections —Yes
    template:null
    toc —no
documentclass —bxjsarticle
classoption: 
  - xelatex
  - ja = standard
---

``{r,include=FALSE}
options(xtable.comment=FALSE)
```

# Table Output

``{r, echo=FALSE, results='asis'}
# write the code to generate the table here
# In this sample, the pressure data built into R.
xtable::xtable (pressure)
```


2022-09-30 13:59

Looking at the error message, do you feel that the processing system you are trying to use does not correspond to the contents of the file?
When I read the question, it seems that RStudio compiled it with XeLaTeX, so please change the documentclass from jsarticle to bxjsarticle.

Also, the original code

I need to correct the two points in , but the output of xtable will be properly shaped here, so I think it's a copy error when posting.

This is the output of the xtable.

%latex table generated in R 3.3.2 by xtable 1.8-2 package
% Fri Jul 07 00:45:59 2017
\begin {table} [ht]
\centering
\begin {tabular} {rrr}
  \hline
 &ans & p.ans\\ 
  \hline
US>hh\_under 18&659.00&21.20\\ 
  US>hh\_over 65&857.00&27.60\\ 
  US>hh\_others&1592.00&51.20\\ 
   \hline
\end {tabular}
\end {table}

If you keep the original code (using jsarticle, you will use platex, so you will use

from the command prompt (or terminal) instead of from RStudio.
platex hoge.tex
dvipdfmxhoge.dvi

Compile like this.


2022-09-30 13:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.