R - kableExtra create column with link - r

I am creating a table with a column with hyperlinks, but those hyperlinks are very long and I wanted to substitute the long text for an image, to click it and to open the link in a new tab.
For example, with this code
df = iris[c(1,51,101),]
df$hyperlink = c("https://en.wikipedia.org/wiki/Iris_setosa", "https://en.wikipedia.org/wiki/Iris_versicolor", "https://en.wikipedia.org/wiki/Iris_virginica")
kable(df,format = "html")%>%
kable_styling(bootstrap_options = c("hover", "condensed"), full_width = F)
I obtain the last column as a hyperlinks, but what I would like is to put an image that, when clicked, it opens the url (preferably in a new window or tab)

You add clickable images by adding the appropriate html tags. <a href='...'></a> is for hyperlinks, and <img src='...'> is for images. Simply place the image tag between the opening and closing html tags. Also, be sure to include escape=FALSE in the kable statement to make it work.
library(kableExtra)
library(dplyr)
df = iris[c(1,51,101),]
df$hyperlink = c("<a href='https://en.wikipedia.org/wiki/Iris_setosa'><img src='setosa.png' /</a>",
"<a href='https://en.wikipedia.org/wiki/Iris_versicolor'><img src='versicolor.png' /></a>",
"<a href='https://en.wikipedia.org/wiki/Iris_virginica'><img src='virginica.png' /></a>")
kable(df,escape=FALSE,format = "html")%>%
kable_styling(bootstrap_options = c("hover", "condensed"), full_width = F)

Related

Compact table in html document rmarkdown with kableExtra?

I'm trying to make a 'compact' table in an RMarkdown
I've tried a few things, mostly variations on setting a custom css class and providing the custom css class to a code chunk
I've tried a lot of variations, all of which I can see flow through to the source code (accessed via knitting the html document, opening in chrome, and cmd + opt + u to view source and inspecting the source)
However, I can't work out what's necessary to simply make rows thinner (I believe that's simply reducing cell padding) in a kableExtra table
What I've tried so far
Here's one variation of what I've tried, but the rows are not compact as hoped (they are the standard height)
Which is done with:
---
output: html_document
---
```{r setup, include=FALSE}
library(dplyr); library(kableExtra)
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(dplyr)
library(kableExtra)
```
<style>
pre code, pre, code {
padding: 200 !important;
}
</style>
```{r}
iris %>%
kable %>%
kable_styling("striped", full_width = F) %>%
column_spec(4:5, bold = T) %>%
row_spec(3:5, bold = T, color = "white", background = "#D7261E")
```
but note that the custom css is not taking effect
The easiest way is to override the Bootstrap CSS, decreasing value of padding property (default value is 8px):
<style>
.table>tbody>tr>td{
padding: 1px;
}
</style>
As you pointed out, inspecting the source will lead you to the values above:
You could also do something similar within row_spec(1:nrow(iris), extra_css = "..")
To make the kable rows smaller in a knit to HTML, you can use bootstrap_options = c("condensed") in your kable_styling:
kable_styling(bootstrap_options = c("condensed"))
See https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html
If anyone knows the pdf variant for it, please let me know :)

Fix header on top with kable or DT

I'm trying to include a vertically scrollable table in my html output. The table looks stunning, but I would like to fix the header on top (while right now it scrolls up like everything else). A plus would be being able to filter the columns as well, or interact with them (e.g. sorting).
I have tried using both kable() + kableExtra() and DT(). My understanding is it would be easier with DT, but I find the documentation very confusing.
I'm doing something like this:
library(datasets)
library(knitr)
library(kableExtra)
library(DT)
data(iris)
# with kable
kable(iris, "html") %>%
kable_styling(bootstrap_options = c("striped", "hover", "responsive"),
full_width = F) %>%
scroll_box(width = "100%", height = "400px")
# with DT
datatable(showData, options = list("scrollY"))
I can't really find a way to fix the header on top or add filters. Any tips?
Thanks!
You can combine custom CSS with kable to create a scrollable table body with fixed header.
For example, create file styles.css in project directory with the following content:
.table-fixed-header tbody{
display:block;
overflow:auto;
height:400px;
width:100%;
}
.table-fixed-header thead tr{
display:table-header-group;
height:100%;
}
In YAML of .Rmd document, refer to styles.css:
output:
html_document:
css: styles.css
In R code chunk, pass style name to the kable() function
kable(iris, escape = F, table.attr='class="table-fixed-header"') %>%
kable_styling()
You could've also added
fixed_thead = T
in the scroll box.

How to change appearance of table header row with R formattable package

I'm using the formattable package in R to produce a HTML table. I can use formatter to customise the look of data values in in my table e.g. font-size, color etc. But I can't work out how to alter the appearance of the table header row.I can alter the actual column names using col.names(), but haven't been able to change their appearance.
For example, in the table below how can I change the text color or background color in the header row (mpg, cyl, disp etc.)
Ultimately, I plan to use formattable::as.htmlwidget() and library(webshot) to grab an image file of the table, see
Command for exporting/saving table made with Formattable package in R
Thanks
library(formattable)
formatRed <- formatter("span"
, style = x ~ style(color = ifelse(x > 21 , "red", "black")))
formatSize <- formatter("span"
, style = x ~ style("font-size" = "8px"))
exTb <- formattable(head(mtcars, 5)
, table.attr = "class='table table-striped'"
, list(mpg = formatRed
, wt = formatSize)
)
exTb
You can use a style sheet. You can either embed your style sheet in your .Rmd file, or, you can save your style sheet as a .css file, and then reference it from the .Rmd file. If you want more information about embedding style sheets into your .Rmd file, see this question. If you want more information about referencing an external style sheet, see Section 3.1.4.1. In my example, I'm embedding the style sheet (the <style>...</style> component) in my .Rmd file. My style sheet defines styles to change table headings' fonts to Times New Roman, and table headings' font colours to red.
---
title: "Test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<style>
thead {
font-family: "Times New Roman";
color: red;
}
</style>
```{r, echo=FALSE}
library(formattable)
df <- data.frame(Change = c(1), My = c(2), Style = c(3))
ft <- formattable(df)
ft
```
By extending your style sheet, you can impact other elements in the HTML file.

Change line spacing in footnote of docx using ReporteRs in R

In a word document, I want single spaced footnotes while the body of the document is 1.5 spaced.
No matter what I try, the footnote spacing stays at 1.15 spacing instead of single spacing.
I've tried changing the style of the footnote and changing the padding around each line of the footnote (treating each as a different paragraph). Styles don't seem to work on footnotes and changing the padding to zero still left the default 1.15 space between each "paragraph."
My current code:
footnote1 = Footnote()
footnote1 = addParagraph( footnote1,
value = pot("footnote text",
textProperties(font.size = 10, font.family = "Arial"))
)
doc <- addParagraph( doc,
value = "\tparagraph text" +
pot("addfootnotehere.", footnote = footnote1 ) +
"more text.",
stylename = "NormalLeft",
bookmark = "paragraph1"
)
Your problem may be fixed if you used your own created template instead of the default doc <- docx().
Set up a template with the spacing required, save it, then call the newly created template with:
doc <- docx(template = 'D:/docs/template/my_corporate_template.docx')
then addFootnote should read your template sizing without auto correcting.

cObject in Typolink assigned with border-content

Basically, I (think I ) need to know how to assign borderContent to a cObject, when it is a typolink parameter.
To tell the whole story: I'm using perfect lightbox, and I want it to open the lightbox when a text is clicked, and display the images that are in a single content element in the border section.
Looking through the manual, i found this code:
page.20 = TEXT
page.20.value = Open an image in a lightbox
page.20.typolink {
title = This is my caption
parameter.cObject = IMG_RESOURCE
parameter.cObject = fileadmin/image2.jpg
parameter.cObject.file.maxW = 600
parameter.cObject.file.maxH = 600
ATagParams = rel="lightbox[mySet]"
}
which is working fine. But I don't want the path to be hard set, but the content to be loaded from the border section, as I said. But if I try the following:
page.20 = TEXT
page.20.value = Open an image in a lightbox
page.20.typolink {
title = This is my caption
parameter.cObject = IMG_RESOURCE
parameter.cObject < styles.content.getBorder
parameter.cObject.file.maxW = 600
parameter.cObject.file.maxH = 600
ATagParams = rel="lightbox[mySet]"
}
the link is gone.
So I GUESS I'm assigning the content wrong. Somebody knows the answer?
Thanks!
(If of any help, I use automaketemplate..)
Assigning styles.content.getBorder will just assign the full content elements from the border column. This will not get you anywhere.
You will need to manually load the content elements from the border column, of course this can be done with TypoScript. It should be something like this:
page.20 = TEXT
page.20 {
value = Open an image in a lightbox
typolink {
ATagParams = rel="lightbox[mySet]"
title = This will be the title attribute
parameter.cObject = CONTENT
parameter.cObject {
table = tt_content
select {
pidInList = this
where = colPos = 3
}
renderObj = IMG_RESOURCE
renderObj.file {
import = uploads/pics
import.field = image
import.listNum = 0
width = 600
height = 600
}
}
}
}
Basically this will load all content elements on the border position from the current page. Render the first image in the list of images and return you the resource.

Resources