Combine leaflet and markdown in loop - knitr

This question shows how to loop over/apply leaflet objects within a markdown file. I'd like to do a similar thing, though I'd like to add additional markdown content.
---
title: "Test"
output: html_document
---
```{r setup, echo=T,results='asis'}
library(leaflet)
library(dplyr) ### !!! uses development version with tidyeval !!!
library(htmltools)
##Add A Random Year Column
data(quakes)
quakes <- tbl_df(quakes) %>%
mutate(year = sample(2008:2010, n(), replace=TRUE))
```
```{r maps, echo=T,results='asis'}
createMaps <- function(year){
cat(paste("###", year, "\n"))
leaflet(quakes %>% filter(year == !!year)) %>%
addTiles() %>%
addMarkers(
lng = ~long,
lat = ~lat,
popup = ~as.character(mag))
cat("\n\n")
}
htmltools::tagList(lapply(as.list(2008:2010), function(x) createMaps(x) ))
```
If I leave out the cat statements in the createMaps function, this code prints all three maps. If I put in the cat statements, I get the markdown, but no maps. Any way to combine both types of element?

The problem is, that your cat statements are being evaluated, before lapply returns its result list.
Delete the cat statements, change your createMaps function to
createMaps <- function(year){
mymap <- leaflet(quakes %>% filter(year == !!year)) %>%
addTiles() %>%
addMarkers(
lng = ~long,
lat = ~lat,
popup = ~as.character(mag))
return(list(tags$h1(year), mymap))
}
and change tags$h1() to whatever size of header you want (tags$h2(), ...)

Related

R stringr using subset

In R using the stringr package, how would you get only three occurrences of a letter within a word using str_subset?
Example- the letter "a" three times within a word
Results- banana and Canada
library(stringr)
text <- c("Canada", "and", "banana", "baobab")
# Any character repeated three times:
#
# maybe something followed by a marked character, maybe followed by
# something different, followed by that character, maybe followed by
# something different, followed by that character, maybe followed by
# something different
pattern <- "^.*(.)+.*\\1.*\\1.*$"
are_matching <- str_detect(text, pattern)
are_matching
#> [1] TRUE FALSE TRUE TRUE
words_extracted <- str_subset(text, pattern)
words_extracted
#> [1] "Canada" "banana" "baobab"
letter_repeated <- str_replace(words_extracted, pattern, "\\1")
letter_repeated
#> [1] "a" "a" "b"
# That give you the "last" repeated character
str_replace("baobaba", pattern, "\\1")
#> [1] "a"
# Note: If you want the first repeated character (if multiple), you
# should be lazy both at the initial optional set of character and at
# the first marked matching. (Not relevant for "detect" and "subset")
lazy_text <- c("bananan", "baobaba")
lazy_pattern <- "^.*?(.)+?.*\\1.*\\1.*$"
str_replace(lazy_text, pattern, "\\1")
#> [1] "n" "a"
str_replace(lazy_text, lazy_pattern, "\\1")
#> [1] "a" "b"
Created on 2020-09-02 by the reprex package (v0.3.0)
This will give you all words in which at least one letter appears exactly 3 times:
library(tidyverse)
vec <- "banana and Canada"
words <- vec %>% str_split(" ") %>% .[[1]]
lgl_vec <- words %>% map_lgl(
~str_split(.x, "") %>%
.[[1]] %>%
factor() %>%
summary() %>%
"=="(3) %>%
any()
)
words[lgl_vec]
[1] "banana" "Canada"
Use str_extract_all:
input <- c("apple", "banana", "Canada")
regex <- "\\b[^\\WAa]*[Aa][^\\WAa]*[Aa][^\\WAa]*[Aa][^\\WAa]*\\b"
matches <- str_extract_all(input, regex)
Demo

Rmd table captions are messed up when knitting to word doc

I am trying to make tables in Rmd, with different captions or headers. The package flextable has great options that can be output to word documents. It's function add_header_lines() allows you to add a caption header at the top of each table. When Rmd output wraps to a new page, add_header_lines() adds another caption header to the top of the continued table on the next page. However, it grabs the arguments passed to whatever table you made first. Then it continues to be correct for the next table, until the next page is reached, where it reverts to the first again (see pictures).
Here is a reproducible example, where all values in a table should be the same as the Table number.
Any ideas on how to fix this? I would like it to have the correct caption, but would settle for simply getting rid of the second caption after a page break.
---
title: "Untitled"
author: "Anyone"
date: "2/29/2020"
output:
word_document: default
html_document:
df_print: paged
pdf_document: default
---
```{r setup, include=FALSE}
library(dplyr)
library(flextable)
knitr::opts_chunk$set(echo = FALSE,message=FALSE)
```
```{r Table1}
cars1<-cars*0+1
theme_zebra(regulartable(cars1))%>%
align(align="center",part="all") %>%
autofit%>%
add_header_lines(paste("Table 1: Model output for thing 1"))
```
```{r Table2}
cars2<-cars*0+2
theme_zebra(regulartable(cars2))%>%
align(align="center",part="all") %>%
autofit%>%
add_header_lines(paste("Table 2: Model output for thing 2"))
```
```{r Table3}
cars3<-cars*0+3
theme_zebra(regulartable(cars3))%>%
align(align="center",part="all") %>%
autofit%>%
add_header_lines(paste("Table 3: Model output for thing 3"))
```
```{r Table4}
cars4<-cars*0+4
theme_zebra(regulartable(cars4))%>%
align(align="center",part="all") %>%
autofit%>%
add_header_lines(paste("Table 4: Model output for thing 4"))
```
Note that this is where the page break is. The table labelled '1' here, should really be '2.' It will continue to do this for every page, where the rest of the table is labelled with a '1' (try Rmd code).
That's Word fault :) It decides that two tables with no break between them is the same table.
---
title: "Untitled"
author: "Anyone"
date: "2/29/2020"
output:
word_document: default
---
```{r setup, include=FALSE}
library(dplyr)
library(flextable)
knitr::opts_chunk$set(echo = FALSE,message=FALSE)
```
```{r Table1}
cars1<-cars*0+1
theme_zebra(flextable(cars1))%>%
align(align="center",part="all") %>%
autofit%>%
add_header_lines(paste("Table 1: Model output for thing 1"))
```
blah
```{r Table2}
cars2<-cars*0+2
theme_zebra(flextable(cars2))%>%
align(align="center",part="all") %>%
autofit%>%
add_header_lines(paste("Table 2: Model output for thing 2"))
```
blah
```{r Table3}
cars3<-cars*0+3
theme_zebra(flextable(cars3))%>%
align(align="center",part="all") %>%
autofit%>%
add_header_lines(paste("Table 3: Model output for thing 3"))
```
blah
```{r Table4}
cars4<-cars*0+4
theme_zebra(flextable(cars4))%>%
align(align="center",part="all") %>%
autofit%>%
add_header_lines(paste("Table 4: Model output for thing 4"))
```
As far as i know, this is not something that can be solved with flextable but if there is an option I am not aware of, I'd be happy to integrate it.

Knitr option hook results in LaTeX output

I'm creating ioslides and beamer output from RMarkdown source files and need to have variable figure output dependent on the output format.
I've generate a plot using ggplot2 which renders fine.
I want the plot to have an out.width set to 100% for HTML output and 70% for LaTeX output.
The problem is that when I set the option hook and check for LaTeX output, the tex file generated by knitr contains the LaTeX source verbatim for including the image which renders as text in the slide.
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
options
})
The plot renders fine in HTML output.
However, for beamer I get as shown in the image:
And the resulting output in the .tex file:
\begin{frame}{Why bother?}
\protect\hypertarget{why-bother}{}
\textbackslash begin\{center\}\textbackslash includegraphics{[}width=70\% {]}\{slide-book\_files/figure-beamer/lec-4-modularity-cost-1\}
\textbackslash end\{center\}
\end{frame}
Here's complete code for a MWE:
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
return (options)
})
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```
When setting out.width this way you have to use a format that LaTeX understands right away, i.e. 0.7\linewidth instead of 70%. And you have to double the backslash in the R code:
---
output:
html_document: default
pdf_document:
keep_tex: yes
---
```{r setup, include=FALSE}
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '0.7\\linewidth'
}
options
})
```
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
library(ggplot2)
library(tibble)
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```

Remove white spaces in scala-spark

I have sample file record like this
2018-01-1509.05.540000000000001000000751111EMAIL#AAA.BB.CL
and the above record is from a fixed length file and I wanted to split based on the lengths
and when I split I am getting a list as shown below.
ListBuffer(2018-01-15, 09.05.54, 00000000000010000007, 5, 1111, EMAIL#AAA.BB.CL)
Everything looks fine until now . But I am not sure why is there extra-space adding in each field in the list(not for the first field).
Example : My data is "09.05.54",But I am getting as" 09.05.54" in the list.
My Logic for splitting is shown below
// Logic to Split the Line based on the lengths
def splitLineBasedOnLengths(line: String, lengths: List[String]): ListBuffer[Any] = {
var splittedLine = line
var split = new ListBuffer[Any]()
for (i <- lengths) yield {
var c = i.toInt
var fi = splittedLine.take(c)
split += fi
splittedLine = splittedLine.drop(c)
}
split
}
The above code take's the line and list[String] which are nothing but lengths as input and gives the listbuffer[Any] which has the lines split according to the length.
Can any one help me why am I getting extra space before each field after splitting ?
There are no extra spaces in the data. It's just adding some separation between the elements when printing them (using toString) to make them easier to read.
To prove this try the following code:
split.foreach(s => println(s"\"$s\""))
You will see the following printed:
"2018-01-15"
"09.05.54"
"00000000000010000007"
"5"
"1111"
"EMAIL#AAA.BB.CL"

Inserting blank spaces at the end of a column name in a table using pander

I am trying to find a way of centering a column heading in a pander table using knitr to pdf in rmarkdwon, but keeping the column entries right justified.
---
title: "Table Doc"
output: pdf_document
---
```{r table, echo = FALSE}
table1 <- anova(lm(Petal.Length ~ Species*Petal.Width, iris))
names(table1) <- c("DF", "Sum Sq", "Mean Sq", "*F*", "*p*")
library(pander)
pander(table1, justify = c("left", rep("right", 5)))
```
There is no way to align individual cells inside a table in pandoc apparently. I want the entries to be to the right so they are all aligned properly but sit the column headings 'F' and 'p' in the center. So what I need to do is insert blank spaces after F and p to force them into the center. How do I do this? I tried simply inserting the blank spaces:
names(table1) <- c("DF", "Sum Sq", "Mean Sq", "*F* ", "*p* ")
but the spaces are not recognised by pander.
I also tried LaTex spacing characters
names(table1) <- c("DF", "Sum Sq", "Mean Sq", "*F*\\", "*p*\\")
but this didn't work either. Can anyone think of a workaround?