rstudio chunks not appearing after chunk that chunk that created them - rstudio

I don't quite understand placement of chunks in an Rstudio notebook. I need to know how to make the chunks and results in the markdown appear in the same order as the original document and be able to control which are included using the include= chunk option.
---
title: "Test markdown"
output:
html_notebook: default
---
```{r, setup, include=FALSE}
# set default chunk options:w
knitr::opts_chunk$set(echo = FALSE, include = FALSE, fig.width = 8, collapse = TRUE, warning = FALSE)
```
Note 0
```{r}
# Comment
c <- 92461110
m <- 7056556
```
```{r, include=TRUE}
c
```
Note 1
```{r}
paste("Nothing")
```
Note 2
```{r,include=TRUE}
paste("Something")
```
```{r, include=TRUE}
paste("something else")
```
I should get
Test Markdown
Note 0
92461110
Note 1
Note 2
[1] "Something"
[1] "Something else"
Instead I get
Test markdown
Note 0
Note 1
Note 2
[1] "Nothing"
[1] "Something"

Related

How to insert two images in the same line, one with left one with right alignment

I have two same size images, and I would like to insert in for knitting to a PDF document, one with left one with right alignment.
I have used
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Testing
![](meme.jpg){width=5%, align="left"} ![](meme.jpg){width=5%,align="right"}
but it does not seem to work.
What am I doing wrong?
Markdown does not include alignment but you can use LaTeX's \hfill:
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Testing
![](meme.jpg){width=5%} \hfill ![](meme.jpg){width=5%}

How to display ggplotly plots with dynamically created tabs and for-loops?

I have R markdown document and I want to dynamically create tabs with ggplotly graphics inside them
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r}
fig=ggplot(cars)+geom_point(aes(speed, dist))
```
# level 1
## level 2{.tabset .tabset-pills}
```{r echo=FALSE, results='asis'}
for (h in 1:3){
cat("###", h,'{-}', '\n\n')
ggplotly(fig)
cat( '\n\n')
}
```
I understand that it is different from normal ggplot graph and I looked at the solutions here: enter link description here but It did not work for me
Following this post this can be achieved like so:
Edit: Following this post I added two functions to pass the fig.width and fig.height to ggplotly.
Edit 2: Added the code to additionally use plotly::subplots.
---
title: test
date: "20 5 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r, echo=FALSE}
# Get the current figure size in pixels:
get_w <- function() {
with(knitr::opts_current$get(c("fig.width", "dpi", "fig.retina")),
fig.width*dpi/fig.retina)
}
get_h <- function() {
with(knitr::opts_current$get(c("fig.height", "dpi", "fig.retina")),
fig.height*dpi/fig.retina)
}
```
```{r}
fig <- ggplot(cars) +
geom_point(aes(speed, dist))
```
# level 1
## level 2 {.tabset .tabset-pills}
```{r, include=FALSE}
htmltools::tagList(ggplotly(fig))
```
```{r echo=FALSE, results='asis', fig.width=4, fig.height=4}
fig <- ggplotly(fig, width = get_w(), height = get_h())
for (h in 1:3) {
cat("###", h, '{-}', '\n\n')
print(htmltools::tagList(plotly::subplot(fig, fig, nrows=2, heights = c(0.1, 0.9))))
cat( '\n\n')
}
```
found a solution from this link too, this doesn't call for HTML, just markdown.
---
date: "20 5 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r}
fig <- ggplot(cars) +
geom_point(aes(speed, dist))
```
## Results {.tabset}
### 1
We show a scatter plot in this section.
```{r}
ggplotly(fig)
```
### 2
We show the data in this tab.
```{r}
ggplotly(fig)
```

How to specify figure sizes by using fig_height & fig_width in R Notebook

I encounter a problem about the figure size when I using R Notebook.
The fig_height & fig_width did not work in html_notebook when I set the fig_height: 2 &fig_width: 2 as below.
---
title: "R Notebook"
output:
html_notebook:
fig_height: 2
fig_width: 2
---
# test
## test2
```{r}
plot(cars)
```
The R Notebook
But if I change the html_notebook to html_document, the fig_height & fig_width will become as I need.
---
title: "R Notebook"
output:
html_document:
fig_height: 2
fig_width: 2
---
# test
## test2
```{r}
plot(cars)
```
The R Markdown
Does it mean R Notebook can not use fig_height: &fig_width: at the beginning ?? Or I do something wrong??
I using Rstudio 1.1.383, R version 3.4.2. and mac OS HIGH Sierra.
Thanks for your attention.

rmarkdown comments or something similar

When the rmarkdown file is knitted, there are something after the title area, as it is shown in the picture.
How to remove those?
I have set "warning=FALSE, comment=NA" in the r code chunk, but it does not work.
Some of the codes are as follows:
---
title: 'Validness Report by All Facilities'
subtitle: '2017-01-03 to 2017-01-09'
output: pdf_document
geometry: margin=0.5in
classoption: landscape
documentclass: article
---
```{r input, echo = FALSE, results = 'hide', cache = TRUE,
warning=FALSE, comment=FALSE, error=FALSE}
setwd("F:/")
dfDataIn_valid2 <- read.csv("full_valid.csv", stringsAsFactors = FALSE)
```
```{r validness, echo = FALSE, results = 'hide',
cache = TRUE, warning=FALSE, comment=FALSE, error=FALSE}
# Check if required packages are installed. If not, install them.
packages <- c("rJava", "xlsxjars", "xlsx", "lubridate", "dplyr", "lazyeval")
lapply(packages, library, character.only = TRUE)
```
You need chunk option message set to FALSE
```{r, echo = FALSE, results = 'hide', cache = TRUE, warning=FALSE, comment=FALSE, error=FALSE, message=FALSE)}
Also, you don't need to repeat the options for each chunk; you could also just set them in the first one:
```{r}
library(knitr)
opts_chunk$set(echo = FALSE, results = 'hide', cache = TRUE, warning=FALSE, comment=FALSE, error=FALSE, message=FALSE)
```

r: dprint: size of image of table alteration

I am using the dprint package with knitr , mainly so that I can highlight rows from a table, which I have got working, but the output image leaves a fairly large space for a footnote, and it is taking up unnecessary space.
Is there away to get rid of it?
Also since I am fairly new to dprint, if anybody has better ideas/suggestions as to how to highlight tables and make them look pretty without any footnotes... or ways to tidy up my code that would be great!
An example of the Rmd file code is below...
```{r fig.height=10, fig.width=10, dev='jpeg'}
library("dprint")
k <- data.frame(matrix(1:100, 10,10))
CBs <- style(frmt.bdy=frmt(fontfamily="HersheySans"), frmt.tbl=frmt(bty="o", lwd=1),
frmt.col=frmt(fontfamily="HersheySans", bg="khaki", fontface="bold", lwd=2, bty="_"),
frmt.grp=frmt(fontfamily="HersheySans",bg="khaki", fontface="bold"),
frmt.main=frmt(fontfamily="HersheySans", fontface="bold", fontsize=12),
frmt.ftn=frmt(fontfamily="HersheySans"),
justify="right", tbl.buf=0)
x <- dprint(~., data=k,footnote=NA, pg.dim=c(10,10), margins=c(0.2,0.2,0.2,0.2),
style=CBs, row.hl=row.hl(which(k[,1]==5), col='red'),
fit.width=TRUE, fit.height=TRUE,
showmargins=TRUE, newpage=TRUE, main="TABLE TITLE")
```
Thanks in advance!
I haven't used dprint before, but I see a couple of different things that might be causing problems:
The start of your code chunk has defined the image width and height, which dprint seems to be trying to use.
You are setting both fit.height and fit.width. I think only one of those is used (in other words, the resulting image isn't stretched to fit both height and width, but only the one that seems to make most sense, in this case, width).
After tinkering around for a minute, here's what I did that minimizes the footnote. However, I don't know if there is a more efficient way to do this.
```{r dev='jpeg'}
library("dprint")
k <- data.frame(matrix(1:100, 10,10))
CBs <- style(frmt.bdy=frmt(fontfamily="HersheySans"),
frmt.tbl=frmt(bty="o", lwd=1),
frmt.col=frmt(fontfamily="HersheySans", bg="khaki",
fontface="bold", lwd=2, bty="_"),
frmt.grp=frmt(fontfamily="HersheySans",bg="khaki",
fontface="bold"),
frmt.main=frmt(fontfamily="HersheySans", fontface="bold",
fontsize=12),
frmt.ftn=frmt(fontfamily="HersheySans"),
justify="right", tbl.buf=0)
x <- dprint(~., data=k, style=CBs, pg.dim = c(7, 4.5),
showmargins=TRUE, newpage=TRUE,
main="TABLE TITLE", fit.width=TRUE)
```
Update
Playing around to determine the sizes of the images is a total drag. But, if you run the code in R and look at the structure of x, you'll find the following:
str(x)
# List of 3
# $ cord1 : num [1:2] 0.2 6.8
# $ cord2 : Named num [1:2] 3.42 4.78
# ..- attr(*, "names")= chr [1:2] "" ""
# $ pagenum: num 2
Or, simply:
x$cord2
# 3.420247 4.782485
These are the dimensions of your resulting image, and this information can probably easily be plugged into a function to make your plots better.
Good luck!
So here's my solution...with some examples...
I've just copied and pasted my Rmd file to demonstrate how to use it.
you should be able to just copy and paste it into a blank Rmd file and then knit to HTML to see the results...
Ideally what I would have liked would have been to make it all one nice neat function rather than splitting it up into two (i.e. setup.table & print.table) but since chunk options can't be changed mid chunk as suggested by Yihui, it had to be split up into two functions...
`dprint` + `knitr` Examples to create table images
===========
```{r}
library(dprint)
# creating the sytle object to be used
CBs <- style(frmt.bdy=frmt(fontfamily="HersheySans"),
frmt.tbl=frmt(bty="o", lwd=1),
frmt.col=frmt(fontfamily="HersheySans", bg="khaki",
fontface="bold", lwd=2, bty="_"),
frmt.grp=frmt(fontfamily="HersheySans",bg="khaki",
fontface="bold"),
frmt.main=frmt(fontfamily="HersheySans", fontface="bold",
fontsize=12),
frmt.ftn=frmt(fontfamily="HersheySans"),
justify="right", tbl.buf=0)
# creating a setup function to setup printing a table (will probably put this function into my .Rprofile file)
setup.table <- function(df,width=10, style.obj='CBs'){
require(dprint)
table.style <- get(style.obj)
a <- tbl.struct(~., df)
b <- char.dim(a, style=table.style)
p <- pagelayout(dtype = "rgraphics", pg.dim = NULL, margins = NULL)
f <- size.simp(a[[1]], char.dim.obj=b, loc.y=0, pagelayout=p)
# now to work out the natural table width to height ratio (w.2.h.r) GIVEN the style
w.2.h.r <- as.numeric(f$tbl.width/(f$tbl.height +b$linespace.col+ b$linespace.main))
height <- width/w.2.h.r
table.width <- width
table.height <- height
# Setting chunk options to have right fig dimensions for the next chunk
opts_chunk$set('fig.width'=as.numeric(width+0.1))
opts_chunk$set('fig.height'=as.numeric(height+0.1))
# assigning relevant variables to be used when printing
assign("table.width",table.width, envir=.GlobalEnv)
assign("table.height",table.height, envir=.GlobalEnv)
assign("table.style", table.style, envir=.GlobalEnv)
}
# function to print the table (will probably put this function into my .Rprofile file as well)
print.table <- function(df, row.2.hl='2012-04-30', colour='lightblue',...) {
x <-dprint(~., data=df, style=table.style, pg.dim=c(table.width,table.height), ..., newpage=TRUE,fit.width=TRUE, row.hl=row.hl(which(df[,1]==row.2.hl), col=colour))
}
```
```{r}
# Giving it a go!
# Setting up two differnt size tables
small.df <- data.frame(matrix(1:100, 10,10))
big.df <- data.frame(matrix(1:800,40,20))
```
```{r}
# Using the created setup.table function
setup.table(df=small.df, width=10, style.obj='CBs')
```
```{r}
# Using the print.table function
print.table(small.df,4,'lightblue',main='table title string') # highlighting row 4
```
```{r}
setup.table(big.df,13,'CBs') # now setting up a large table
```
```{r}
print.table(big.df,38,'orange', main='the big table!') # highlighting row 38 in orange
```
```{r}
d <- style() # the default style this time will be used
setup.table(big.df,15,'d')
```
```{r}
print.table(big.df, 23, 'indianred1') # this time higlihting row 23
```

Resources