第 7 章 Web application

  • Webpage

    • Everything is fully downloaded to your computer.

    • Interaction happens within your computer.

  • Web service

    • Only web UI is downloaded to your computer.

    • Interaction may require sending requests to remote computers (aka server) to obtain necessary data to update your browser view.

    • You need to know how to build a webpage in order to build a web service.

7.1 Webpage

7.1.1 The basic

  • a set of documents saved in a project folder, which is called root directory for your webpage.

  • Documents generally contain three major parts:

    • .html

    • .css

    • .js

  • There are other parts such as image, data, etc.

7.1.2 A simple website project

Run:

The following function:

creates a website project under your ./webProject0 folder, with a file structure:

levelName

1 index.html 2 ¦–/js
3 ¦–/css
4 °–/assets

  • index.html is the main webpage file for browser to read. Its

    • style is governed by .css,

    • interaction is governed by .js,

    • image, audio, video files are saved in /assets

Though there are three parts, .css, .js files and asset files, they can be contained within .html so that the html document is self-contained.

  • self-contained: you can share only the .html to others.

    • all .css, .js, and asset files are included inside .html
  • not self-contained: to share a html, you need to share the entire project folder.

    • some .css in /css, .js in /js, asset files in /assets.

試著用Rmardown knit一份html檔。 使用兩種output的設定:

設定1:

設定2:

7.1.3 file path

In a .html, when it refers to some external file (js, css or asset), the following two file path expression are common:

img/...: means at the .html file directory there is a folder called img.

                  levelName

1 /
2 ¦–an .html with ‘img/a.png’ 3 °–img/
4 °–a.png

./img/: means at the root directory there is a folder called img.

                       levelName

1 ./
2 ¦–index.html
3 ¦–page/
4 ¦ °–p1.html with ‘./img/a.png’ 5 °–img/
6 °–a.png

7.1.4 html tags

  • <div>

  • <span>

7.1.5 div and span

加到index.html body:

  • inline element: span

  • block element: div

7.1.6 attributes and style

7.1.7 CSS

An element’s style can also be defined outside the element style attribute:

Within .html, use <style> tag

  • placed inside header or the beginning of body

  • img, #myTargetElement, .myBlue is called CSS selector. It defines which element(s) should the style applied to. (# for id attr, . for class attr)

  • id獨一無二,每個id在一個html檔只能有一個。

  • class可用在多數個element。

7.1.7.1 External .css file

執行:

將my.css貼如下內容,並把檔案放到css/目錄下:

在html header加入:

7.1.8 Javascript

For a start, it is not a good idea to mix up your HTML and your JavaScript, as it becomes hard to parse — keeping your JavaScript all in one place is better; if it is in a separate file you can apply it to multiple HTML documents.

在html裡用<script> tag來寫Javascript:

7.1.8.1 宣告變數var

變數第一次出現時要先以var宣告開始。

7.1.8.2 DOM (Document Object Method)

JS把html document的每個<tab>...</tab>視為一個Object,並賦予它許多有趣的可能變化方式,稱為Method。

首先,要拿出某個element成為Document object:

  • get an element: document.getElementBy...()

.getElementById(‘{idname}’)

  • 選一個element為目標,增加id="myTarget"這個attribute。

會選出這個element object。

document.querySelector(“{CSS selector}”)

每個element可以用CSS selector描述它的位置。在chrome developer Elements tab,選到所要element,按右鍵選Copy JS Path, 其內容即為完整

再來可以取得element裡的content, attribute, 或style並加以改變:

開啟一個網頁,選其中一個element,並執行以下JS,讓它的內容消失:

7.1.8.3 Event

getElement後addEventListener

  • Event handler: the function that takes action.

7.1.8.4 inline使用

在html inline寫下:

7.1.8.5 external include

在html inline寫入:

7.1.9 SVG

7.1.9.3 Select SVG element

分成「SVG內部JS」(即JS位於<svg>裡,Internal JS)與「SVG外部JS」(External JS)兩種,這裡只講會用到的External JS。然SVG又可分成「html內含SVG」及「html外部引入SVG」兩種:

  • html內含SVG+External JS:JS取SVG元素和以前所學相同。

  • html外部引入SVG + External JS:

當html引用到外部檔案時,必需用http serve的方式瀏覽,也就是要把你的電腦虛擬成一個server,只能用http://方式開啟網頁(注意不是https),在R console執行:

範例:

執行以下程序得到一個getSVGelement資料匣,以local serve方式打開html檔:

試著:

  • 滑鼠點擊黃圓圈,它會變成藍圓圈。

  • 滑鼠點擊右邊三角帽,左邊的圓圈會變粉紅色。

以下做法當滑鼠hover在三角帽時其顏色變透明,供大家參考:

7.1.10 JSON

JavaScript Object Notation: text, written with JavaScript object notation.

JavaScript data types:

JSON content:

  • JSON objects are surrounded by curly braces {}:

    JSON objects are written in key/value pairs.
    Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).

  • JSON arrays are surrounded by brackets []:

    Array values must be of type string, number, object, array, boolean or null.

7.1.10.1 Inline使用

  • JSON內容包覆在<script type="application/json">...</script>中。
  • getElementById('json1').innerHTML方式取出,再用JSON.parse()轉成JS object使用:

7.1.10.2 R物件轉JSON

  • JSON內容可用jsonlite::toJSON()產生,用writeLines存成外部json檔。

範例:將R oject轉成html下的JSON element mtcars dataframe存成mtcars.json檔

  • (1)將R物件轉JSON,若沒有(2),只要在code chunk外用<script type="application/json>...</script>包覆knit完就成為inline使用的JSON資料

  • (2)將JSON另外存在mtcars.json檔中。

7.1.10.3 外部JSON inline使用

  • JSON存在外部檔案,但在HTML想用<script> inline使用。

要在HTML產生inline <script>,只要在RMarkdown裡:

當knit成html時,此段內容會變成

要在JavaScript引入此資料時,可使用:

7.1.11 Online form

包含在<form>...</form>裡,使用者最後輸入/選擇的值主要由裡頭的<input>, <select>, <textarea>這些input element的attrs. 或innerHTML內容決定。

<form>裡的input element進行:

7.2 不同步傳輸

  • in_header: JS需要先load完網頁layout才會正常的。

  • after_body: 網頁layout完成才需要執行的JS;

    • 也可以:

網頁外部訊息的pull in多數不是同步的,有時會造成你js效果失效。此時可以把你的js, 包在:

7.3 Webpage hosting

7.4 flexdashboard

  • R Markdown to Markdown: code chunks are processed.

    • echo: should code present in the webpage. (=T if yes)

    • eval: should code be processed. (=T if yes)

  • Markdown to HTML: convert anything not html to html.

    • If it is already html, leave it as it is.

7.4.1 Setup

先安裝flexdashboard

  1. File > New File > R Markdown > From Template > Flex Dashboard

  2. Design your dashboard.
  1. Knit your Rmd.

  2. Publish to your bookdown.org

7.4.2 Publish

For the first time to publish, at step 4 you press the publish icon:

  1. Select RStudio Connect.

  2. Select Publish finished document only.

  3. Select Add new account.

  4. Type bookdown.org, click next.

Once it is published to bookdown.org, you have to change Who can view this document from you to Anyone, and Save.

  1. Copy the URL under Custom URL (which is your webpage url)

7.4.3 External setup

執行以下程式

在frontmatter的flexdashboard::flex_dashboard: enter換行後,可輸入

includes:  
  in_header:  ["檔案1", "檔案2", ...]
  before_body:  ["檔案1", "檔案2", ...]
  after_body:  ["檔案1", "檔案2", ...]

它會將knit後的html成品加上檔案中的html scripts.

典型html:

<html>
  <header>
    {in_header加在這}
  </header>
  <body>
    {before_body加在這}
    ...
    {after_body加在這}
  </body>
</html>

7.4.3.1 Fontawesome Example

以下指令會創一個headerScript.html檔:

請在裡面加上:

<!---Fontaweseom-->
<script src="https://kit.fontawesome.com/fda67dba4d.js" crossorigin="anonymous"></script>

恭喜你,你已可以在Rmd文件中自由使用fontawesome。

執行以下程式:

利用

html_create_plain("xxx.html")

產生一個單純的html檔,檔名xxx可自取。(此函數會同時以browser及RStudio打開檔案呈現)

  • 引入fontawesome.

  • 在body裡加上<i class="far fa-address-card"></i>

  • refresh browser view.

<i>是inline element, 可以放在文字訊息字裡行間當成一個字使用。

7.4.4 Inline web language

R Markdown allows you to intertwine web languages (html, css, js) inside the document:

7.5 流程圖:Sidebar UI設計流程圖

7.6 流程圖:SVG生成與引入

以下流程圖說明如何形成獨立SVG檔以便融入flexdashboard使用。