Found this package called knitrBootstrap Which is to allow for Bootstrap style web pages when reporting in Rmarkdown.
Note: I am using the klippy, kableExtra, and knitrBootstrap
My issue is that when rendered is does not center the whole report, it is stuck to one side. And also the Title of the Document doesn't get displayed? Any suggestions to give this HTML page a more "fuller" feel? Because I can insert straight HTML code in Rmarkdown I added the HTML tag
---
output:
knitrBootstrap::bootstrap_document:
title: "Test file"
theme: united
highlight: sunburst
---
```{r}
library(kableExtra)
library(klippy)
library(knitrBootstrap)
```
```{r echo=FALSE, include=TRUE, out.width="100%"}
mpg_list <- split(mtcars$mpg, mtcars$cyl)
disp_list <- split(mtcars$disp, mtcars$cyl)
inline_plot <- data.frame(cyl = c(4, 6, 8), mpg_box = "", mpg_hist = "",
mpg_line1 = "", mpg_line2 = "",
mpg_points1 = "", mpg_points2 = "", mpg_poly = "")
inline_plot %>%
kbl(booktabs = TRUE) %>%
kable_paper(full_width = FALSE) %>%
column_spec(2, image = spec_boxplot(mpg_list)) %>%
column_spec(3, image = spec_hist(mpg_list)) %>%
column_spec(4, image = spec_plot(mpg_list, same_lim = TRUE)) %>%
column_spec(5, image = spec_plot(mpg_list, same_lim = FALSE)) %>%
column_spec(6, image = spec_plot(mpg_list, type = "p")) %>%
column_spec(7, image = spec_plot(mpg_list, disp_list, type = "p")) %>%
column_spec(8, image = spec_plot(mpg_list, polymin = 5))
```
I can't seem to find a ton of literature on the format you're using. However, I did notice that it doesn't change size when the screen size changes. It is all just set to one final size. That being said, the table thinks it is centered. In reality, it is formatted to 'fit' the contents, but the table is set to fill a space so that that outer space is centered in the body, but the table is left-aligned in that available space. On top of all that, the body is set to a max-width of 36em. That's why it looks left-aligned.
Clear as mud, I know. Sigh.
I can help make it better, but a different output format may be a better option. Almost any method I tried to make the table bigger destroyed the plots' SVG (distorted them).
This worked, but I don't know if the juice is worth the squeeze.
Add these styles between chunks and keep your code the same.
<style>
body {
max-width: 100%; // 36 em isn't working for me
}
table{
width: 924px !important;
height: auto;
}
tr {
height: 4em;
width: 924px !important; // 28 + (7*128) (for the 8 columns)
}
td {
vertical-align: middle !important;
padding-bottom: 0px !important;
}
svg {
width: 110%;
height: auto; // keep the aspect ratio
}
thead > tr *:not(:first-child) {
width: 128px; // only set here, if set to all td, it blows the svg
}
</style>
If you have any questions, let me know.
This centers, without centering, by filling the available space.
Related
I am trying to render any kind of R Shiny input in a Shiny DT, however I would like to avoid the linebreaking. If I concatenate some text and the html tags with the shinyInput function both the text and the inputs are rendered, but a linebreak happens before and after the input.
I think that the root cause for this is the div tag, but googling it seems that adding the style="display:inline;" css code should solve it, but it doesn't, it even breaks the width definition.
do you have any idea how to get the text before, the div, and the text after on the same cell?
below some code to play with.
library(DT)
ui <- basicPage(
h2("The mtcars data"),
DT::dataTableOutput("mytable")
)
server <- function(input, output) {
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
inputs
}
mtcarsx <- data.frame(mtcars, newvar=
paste0(
"tex before "
,shinyInput(checkboxInput,nrow(mtcars),"mychbx",label="",value=FALSE,width=NULL),
" text after"))
output$mytable = DT::renderDataTable({
DT::datatable(mtcarsx,
escape = FALSE,
selection = 'none',
rownames = FALSE,
extensions = 'RowGroup',
options = list(searching = FALSE,
ordering = FALSE,
rowGroup = list(dataSrc=c(1)),
columnDefs = list(list(visible=FALSE, targets=c(1)))
))
})
}
shinyApp(ui, server)
You are right, you need to change the divs that wrap the checkbox element to display:inline. You say that this doesn't solve it as it breaks the width definition. Perhaps I'm missing something? I do not see a change in the widths column.
tags$style("
#mytable tr td div.form-group {
display: inline;
}
#mytable tr td div.checkbox {
display: inline;
}
#mytable tr td div.checkbox label {
padding: 0;
}
#mytable tr td div.checkbox input {
position: relative;
margin: 0;
}")
If the text that goes before and after are constant, you could use the css pseudo classes after and before to add that content.
I want the contents of my cells to be displayed in a single line. I'm using Rmarkdown to HTML.
But no matter which package I use (Kable, Flextable, Huxtable), the column width specification is ignored and a line break is introduced, which makes the very ugly and unreadable results.
In HTML, with a drop-down box, the total width shouldn't be a problem. I just want the results to be readable.
library(kableExtra)
library(flextable)
table = as.data.frame(matrix(rep("value [value1 - value2]",20), ncol = 10))
kbl(table) %>%
kable_paper() %>%column_spec(1:ncol(table), width = "3.5cm", bold = TRUE, italic = TRUE)%>%
scroll_box(width = "1000px", height = "500px")
tb = flextable(table)%>% flextable::width(width = 10)
knit_print(tb)
With flextable, this code forces (note the usage of autofit()) the display on one single line:
library(flextable)
as.data.frame(matrix(rep("value [value1 - value2]", 20), ncol = 10)) %>%
flextable()%>% theme_box() %>% autofit()
This will produce a table display in an HTML window, this window has a width that is limited (the size of your window or the max-width of your HTML page). If the width of the browser window is less than the width of the table, it will be compressed to fit the window or the available space.
If you need to make this flextable horizontally scrollable (it is already implemented for bookdown but not yet for all HTML format), you can add this CSS code to your r markdown so that flextables can be scrollable (soon integrated into flextable then soon not necessary):
```{css echo=FALSE}
.flextable-shadow-host{
overflow: scroll;
white-space: nowrap;
}
```
An HTML 'R Markdown' document with it:
---
output: html_document
---
```{css echo=FALSE}
.flextable-shadow-host{
overflow: scroll;
white-space: nowrap;
}
```
```{r}
library(flextable)
as.data.frame(matrix(rep("value [value1 - value2]", 20), ncol = 10)) |>
flextable()|> theme_box() |> autofit()
```
Here is the huxtable equivalent:
as_hux(table) |>
set_width(1) |>
set_wrap(FALSE) |>
set_col_width("3.5cm") |>
quick_html()
which makes the table as wide as you want:
I build a leaflet in R like this:
my_leaflet <- my_data %>%
leaflet (options = leafletOptions(
-
) %>^%
addProvidersTiles(
-
)
setView(
-
) %>%
addMarkers (
--
) %>%
addCircleMarkers(
-
) %>%
addLegend( .. )
I get "height: 100%" in this part of the generated html code:
without browseable()
But when i use browsable() :
browsable(
tagList(list(
tags$head(
tags$style(
".leaflet .legend i{
border-radius: 50%;
width:12px;
height: 12px;
margin-top: 4px;
}",
),
my_leaflet
))
))
then i get "height: 400px" inside the generated html code (see the picture below) and the leaflet map only shows the half of my screen.
with browsable()
Is there a way to get the html code to show a full screen height? i am not sure if the "height: 400px" is the cause of this. is there a way to get this height to 100% when using browsable() ?
Try vh unit. In your case modify my_leaflet with leaflet(height = '100vh', ...).
I am generating a gitbook report with Rstudio the bookdown package.
It is fairly simple in terms of underlying R code, just a few recent tweaks for:
reducing text to 80% of page width
using double columns and
adding line number in the R code displayed.
Everything works well, except when I added "split_by: rmd" in the _output.yml. When doing so the resulting output doesn't respect the margin around the text anymore.
I don't know much about html yet, but looking at the html inspector revealed that the sections are located outside the inner-page formatting when using "split_by: rmd"
Default (no split_by argument):
With split_by: rmd
This is a shot in the dark as I cannot share the code and I am not able to reproduce the error with the minimal bookdown example from Yihui: https://github.com/rstudio/bookdown-demo.
Any leads to identify the origin of the error or even better propose a solution would be very welcomed!
Building the book from a R script:
bookdown::render_book(
input = "index.Rmd",
output_format = "bookdown::gitbook",
output_dir = paste0("gitbook-", format(Sys.time(), format = "%Y-%m-%d-%H%M%S"))
)
index.rmd YAML header:
---
title: "blahblah"
subtitle: "blahblahblah"
author: "DRAFT"
date: "August 2020"
documentclass: article
fontsize: 12pt
geometry: margin=2cm
link-citations: yes
#mainfont: Arial
bibliography: packages.bib
site: bookdown::bookdown_site
biblio-style: apalike
urlcolor: blue
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE, attr.source='.numberLines')
table_format <- knitr::opts_knit$get('rmarkdown.pandoc.to')
if (table_format %in% c("html", "latex")) {
library(kableExtra)
knitr::opts_chunk$set(fig.pos='H', fig.align='center', out.width='80%')
}
## Automatically create a bib database for R packages
knitr::write_bib(c(.packages(), 'bookdown', 'knitr', 'rmarkdown', 'Hmisc'), 'packages.bib')
```
_output.yml:
bookdown::gitbook:
css: style.css
config:
toc:
before: |
<li>My book title</li>
#after: |
# <li>Published with bookdown</li>
edit: null
download: null
sharing: null
info: null
split_bib: FALSE
split_by: rmd
style.css:
p.caption {
color: #777;
margin-top: 10px;
}
p code {
white-space: inherit;
}
pre {
word-break: normal;
word-wrap: normal;
}
pre code {
white-space: inherit;
}
/* watermark for draft report
.watermark {
opacity: 0.2;
position: fixed;
top: 45%;
left: 45%;
font-size: 500%;
color: #606099;
z-index: 1000000;
}
*/
.book .book-body .page-wrapper .page-inner {
max-width: 80% !important;
}
/* Increase space to display line number in R chunks correctly */
pre.numberSource code > span > a:first-child::before {
left: -0.3em;
}
/* for multi cols */
/*.cols {display: flex; } /* uncomment for flex column size */
.cols {display: grid; grid-template-columns: 30% 50% 20%;} /* for fixed column size */
I found that the origin of the problem was the \newpage I added at the beginning of each rmd file for building the report in pdf format.
Solution 1: was to change split_by: rmd to split_by: section as my rmd files correspond mostly to level 2 sections.
Solution 2: was to put a wrapper around \newpage so that they are not evaluated when the output is html:
`r if (knitr::opts_knit$get('rmarkdown.pandoc.to') != "html") '
\\newpage
'`
Other approaches to solution 2 have been described here:
How to add \newpage in Rmarkdown in a smart way?
I'm trying to scale down a plotOutput with Shiny R.
I have this plot:
from this code:
#In ui:
fluidRow(
column(width = 12,
h4("Diagrama Persistencia"),
plotOutput("Diagrama")
)
)
#In server
output$Diagrama <- renderPlot({
F_PlotDiag(Diag = isolate(values$data), tipoPlot = input$radioPlot, tamEjeNom = input$sliderTamEjeNom)
}, height = input$sliderHeight, width = input$sliderWidth)
Notice the height and width params. This works because all is in an observeEvent context.
As you can see, the hole plot won't fit in the screen. The problem with reducing height and width is that it looks like this:
But actually, if I right click and save the FIRST image, it looks fine unlike the second image:
Is there a way to show the whole plot in the browser by scaling it down? So that I can see it as if I downloaded the image.
I really don't know much about CSS so I can't really provide any logical attempts, but this is what I've tried for my HTML:
tags$style(type="text/css", ".shiny-bound-output { transform: 'scale(.5)' }")
tags$style(type="text/css", ".shiny-plot-output { transform: 'scale(.5)' }")
tags$style(type="text/css", "#Diagrama { height: 20px }")
With no success.
Since you didn't provide a reproducible example, see if this example helps you. Based on https://stackoverflow.com/a/8839678/4190526
The key is the following line, which finds the image under the div with the id distPlot (i.e. the plot name in ui.R), and define the CSS with a max-height but otherwise auto.
tags$style(HTML("div#distPlot img {width: auto; height: auto; max-width: auto; max-height: 400px;}")),
Full code
library(shiny)
ui <- shinyUI(fluidPage(
tags$style(HTML("div#distPlot img {width: auto; height: auto; max-width: auto; max-height: 400px;}")),
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput("distPlot")
)
)
))
server <- shinyServer(function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
}, width=600, height=1200)
})
shinyApp(ui = ui, server = server)