Using R Markdown, I would like to generate an html report where I could browse multiple plots in the same 'window', e.g. using a scrollbar with two arrow buttons to print the previous and next plot. I have found several questions asking how to scroll within a large figure or image, but could not identify a solution to scroll between several plots.
For example, knitting the Rmd code below would generate three graphs on the top of each other. Which code should I use to make them scrollable?
EDIT: I need a 'single page view' to easily compare two consecutive plots, i.e. I should be able to move the scrollbar in a discrete, not continuous, way.
---
title: "Test multiple plots"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Generating multiple plots
I would like these three plots to be at the same location, and to switch between them by pressing buttons or sliding a scroll bar in the html document.
```{r plots, echo = F}
plot(0, pch = 16, col = 1)
plot(0, pch = 16, col = 2)
plot(0, pch = 16, col = 3)
```
Thank you for your help.
You can use the following code:
---
title: "Test multiple plots"
output: html_document
---
<style>
.vscroll-plot {
width: 1000px;
height: 400px;
overflow-y: scroll;
overflow-x: hidden;
}
</style>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(width=200)
```
## Generating multiple plots
I would like these three plots to be at the same location, and to switch between them by pressing buttons or sliding a scroll bar in the html document.
<div class="vscroll-plot">
```{r plots, echo = F}
plot(0, pch = 16, col = 1)
plot(0, pch = 16, col = 2)
plot(0, pch = 16, col = 3)
```
</div>
Output:
You will see that there is a vertical scrollbar. You can change the height and width in the style to make the view bigger or smaller.
Related
Using a loop in R, I generated 100 random datasets and made a plot for each of these 100 datasets:
library(ggplot2)
results = list()
for (i in 1:100)
{
my_data_i = data.frame(var_1 = rnorm(100,10,10), var_2 = rnorm(100,10,10))
plot_i = ggplot(my_data_i, aes(x=var_1, y=var_2)) + geom_point() + ggtitle(paste0("graph", i))
results[[i]] = plot_i
}
list2env(setNames(results,paste0("plot",seq(results))),envir = .GlobalEnv)
What I am now trying to do, is make a Rmarkdown/flexdashboard that can be saved as an HTML file, and:
Contains all these plots
Allows the user to search for these plots (e.g. type in "plot76")
In a previous question (How to create a dropdown menu in flexdashboard?) I learned how to do something similar.
But I am still trying to figure out how I can get something like this to work. I would like there to be a single page with a search bar, and you can type in which graph you want to see.
Can someone please help me out with this? Are there any online tutorials that someone could recommend?
Thank you!
This is a close solution depending on how much you need the type feature. First in the YAML specify toc and theme as below. This will create a table of contents and allow the users to click each anchor in the list and it will bring them to the appropriate figure. Second use knit_expand() and knit() to dynamically create html blocks in your code. I did 5 plots here but it should scale to 100.
---
title: "plots"
author: "Michael"
output:
html_document:
toc: true
theme: united
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(glue)
library(knitr)
```
```{r,echo=FALSE, results = 'asis'}
my_data_i = data.frame(var_1 = rnorm(100,10,10), var_2 = rnorm(100,10,10))
out = NULL
out2 = NULL
plot_i = NULL
for (i in 1:5)
{
cat('\n## Plot = ', i, '\n')
plot_i = ggplot(my_data_i, aes(x=var_1, y=var_2)) + geom_point() + ggtitle(paste0("graph", i))
out2 = c(out2, knit_expand(text = "{{print(plot_i)}}" ))
cat('\n')
}
```
`r knit(text = out2)`
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 am creating tables that will be rendered using Rmarkdown to HTML. I am using kable and have been experimenting with kableExtra to add features to my tables. I am not able to get the width option in column_spec to work when applying it to all columns in a table:
data.frame(RRmin=1, RRmax=10) %>%
dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
kable() %>%
column_spec(1:2, width = "0.5in") %>%
kable_styling(c("bordered", "condensed"), full_width = F)
This gives a table that looks like this.
I can make the width longer and both columns change, but when it goes smaller it does not seem to work. I can make one column smaller but not the other:
data.frame(RRmin=1, RRmax=10) %>%
dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
kable() %>%
column_spec(1, width = "0.5in") %>%
kable_styling(c("bordered", "condensed"), full_width = F)
This gives a table that looks like this. The first column was appropriately changed but I cannot get this effect when I'm trying to change the size of both columns. I have tried doing separate column_spec lines for each column, using escape=F and am not sure what to try next.
I have had similar problems with column_spec not working. I was able to find a fix that worked for my purposes by playing with the width_min option. Maybe that will help.
My issue was that none of the columns widths seemed to be adjusted by column_spec, even when I tried all of the options you mention above. The result was that some columns were way too thin. I set width_min="3in" and fixed it. This was not a perfect fix because now I'm left with other column that are too wide, but it at least made my table a little more readable.
This may be a little late, but I've just been working with the kableExtra package, and it seems that your code is now working pretty much as is.
At first I thought it might have something to do with the ordering of the kable_styling component, but it seems not to matter which order it is in. Perhaps it was a bug in the package that has since been fixed. It is also immaterial wether you use column_spec(column = 1:2, width = "2in"), or column_spec(1:2, width = "2in"). Both seem to work well, as do modifications to the columns size. See below:
---
output: pdf_document
---
```{r global_options, include=FALSE}
# Just some setup:
sapply(c("knitr", "tidyverse", "kableExtra"), require, character.only = TRUE)
options(knitr.kable.NA = '', knitr.table.format = "latex")
knitr::opts_chunk$set(fig.path = 'figures/',
echo = FALSE, warning = FALSE, message = FALSE)
opts_chunk$set(echo = FALSE,
message = FALSE,
warning = FALSE,
fig.align = "center",
fig.width = 5,
fig.pos = 'H',
as.is = TRUE)
```
```{r variable-names-table, as.is=TRUE}
# Size example 1; 1.5 inch columns
data.frame(RRmin=1, RRmax=10) %>%
dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
kable() %>%
kable_styling(c("bordered", "condensed"), full_width = F) %>%
column_spec(column = 1:2, width = "1.5in")
# Size example 2; 3 inches
data.frame(RRmin=1, RRmax=10) %>%
dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
kable() %>%
column_spec(column = 1:2, width = "3in") %>%
kable_styling(c("bordered", "condensed"), full_width = F)
# To set columns 1 and two to different sizes
data.frame(RRmin=1, RRmax=10) %>%
dplyr::rename(`Reportable Range Min` = RRmin, `Reportable Range Max` = RRmax) %>%
kable() %>%
column_spec(column = 1, width = "3in") %>%
column_spec(column = 2, width = "2in") %>%
kable_styling(c("bordered", "condensed"), full_width = F)
```
Just a note for anyone else dealing with the issue. The above will run as an RMD
R version 3.6.1, on mac
RStudio 1.2.1335
kableExtra 1.1.0
knitr 1.25
tidyverse 1.2.1
Simply replace width by width_min!
I'm using Tex Live 2020, and this problem still exists - it appears that column_spec has a bug. All thse examples run without a probllem if I remove the column_spec commands. As soon as I include the column_spec commands, I get a cryptic error which says 'Undefined control sequence, Latex Error: Illegal character in array arg. The description is also cryptic: ...n}|>{\raggedleft\arraybackslash}p{1.5in}}
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., \hobx), type I and the correct spelling (e.g., I\hbox). Otherwise just continue, and I'll forget about whatever was undefined. Removing the column_spec command fixes the problem.
The fix is to include the array package in the latex preamble. One way of doing this is adding the following lines to your Rmarkdown header:
header-includes:
- \usepackage{array}
I am trying to create an HTML output using R - markdown. The problem is that whenever I try to output any table, the formatting is very sparse. Ideally the table should be easily fitted in one page width but since the formatting is too sparse, one needs to scroll to the right to see the output.
Here is the code and output.
```{r}
df = data.frame(
first.var = 1:10,
second.var = letters[1:10],
third.var = LETTERS[1:10],
fourth.var = paste0(letters[1:10],"-",LETTERS[1:10]),
fifth.var = "this will not go out of screen",
sixth.var = "but this will go out of the screen"
)
df
```
How can I fit more columns in one page width, so that I don't have to scroll.
Try setting the style (CSS) for the table. I wrapped the table into DT::datatable:
<div style="width = 100%">
```{r}
df = data.frame(
first.var = 1:10,
second.var = letters[1:10],
third.var = LETTERS[1:10],
fourth.var = paste0(letters[1:10],"-",LETTERS[1:10]),
fifth.var = "this will not go out of screen",
sixth.var = "but this will go out of the screen"
)
DT::datatable(df)
```
</div>
I am trying to generate a shareable HTML document generated from an R Script in RStudio.
The script makes use of interactive plots generated from networkD3 and collapsibleTree packages. In the RStudio viewer, the colour scheme for these plots is highly visible; colours such as blue and red for the items.
However, when rendered in HTML, the colour scheme becomes a washed out grey: practically white on white background, which makes it too hard to see or use.
Can I specify plot colours in the RScript using a knitr passthrough, I don't know, something like:
#+ colourscheme(RdBu)
or do I need to generate some kind of CSS file to control plot colours? I am unclear and not very knowledgeable in this HTML area, and a little confused why the colours would change at all!
Thanks in advance for any help.
-- edit (example provided)
In response to the request below, I've generated a tiny example. However (!) when this is rendered, it retains the correct colour scheme. I'm unclear now what it is causing this; colours are linked to "gp" in my main diagram, and I have only 3 groups so should see 3 colours. I'm not able to provide a full example due to size (data limitations), so here's the outline:
nodes <- data.frame(Name = c('Alpha', 'Beta', 'Charlie'),
ID = c(0,1,2),
gp = c(1,1,2),
n = c(10,15,20))
links <- data.frame(x = c(0, 0, 0, 1, 1, 2, 2),
y = c(0, 1, 2, 1, 2, 0, 2),
n = c(8, 9, 8, 9, 8, 9, 8))
require(networkD3)
require(RColorBrewer)
forceNetwork(height = 200, width = 400,
Links = links, Nodes = nodes,
Source = "x", Target = "y", Value = "n", # From Links df
NodeID = "Name", Group = "gp", Nodesize = "n", # From Nodes df
arrows = T,
linkWidth = JS("function(d) { return Math.sqrt(d.value); }"),
#linkWidth = JS(" d.value"),
radiusCalculation = JS(" d.nodesize"),
charge = -10,
fontSize = 16,
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10);"),
opacity = 0.9,
bounded = T)
I'm guessing (?) that there's a certain set of conditions that triggers the colours to fail.
I'm pretty sure this happens because collapsibleTree is adding CSS that affects the elements created by forceNetwork. Can you try putting this minimal example in a .Rmd file and knit it to see if shows a similar problem...
---
output: html_document
---
```{r echo=FALSE}
nodes <- data.frame(NodeID = c("Alpha", "Beta", "Charlie"),
Group = c(1, 2, 3),
Nodesize = c(10, 15, 20))
links <- data.frame(Source = c(0, 0, 1, 2),
Target = c(1, 2, 2, 0),
Value = c(9, 8, 8, 9))
library(networkD3)
forceNetwork(Links = links, Nodes = nodes,
Source = "Source", Target = "Target", Value = "Value",
NodeID = "NodeID", Group = "Group", Nodesize = "Nodesize",
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10);"),
width = 100, height = 100)
```
```{r echo=FALSE}
library(collapsibleTree)
collapsibleTree(warpbreaks, c("wool", "tension", "breaks"),
width = 100, height = 100)
```
if so, try installing the dev version of collapsibleTree with devtools::install_github('AdeelK93/collapsibleTree') and then try it again and see if the problem goes away (and your other problem). They added namespaced css in this commit which hasn't made it into a CRAN release yet.