Tables in R Markdown

Discuss the jamovi platform, possible improvements, etc.

by Whirly123 » Tue May 12, 2020 1:46 pm

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

by jonathon » Wed May 13, 2020 11:14 am

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: 1261
Joined: Fri Jan 27, 2017 10:04 am

by jonathon » Wed May 13, 2020 11:24 am

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

by sbalci » Wed May 13, 2020 12:49 pm

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>")
```
sbalci
 
Posts: 16
Joined: Sat Jan 06, 2018 10:25 pm

by mcfanda@gmail.com » Wed May 13, 2020 4:53 pm

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

by Whirly123 » Thu May 14, 2020 1:04 am

I originally just had:
{r echo=FALSE, message=FALSE, comment=NA}

it looks like this:
1.jpg
1.jpg (110.25 KiB) Viewed 188 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: 18
Joined: Mon May 06, 2019 3:07 pm

by Whirly123 » Thu May 14, 2020 1:29 am

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

by jonathon » Thu May 14, 2020 4:54 am

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

by Whirly123 » Thu May 14, 2020 12:15 pm

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/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


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

by jonathon » Thu May 14, 2020 12:21 pm

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

Next

Return to General