Page 1 of 2

Tables in R Markdown

Posted: Tue May 12, 2020 1:46 pm
by Whirly123
I am using the Jamovi module in R to do an analysis. I can get a nice descriptive table that looks great if I print it in the R Console.

I am also producing an R Markdown document and would like to reproduce the lovely looking table for the Markdown doc. Is there any way to do this (without going into Jamvi itself and eg. exporting as HTML, as I wish to stay in R)

So far nothing I have tried works ie. printing, using kable, and doing both after it's been converted to a data frame.

Re: Tables in R Markdown

Posted: Wed May 13, 2020 11:14 am
by jonathon
hi whirly,

sorry, not at the moment. it's something i've always wanted to add, but haven't got around to it.

i'll get to it at some point.

cheers

Re: Tables in R Markdown

Posted: Wed May 13, 2020 11:24 am
by jonathon
wait, i'm hearing that wrapping the output in <pre> tags leads to a pretty good result. i'll let someone else chime in.

cheers

Re: Tables in R Markdown

Posted: Wed May 13, 2020 12:49 pm
by sbalci
Hi,

I learned following code from Marcello Gallucci.



```{css}
pre.jamovitable{
color:black;
background-color: white;
margin-bottom: 35px;
}
```

```{r}
jtable<-function(jobject,digits=3) {
snames<-sapply(jobject$columns,function(a) a$title)
asDF<-jobject$asDF
tnames<-unlist(lapply(names(asDF) ,function(n) snames[[n]]))
names(asDF)<-tnames
kableExtra::kable(asDF,"html",
table.attr='class="jmv-results-table-table"',
row.names = F,
digits=3)
}
```




```{r}
mytable <- jmv::anovaOneW(
formula = Sepal.Length ~ Species,
data = iris
)
```


```{r, results='asis'}
cat("<pre class='jamovitable'>")
print(jtable(mytable$anova))
cat("</pre>")
```

Re: Tables in R Markdown

Posted: Wed May 13, 2020 4:53 pm
by mcfanda@gmail.com
Hi, sbalci code works and it is intended for more elaborated things. If you need jamovi commands output in a neat way (without changing their original look), you can simply prepend the "<pre> tag. Here are two examples:
1. a command that returns only one table

Code: Select all

```{r out, echo=FALSE,results="asis"}
ttest<-jmv::ttestIS(extra,group,data = sleep)

  cat("<pre>")
  print(ttest)
  cat("</pre>")
 ```
2. a command that returns more tables

Code: Select all



```{r out, echo=FALSE,results="asis"}
 
atest<-jmv::anovaOneW(deps = extra,group = group,data = sleep,desc = T)


  cat("<pre >")
  print(atest$anova)
  cat("</pre>")

  cat("<pre>")
  print(atest$desc)
  cat("</pre>")

```

Because you may want to style a bit the output, you can add a css class to the <pre> tag (for instance <pre class="jamovitable">), and in the css of the Rmd add something like this

Code: Select all

```{css}
pre.jamovitable{
color:black;
background-color: white;
margin-bottom: 35px;
}
```
as sblaci was suggesting.

Re: Tables in R Markdown

Posted: Thu May 14, 2020 1:04 am
by Whirly123
I originally just had:
{r echo=FALSE, message=FALSE, comment=NA}

it looks like this:
1.jpg
1.jpg (110.25 KiB) Viewed 17355 times
If I add the code suggested here it looks identical in HTML only this time it won't render at all for a PDF export.

Honestly, it doesn't look too bad like that but it would be cool if there was a way to have those dashes be a line, as it looks in the console.

Am I doing anything wrong here?

Re: Tables in R Markdown

Posted: Thu May 14, 2020 1:29 am
by Whirly123
Additionally, if I wanted to export an image file of the table like I can in Jamovi, is there a way to do this from R?

Re: Tables in R Markdown

Posted: Thu May 14, 2020 4:54 am
by jonathon
Honestly, it doesn't look too bad like that but it would be cool if there was a way to have those dashes be a line, as it looks in the console.
i *think* this is just a matter of your font. i think those dashes are this character:

https://www.fileformat.info/info/unicode/char/2500/index.htm

which *should* render as a continuous line.

i'm not aware of a way to convert a table to an image in R :/

jonathon

Re: Tables in R Markdown

Posted: Thu May 14, 2020 12:15 pm
by Whirly123
jonathon wrote:
Honestly, it doesn't look too bad like that but it would be cool if there was a way to have those dashes be a line, as it looks in the console.
i *think* this is just a matter of your font. i think those dashes are this character:

https://www.fileformat.info/info/unicod ... /index.htm

which *should* render as a continuous line.

i'm not aware of a way to convert a table to an image in R :/

jonathon
I don't think that's right. R seems to actually change the Unicode character. The font of the console window is Lucida Console. If I copy and paste the output from the console into word it looks great. If I change the font in R Markdown to Lucida console it works but changes the long dash into short dashes.

Re: Tables in R Markdown

Posted: Thu May 14, 2020 12:21 pm
by jonathon
oh yup. unicode in R under windows is pretty messed up (although, something suggested the situation may have improved under R 4, not sure).

could it be this?:

https://stackoverflow.com/questions/44153072/unicode-with-knitr-and-rmarkdown

jonathon