Skip to content

Commit

Permalink
change html viewer; close #5
Browse files Browse the repository at this point in the history
  • Loading branch information
agricolamz committed Dec 31, 2019
1 parent f7a7072 commit d16d2af
Show file tree
Hide file tree
Showing 20 changed files with 414 additions and 90 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ URL: https://CRAN.R-project.org/package=phonfieldwork, https://agricolamz.github
BugReports: https://github.com/agricolamz/phonfieldwork/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
RoxygenNote: 7.0.2
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
export(add_leading_symbols)
export(annotate_textgrid)
export(concatenate_soundfiles)
export(create_image_look_up)
export(create_presentation)
export(create_sound_play)
export(create_viewer)
export(df_to_tier)
export(draw_sound)
export(extract_intervals)
export(get_textgrid_names)
export(rename_soundfiles)
export(rename_videofiles)
export(set_textgrid_names)
export(tier_to_df)
importFrom(grDevices,dev.off)
Expand Down
3 changes: 2 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
phonfieldwork 0.0.3 (...2019)
phonfieldwork 0.0.3 (24.09.2019)
- vertically and horisontally center text in presentations created by "create_presentation()"; thx @Pandaklez #1
- add the "font_size" argument to the "create_presentation()" function
- add "rename_videofiles()" function
- rebuild html viewer for sounds with JavaScript with the help of new functions "create_image_look_up()" and "create_sound_play()".

phonfieldwork 0.0.2 (23.09.2019)
- make the "create_presentation()" function render silently
Expand Down
35 changes: 35 additions & 0 deletions R/create_image_look_up.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#' Create image look_up objects for html viewer
#'
#' @author George Moroz <[email protected]>
#'
#' @param img_src string or vector of strings with a image(s) path(s).
#' @param img_caption string or vector of strings that will be displayed when image is clicked.
#' @param text string o vector of strings that will be displayed as view link. By default it is eye emoji (&#x1f441;).
#' @return a string or vector of strings
#'
#' @examples
#' create_image_look_up("path/to/your/file")
#'
#' @export
#'

create_image_look_up <- function(img_src,
img_caption = "",
text = "&#x1f441;"){
if(length(img_src) != length(img_caption)){
stop(paste0("It looks like the img_src variable contains ",
length(img_src),
" objects and the img_caption variable contains ",
length(img_caption)))
}
paste0("<a ",
"onmouseover=\"resize(this, '200%')\" ",
"onmouseout=\"resize(this, '100%')\" ",
"onclick = 'pic_appear(\"",
img_src,
"\", \"",
img_caption,
"\")'> ",
text,
"<a>")
}
25 changes: 25 additions & 0 deletions R/create_sound_play.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' Create audio play objects for html viewer
#'
#' @author George Moroz <[email protected]>
#'
#' @param snd_src string or vector of strings with a image(s) path(s).
#' @param text string o vector of strings that will be displayed as view link. By default it is ear emoji (&#x1f442;).
#' @return a string or vector of strings
#'
#' @examples
#' create_image_look_up("path/to/your/file")
#'
#' @export
#'

create_sound_play <- function(snd_src,
text = "&#x1f442;"){
paste0("<a ",
"onmouseover=\"resize(this, '200%')\" ",
"onmouseout=\"resize(this, '100%')\" ",
"onclick = 'sound_play(\"",
snd_src,
"\")'> ",
text,
"<a>")
}
22 changes: 19 additions & 3 deletions R/create_viewer.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ create_viewer <- function(audio_dir,
picture_dir,
textgrid,
tiers = 1,
merge_column,
caption_column = NULL,
about = "This page were created with the [phonfieldworks](https://github.com/agricolamz/phonfieldwork) package for R",
output_dir,
output_file = "stimuli_viewer",
render = TRUE){
if(is.null(caption_column)){caption_column = merge_column}
if(!("DT" %in% utils::installed.packages()[,"Package"])){
stop('For this function you need to install DT package with a command install.packages("DT").')
}
Expand Down Expand Up @@ -69,17 +73,29 @@ create_viewer <- function(audio_dir,
result_df$pictures <- paste0(picture_dir, "/", pictures)

# create a .csv file ------------------------------------------------------
tmp <- tempfile(fileext = ".csv")
utils::write.csv(result_df, tmp)
tmp1 <- tempfile(fileext = ".csv")
utils::write.csv(result_df, tmp1, row.names = FALSE)

# create about file -------------------------------------------------------
if(substr(about, nchar(about)-3, nchar(about)) == ".Rmd"){
tmp2 <- about
} else {
tmp2 <- tempfile(fileext = ".Rmd")
writeLines(about, tmp2)
}

# render .Rmd -------------------------------------------------------------
if(render == TRUE){
rmarkdown::render(paste0(.libPaths()[1],
"/phonfieldwork/rmarkdown/templates/annotation_viewer/skeleton/skeleton.Rmd"),
params = list(data = tmp),
params = list(data = tmp1,
about = tmp2,
merge_column = merge_column,
caption_column = caption_column),
output_dir = output_dir,
quiet = TRUE,
output_file = output_file)
message(paste0("Output created: ", output_dir, output_file, ".html"))
} else {
return(tmp)
}
Expand Down
5 changes: 5 additions & 0 deletions docs/first_example.html
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,11 @@
}
@media print {

@page {
size: landscape;
margin: 0;
}

slides slide {
display: block !important;
position: relative;
Expand Down
1 change: 1 addition & 0 deletions docs/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ create_viewer(audio_dir = "s1/s1_sounds/",
picture_dir = "s1/s1_pics/",
textgrid = "s1/s1_all.TextGrid",
tiers = c(1, 3),
merge_column = "labels",
output_dir = "s1/",
output_file = "stimuli_viewer")
```
Expand Down
33 changes: 5 additions & 28 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<html>

<head>

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />

Expand Down Expand Up @@ -1602,6 +1601,7 @@
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}

.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
Expand Down Expand Up @@ -1660,9 +1660,8 @@ <h1><span class="header-section-number">2</span> Install the package</h1>
<p>This tutorial can be cited as follows:</p>
<pre class="r"><code>citation(&quot;phonfieldwork&quot;)</code></pre>
<pre><code>##
## Moroz G (2019). _Phonetic fieldwork and experiments with
## phonfieldwork package_. &lt;URL:
## https://CRAN.R-project.org/package=phonfieldwork&gt;.
## Moroz G (2019). _Phonetic fieldwork and experiments with phonfieldwork
## package_. &lt;URL: https://CRAN.R-project.org/package=phonfieldwork&gt;.
##
## A BibTeX entry for LaTeX users is
##
Expand Down Expand Up @@ -1957,31 +1956,9 @@ <h2><span class="header-section-number">4.8</span> Create a viewer</h2>
picture_dir = &quot;s1/s1_pics/&quot;,
textgrid = &quot;s1/s1_all.TextGrid&quot;,
tiers = c(1, 3),
merge_column = &quot;labels&quot;,
output_dir = &quot;s1/&quot;,
output_file = &quot;stimuli_viewer&quot;)</code></pre>
<pre><code>##
|
| | 0%
|
|...................... | 33%
## ordinary text without R code
##
##
|
|........................................... | 67%
## label: make_viewer (with options)
## List of 3
## $ echo : logi FALSE
## $ message: logi FALSE
## $ warning: logi FALSE
##
##
|
|.................................................................| 100%
## ordinary text without R code
##
##
## /usr/bin/pandoc +RTS -K512m -RTS skeleton.utf8.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash+smart --output /home/agricolamz/work/packages/phonfieldwork/docs/s1/stimuli_viewer.html --email-obfuscation none --self-contained --standalone --section-divs --template /home/agricolamz/R/x86_64-pc-linux-gnu-library/3.6/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable &#39;theme:bootstrap&#39; --include-in-header /tmp/RtmpG7A8W8/rmarkdown-str15f138259130.html --mathjax --variable &#39;mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&#39; --metadata pagetitle=skeleton.utf8.md --lua-filter /home/agricolamz/R/x86_64-pc-linux-gnu-library/3.6/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /home/agricolamz/R/x86_64-pc-linux-gnu-library/3.6/rmarkdown/rmd/lua/latex-div.lua</code></pre>
<p>As a result, a <code>stimuli_viewer.html</code> was created in the <code>s1</code> folder.</p>
<pre><code>## │   │   ├── 01.wav
## │   │   ├── 02.wav
Expand Down
93 changes: 75 additions & 18 deletions docs/s1/stimuli_viewer.html

Large diffs are not rendered by default.

102 changes: 81 additions & 21 deletions inst/rmarkdown/templates/annotation_viewer/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
@@ -1,42 +1,102 @@
---
title: "Sound Viewer"
output: html_document
params:
data: your_file.csv
merge_column: "merge_column"
caption_column: "caption_column"
about: about.Rmd
editor_options:
chunk_output_type: console
---

<style>
.main-container {
margin-left: 0;
margin-right: 0;
#my_block {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0, 0.1);
}
#my_img {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
padding: 10px 0;
height: 150px;
}
</style>

<img id="expandedImg" style="position: fixed; top: 0; left: 900px; width:50%">
# {.tabset .tabset-fade .tabset-pills}
## data

```{r make_viewer, echo=FALSE, message=FALSE, warning=FALSE}
df <- read.csv(params$data, stringsAsFactors = FALSE)
df$audio <-
paste0('<audio controls> <source src="',
if(Sys.info()[1] == "Windows") "file:///",
df$audio,
'" type="audio/wav"></audio>')
df$pictures <-
paste0('<img src="',
if(Sys.info()[1] == "Windows") "file:///",
df$pictures,
'" style="width:10%" onclick="myFunction(this);">')
merge <- which(colnames(df) %in% params$merge_column)
captions <- which(colnames(df) %in% params$caption_column)
df[,merge] <-
paste(df[,merge],
phonfieldwork::create_image_look_up(img_src = df$pictures,
img_caption = df[,captions]),
phonfieldwork::create_sound_play(snd_src = df$audio))
df <- df[, -which(colnames(df) %in% c("audio", "pictures"))]
DT::datatable(data = df,
filter = 'top',
rownames = FALSE,
options = list(pageLength = 50, dom = 'tip'),
escape = FALSE)
```

<div class = "my_block" id="my_block" onclick = "pic_disappear()">
<span class="close">&times;</span>
<img class = "my_img" id="my_img">
<div class = "caption" id="caption">
</div>
</div>


## about

```{r, child = params$about}
```


<script>
function myFunction(imgs) {
var expandImg = document.getElementById("expandedImg");
var imgText = document.getElementById("imgtext");
expandImg.src = imgs.src;
imgText.innerHTML = imgs.alt;
expandImg.parentElement.style.display = "block";
}
function pic_appear(path, caption) {
var path, caption;
var block = document.getElementById("my_block");
var block_img = document.getElementById("my_img");
var caption_text = document.getElementById("caption");
block.style.display = "block";
block_img.src = path;
caption_text.innerHTML = caption;
}
function pic_disappear() {
var block = document.getElementById("my_block");
block.style.display = "none";
}
function sound_play(x) {
var audio = new Audio();
audio.src = x;
audio.play();
}
function resize(elem, percent) {
elem.style.fontSize = percent;
}
</script>
10 changes: 8 additions & 2 deletions man/annotate_textgrid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/create_image_look_up.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d16d2af

Please sign in to comment.