On plotting in HTML5 slides with knitr - html

Here is my problematic slide:
The plain text for this slide is
# Title
```{r plot, echo=FALSE}
library("ggplot2")
x <- seq(-1,1,0.05)
y <- x^3/3-x^2 + rnorm(41,sd=0.1)
qplot(x,y)+geom_smooth()
```
- item 1
- item 2
And I want to:
Hide the comment text (## blahblah)
Hide the text "plot of chunk plot"
Make the plot smaller, so that the items can be shown
Could you please give me some hints? Thank you.

You should take time to read the wonderful documentation provided for knitr.
Without knowing your work flow to create the slides it is hard answer from the html5 end.
You are looking for fig.width, fig.height, fig.cap, warning and message from the knitr chunk options.
```{r plot, echo=FALSE,fig.width = 3, fig.height = 3, fig.cap = '', warning = FALSE, message = FALSE, cache = FALSE}
library("ggplot2")
x <- seq(-1,1,0.05)
y <- x^3/3-x^2 + rnorm(41,sd=0.1)
qplot(x,y)+geom_smooth()
```
- item 1
- item 2
You will have to adjust the height and width to get the size that is suitable for slides

Related

R: Converting "rmarkdown" to "html" files

I am using the R programming language. I am trying to recreate the interactive "dashboard" from this website : https://beta.rstudioconnect.com/jjallaire/htmlwidgets-rbokeh-iris/htmlwidgets-rbokeh-iris.html (code is provided on this website).
First, I ran this code to access the "flexdashboard template maker" :
library(flexdashboard)
rmarkdown::draft("dashboard.Rmd", template = "flex_dashboard", package = "flexdashboard")
Then, I deleted all the text in the window that popped up. I copied the R code from the website (https://beta.rstudioconnect.com/jjallaire/htmlwidgets-rbokeh-iris/htmlwidgets-rbokeh-iris.html) into this window and clicked "save":
---
title: "rbokeh iris dataset"
author: "Ryan Hafen"
output:
flexdashboard::flex_dashboard:
orientation: columns
social: menu
source_code: embed
---
```{r setup, include=FALSE}
library(rbokeh)
library(flexdashboard)
```
Column {data-width=600}
-----------------------------------------------------------------------
### Species
```{r}
figure(width = NULL, height = NULL) %>%
ly_points(Sepal.Length, Sepal.Width, data = iris, color = Species)
# figure() %>%
# ly_points(Sepal.Length, Sepal.Width, data = iris,
# color = Species, glyph = Species)
```
Column {data-width=400}
-----------------------------------------------------------------------
### Species (Quantile)
```{r}
figure(width = NULL, height = NULL, legend_location = "top_left") %>%
ly_quantile(Sepal.Length, group = Species, data = iris)
```
### Petal Width
```{r}
figure(width = NULL, height = NULL) %>%
ly_points(Sepal.Length, Sepal.Width, data = iris,
color = Petal.Width)
```
This file ("dashboard.Rmd") is saved in "my documents" (which has been also set to the default working directory):
Now, I want to "view" the dashboard and "save" the dashboard as an ".html" file. I found this other stackoverflow post that shows how to solve this problem: How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?
I tried to follow the steps in one of the answers provided on this stackoverflow post:
require(knitr) # required for knitting from rmd to md
require(markdown) # required for md to html
markdownToHTML('dashboard.Rmd', 'test.html')
But this produced the following output (incorrect):
Instead of the desired output:
Can someone please show me what I am doing wrong and how can I fix this (i.e. get the desired output) ?
Thanks
After you save the file in dashboard.Rmd, click on Knit -> Knit to flex_dasboard
This opens the dashboard template in RStudio itself. This also automatically creates a HTML file with the same name (dashboard.html) in your working directory.

Anyway to fill space on a chart area using flex dashboard? Two outputs per chart area?

I've been struggling with this for a while and pretty much I want see if there is a way I can have a combined output. I'm using flexdashboard to make a dashboard and I really want to have a tab feature where I can put various monthly datasets. Unfortunately when I use .tabset I can only have one chart area per column.
Here is where I am at with my dashboard.
The problem is all that empty space below. I can't add another chart area using ### because it will just add it to the tabset.
I was wondering if there was a way to make two objects output in the area (above and below) or maybe its possible to attach a graph below it?
Any insight would be greatly appreciated. I've already tried using tabBox and tabsetPabels but it doens;t work properly so I'm stuck with this format.
Desired output:
Code:
---
title: "Example Demo Dash"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
# Tab 1
```{r setup, include=FALSE}
library(readr)
library(DT)
library(dplyr)
library(rsconnect)
require(flexdashboard)
library(shiny)
library(shinydashboard)
require(plotly)
```
Column
-----------------------------------------------------------------------
### Tab 1 Graph
# Tab 2
Column {data-width=450, .tabset}
-----------------------------------------------------------------------
```{r}
cars <- datatable(head(mtcars,10),
rownames = FALSE,
options = list(
dom = 't',
paging = FALSE,
info = FALSE,
scrollY = FALSE,
sort = FALSE
)
)
iriss <- datatable(head(iris,10),
rownames = FALSE,
options = list(
dom = 't',
paging = FALSE,
info = FALSE,
scrollY = FALSE,
sort = FALSE
)
)
```
### Cars Tab 1
```{r}
cars
```
### Iris Tab 2
```{r}
iriss
```
Column {data-width=200}
-----------------------------------------------------------------------
### Tab 2 Col 2 Graph 1
### Tab 2 Col 2 Graph 1
### Tab 2 Col 2 Graph 3
### Tab 2 Col 2 Graph 3
Column {data-width=350}
-----------------------------------------------------------------------
### Col 3 graph 1
### Col 3 graph 2
# tab 3

R Markdown: is it possible to get all the code of an R chunk precede its output, instead than having an output for each line of code?

I have an R Markdown document, with an R chunk containing 2 lines of code. In the resulting HTML document, I would like all the code lines belonging to the same chunk (two in this example) to be printed in a single box, before their output.
---
title: "Just a test"
author: "Yours Truly"
date: '`r Sys.Date()`'
output:
html_document:
fig_caption: yes
---
```{r setup, include=FALSE}
library(ggplot2)
library(car)
library(knitr)
opts_chunk$set(cache = TRUE,
out.width = "75%",
fig.align = "center")
```
here is some code
```{r EDA}
summary(Davis)
ggplot(Davis, aes(x = height, y = weight)) +
geom_point()
```
Instead, what I get is that each line of code of the chunk is immediately followed by its output:
I want the two gray boxes containing summary(Davis) and ggplot(Davis, aes(x = height, y = weight)) + geom_point() to be joined together in a single gray box, followed by their output. Is this possible?

Rmarkdown: Placing static content under a tabbed section

I was hoping for help placing static content under a tabbed section in my R markdown file. It's similar to this question: RMarkdown: Tabbed and Untabbed headings, but the solution doesn't account for blank lines in the table of contents.
Is it possible to end the tabbed section without starting a new section? Here's an example:
---
title: "Mtcars Example"
output:
html_document:
toc: true
toc_float: true
number_sections: true
---
# Mtcars Info
The data was extracted from the 1974 Motor Trend US magazine..
# Dataset Prep
No changes were made to the dataset..
# Plots {.tabset .tabset-pills}
```{r results = 'hide', message = FALSE, fig.height = 5, fig.width = 5, echo = FALSE}
plot1 <- ggplot(data = mtcars) +
geom_point(aes(x = mpg, y = hp))
plot2 <- ggplot(data = mtcars) +
geom_point(aes(x = mpg, y = wt))
```
## mpg vs hp
```{r, echo = FALSE}
plot1
```
## mpg vs wt
```{r, echo = FALSE}
plot2
```
#
The plots above show how mpg is related..
# Analysis
the mtcars dataset is a great exploratory dataset to show..
Section 3 is a tabbed section allowing the user to switch between plots. I'd like to have static text underneath it.
The problem is, without starting a new heading, the content is only visible when the 2nd tab is selected. Not good. I can fix this by starting a new heading and putting the content in there.. but now I have a numbered section 4 in my table of contents that's blank. Also not desirable.
Is there any way to fix this? In the Rmarkdown cheatsheet https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf, you supposedly can end a tabbed section with ###, but that doesn't seem to work either.
I am not sure about any built-in solution. In such cases I usually just go with jQuery. In this case, it is a one-liner.
Reproducible Example:
---
title: "Mtcars Example"
output:
html_document:
number_sections: true
---
<script>
$(document).ready(function() {
$('#myDiv').appendTo('#first-tab-sec');
});
</script>
<div id="myDiv">a
The quick brown fox jumps over the lazy dog.
</div>
# Plots {.tabset .tabset-pills #first-tab-sec}
## mpg vs hp
```{r, echo = FALSE}
plot(mtcars$mpg, mtcars$hp)
```
## mpg vs wt
```{r, echo = FALSE}
plot(mtcars$mpg, mtcars$wt)
```
# Next Section
We created a div element with the id myDiv. Inside that element we store the content, thats supposed to go underneath the tabbed area.
The jQuery (JS) snippet does the following:
As soon as the document finished loading ($(document).ready()), we execute the anonymous function(){...}. Inside that function we grab our div element by its id and append it to the element with the id first-tab-sec.
Multiple Tabbed Sections
If we have another tabbed-section, we just give it a new id, for example second-tab-sec, and add the code
$('#mySecDiv').appendTo('#second-tab-sec');
to the JS chunk. Here we assume that the content is contained within a div with the id mySecDiv.

What is the maximum fig.height for html output when using knitr

While creating an HTML doc using the excellent knitr package, I noticed that sometimes the script would crash and sometimes not. After much poking around, I realized that it was simply due to the fig.height parameter in the chunk header
This caused a crash:
```{r, heatmap_res, eval=TRUE, echo=FALSE, fig.height=200}
yet this was OK and yielded all graphs as expected:
```{r, heatmap_res, eval=TRUE, echo=FALSE, fig.height=100}
Here is a reproducible example using ggplot2's diamonds dataset:
---
title: "figheight"
author: "Hackr"
date: "June 24, 2015"
output: html_document
---
```{r, heatmap_res, eval=TRUE, echo=FALSE, fig.height=200}
library(ggplot2)
data(diamonds)
ggplot(diamonds, aes(cut, price, fill = depth)) + geom_tile() + facet_wrap(~ clarity + color, ncol = 1)
```
So what is the maximum value allowed in the fig.height parameter? Also what units is this value? inches?
Thanks, Hackr
The chunk option fig.height is in inches. Normally you do not want a figure of 200 inches tall, although in theory there is no upper limit of the figure height.