Tables in R Markdown

Discuss the jamovi platform, possible improvements, etc.
Whirly123
Posts: 31
Joined: Mon May 06, 2019 3:07 pm

Tables in R Markdown

Post 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.
User avatar
jonathon
Posts: 2620
Joined: Fri Jan 27, 2017 10:04 am

Re: Tables in R Markdown

Post 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
User avatar
jonathon
Posts: 2620
Joined: Fri Jan 27, 2017 10:04 am

Re: Tables in R Markdown

Post 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
sbalci
Posts: 125
Joined: Sat Jan 06, 2018 10:25 pm
Contact:

Re: Tables in R Markdown

Post 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>")
```
User avatar
mcfanda@gmail.com
Posts: 460
Joined: Thu Mar 23, 2017 9:24 pm

Re: Tables in R Markdown

Post 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.
Whirly123
Posts: 31
Joined: Mon May 06, 2019 3:07 pm

Re: Tables in R Markdown

Post 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 6634 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?
Whirly123
Posts: 31
Joined: Mon May 06, 2019 3:07 pm

Re: Tables in R Markdown

Post 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?
User avatar
jonathon
Posts: 2620
Joined: Fri Jan 27, 2017 10:04 am

Re: Tables in R Markdown

Post 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
Whirly123
Posts: 31
Joined: Mon May 06, 2019 3:07 pm

Re: Tables in R Markdown

Post 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.
User avatar
jonathon
Posts: 2620
Joined: Fri Jan 27, 2017 10:04 am

Re: Tables in R Markdown

Post 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
Post Reply