[R] Tables, knitr markdown

Duncan Murdoch murdoch.duncan at gmail.com
Tue Feb 23 02:51:41 CET 2016


On 22/02/2016 6:19 PM, Santosh wrote:
> Sorry.. I forgot to mention that I wanted it be published in MS Word,
> because it goes into a Report this is prepared using MS Word.

I can't help you with that.

>
> Hence,the above effort.. yes, it's a lot easier to send it to Latex..
>
> I was also wondering if it is possible to add "\hline" separating the
> categories in a table..
>
> Using tabular, I get this:
>
> \begin{tabular}{lcccc}
> \hline
> "Name"  & "Value1" & \multicolumn{1}{c}{"Value2"} \\
> \hline
> \nopagebreak A1  &   0.06 &   1.2 \\
> \nopagebreak A5  &   0.62 &   8.9 \\
> \nopagebreak A6  &   0.48 &   4.2 \\
> \rule{0pt}{1.7\normalbaselineskip}A2  &   1.50 &   1.27 \\
> \nopagebreak A7  &   0.11 &   4.3 \\
> \nopagebreak A3  &   0.01 &   3.1 \\
> \rule{0pt}{1.7\normalbaselineskip}A4  &   2.19 &   1.0 \\
> \nopagebreak B1.  &  0.03 &   2.0 \\
> \nopagebreak B2.  &  0.011 &  1.8 \\
> \rule{0pt}{1.7\normalbaselineskip}B3  &  0.10 &  2.7 \\
> \nopagebreak B4.  &  0.02 &   1.6 \\
> \nopagebreak C1.  &  0.01 &   1.1 \\
> \hline
> \end{tabular}
>
> But, I want in this way.. (with horizontal lines and customized text
> inserted at the beginning of a group..
>

If you wanted LaTeX output, you could do this like the example at the 
end of section 2.1.5 in the vignette.  (You might want to combine that 
with subsetting as in section 3.3.)

Duncan Murdoch

> \begin{tabular}{lcccc}
> \hline
> "Name"  & "Value1" & \multicolumn{1}{c}{"Value2"} \\
> \hline
> \multicolumn{5}{l}{\textbf{Hardened}}\\
> \hline
> \nopagebreak A1  &   0.06 &   1.2 \\
> \nopagebreak \tA5  &   0.62 &   8.9 \\
> \nopagebreak \tA6  &   0.48 &   4.2 \\
> \rule{0pt}{1.7\normalbaselineskip}A2  &   1.50 &   1.27 \\
> \nopagebreak \tA7  &   0.11 &   4.3 \\
> \nopagebreak \tA3  &   0.01 &   3.1 \\
> \rule{0pt}{1.7\normalbaselineskip}A4  &   2.19 &   1.0 \\
> \hline
> \multicolumn{3}{l}{\textbf{Pulverized}}\\
> \hline
> \nopagebreak B1.  &  0.03 &   2.0 \\
> \nopagebreak B2.  &  0.011 &  1.8 \\
> \rule{0pt}{1.7\normalbaselineskip}B3  &  0.10 &  2.7 \\
> \nopagebreak B4.  &  0.02 &   1.6 \\
> \hline
> \multicolumn{3}{l}{\textbf{Molten}}\\
> \hline
> \nopagebreak C1.  &  0.01 &   1.1 \\
> \hline
> \end{tabular}
>
> Thanks so much for your help!
> Santosh
>
> On Mon, Feb 22, 2016 at 1:55 PM, Duncan Murdoch <murdoch.duncan at gmail.com>
> wrote:
>
>> On 22/02/2016 3:46 PM, Santosh wrote:
>>
>>> Just figured out..
>>>
>>> as.data.frame(as.matrix(<tabular_object>),stringsAsFactors=F)
>>>
>>> could work! :)
>>>
>>
>> Why do you want to produce Markdown output?  the tables package (lowercase
>> t!) can produce output in either LaTeX or HTML.  Just tell knitr to leave
>> the output alone, e.g. for PDF output
>>
>> ```{r results="asis"}
>> require(tables)
>> tab <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
>>           (Sepal.Length + Sepal.Width)*(mean + sd), data=iris )
>> latex(tab)
>> ```
>>
>> or for HTML output
>>
>> ```{r results="asis"}
>> require(tables)
>> tab <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
>> (Sepal.Length + Sepal.Width)*(mean + sd), data=iris )
>> html(tab)
>> ```
>>
>>
>> Duncan Murdoch
>>
>>
>>>
>>> On Mon, Feb 22, 2016 at 12:17 PM, Santosh <santosh2005 at gmail.com> wrote:
>>>
>>> Dear Rxperts..
>>>> I am able to generate tables using Tables R package..
>>>> However, when I have been unsuccessful in using kable (from knitr
>>>> package)
>>>> to generate the table in R markdown script..
>>>>
>>>> It's because the output generated by "tabular" in Tables package is of
>>>> class "tabular". The kable function in knitr package accepts data.frame.
>>>>
>>>> Is there a way to convert the tabular class objects into data.frame
>>>>    objects?
>>>>
>>>> Or is there a way that kable can accept "tabular" class object?
>>>>
>>>>
>>>> Thanks so much..
>>>> Santosh
>>>>
>>>>
>>>          [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>
>



More information about the R-help mailing list