Tables in R Markdown
Tables in R Markdown
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.
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
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
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
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
cheers
Re: Tables in R Markdown
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>")
```
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>")
```
- mcfanda@gmail.com
- Posts: 467
- Joined: Thu Mar 23, 2017 9:24 pm
Re: Tables in R Markdown
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
2. a command that returns more tables
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
as sblaci was suggesting.
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>")
```
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>")
```
Code: Select all
```{css}
pre.jamovitable{
color:black;
background-color: white;
margin-bottom: 35px;
}
```
Re: Tables in R Markdown
I originally just had:
{r echo=FALSE, message=FALSE, comment=NA}
it looks like this: 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?
{r echo=FALSE, message=FALSE, comment=NA}
it looks like this: 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
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
i *think* this is just a matter of your font. i think those dashes are this character: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.
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
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.jonathon wrote:i *think* this is just a matter of your font. i think those dashes are this character: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.
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
Re: Tables in R Markdown
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
could it be this?:
https://stackoverflow.com/questions/44153072/unicode-with-knitr-and-rmarkdown
jonathon