2021年5月29日 星期六

跟著那道光 - Arduino Car

主題

本專案說明使用 Arduino car 加上光敏電阻應用。








需求

1. Arduino 模組

2. Arduino Car 模組

3. 光敏電阻 2個

4. 免費軟體 Arduino IDE

執行成果

https://youtu.be/SXyq6urlTQo

程式碼,講義下載

https://github.com/rwepa/arduino_car

2021年4月27日 星期二

R shiny - reactable 互動表格視覺化


主題

R在使用 shiny 套件建立網頁應用程式時,如果希望表格可以產生互動視覺化效果,此時可以使用 reactable 套件[1]。詳細使用方法參考 reactable 官網[2]之說明。本篇文章範例3使用sparkline套件以繪製表格折線圖[3]。

使用觀念

1. reactable 套件使用五動式表格的函數為 reactable。

2. 如果希望配合 shiny 套件使用,則使用以下二種情形:
  1. ui.R 使用 reactableOutput 函數
  2. server.R 使用 renderReactable 函數
3. 表格功能:
  • 定義欄名稱 Column definitions
  • 排序 Sorting
  • 排序與自定NA值 Sorting with NA
  • 篩選 Filtering
  • 搜尋 Search
  • 分頁大小 Pagination
  • 群組與匯總 Grouping and Aggregation
  • 欄格式(貨幣,百分比,日期格式等) Column formatting
  • 表格加上繪圖(直方圖,盒鬚圖,線圖等) Columns plot
  • 佈景主題 Theme

關鍵字

#shiny
#reactable
#reactableOutput
#renderReactable 

R程式碼下載

R程式碼

# title    : reactable package
# date     : 2021.04.27
# author   : Ming-Chang Lee
# email    : alan9956@gmail.com
# RWEPA    : http://rwepa.blogspot.tw/
# Encoding : UTF-8

# 範例1 - 表格應用
library(reactable)

reactable(CO2)

# 範例2 - shiny 加上表格應用
library(shiny)

library(reactable)

ui <- fluidPage(
  titlePanel("reactable package example"),
  reactableOutput("table")
)

server <- function(input, output, session) {
  output$table <- renderReactable({
    reactable(CO2)
  })
}

shinyApp(ui, server)

# 範例3 - 表格加上折線圖 sparkline

library(dplyr)     #進行 %>% 操作
library(sparkline) #繪製表格折線圖
library(reactable)

mydata <- iris %>%
  group_by(Species) %>%
  summarise(width = list(Petal.Width)) %>%
  mutate(boxplot = NA, sparkline = NA)

reactable(mydata, columns = list(
  width = colDef(cell = function(values) {
    sparkline(values, type = "bar", chartRangeMin = 0, chartRangeMax = max(iris$Petal.Width))
  }),
  boxplot = colDef(cell = function(value, index) {
    sparkline(mydata$width[[index]], type = "box")
  }),
  sparkline = colDef(cell = function(value, index) {
    sparkline(mydata$width[[index]])
  })
))
# end

參考資料


2021年4月12日 星期一

2021-R軟體與Shiny Web應用程式設計


課程目標:

以R語言做為Web服務平台,使學員熟悉使用shiny套件建立Web化服務應用。

Shiny套件的主要功能 (revised from: Hadley Wickham, 2021)

1. 建立企業級儀表板

2. 互動式篩選資料分析

3. 使用視覺化與互動式溝通

4. 建立自我資料分析的工作流程

5. 建立互動式統計教學與資料科學概念,調校參數,理解結果之改變.

6. 提供決策者探索式資料分析等企業智慧計算應用.


課程大綱:

1.基礎shiny程式設計

1.1 shiny套件簡介

1.2 建立第一個shiny網頁程式

1.3 資料物件處理(tidyr, dplyr, stringr套件)

1.4 輸入控制項與檔案上傳

1.5 輸出控制項(文字,表報,圖形ggplot2, leaflet)

1.6 案例示範與操作練習(一)

2.進階shiny程式設計

2.1 版面配置

2.2 HTML UI

2.3 反應型函數(Reactive function)

2.4 shape檔案的輸入與處理

2.5 地理資料-shiny進階網頁程式

2.6 案例示範與操作練習(二)

上課日期:

2021年5月8,9日

報名網頁:

https://www.iiiedu.org.tw/courses/msa331t2101/

2021/5/3前報名即可享優惠價!

#shiny

#visualization

#training

#ggplot2

#leaflet

2021年3月17日 星期三

R-4.0.4-Patched 終結中文亂碼

更新

2021.4.1 

更新至最新版本至R-4.0.5, Windows and Mac 使用時皆不會有中文亂碼.

問題

近日 Windows 系統更新至最新版本 R-4.0.4, 使用時會有中文亂碼問題???











方法

下載最新更新檔即可解決此問題.

檔名: R-4.0.4patched-win.exe

參考: https://cran.r-project.org/bin/windows/base/rpatched.html

下載: https://cran.r-project.org/bin/windows/base/R-4.0.4patched-win.exe












關鍵字

#R

#debug

2021年3月15日 星期一

R shiny 互動模型 crosstalk 套件








主題

R在使用 shiny 套件建立網頁應用程式時, 如果希望二個控制項 (widgets)產生互相聯動效果, 此時可考慮使用 crosstalk 套件[1]. 例如:圖形上選取一個或一個以上之範圍,旁邊自動篩選資料值. 詳細使用方法參考crosstalk官網[2]之說明. 此互動式功能與 plotOutput("plot1", click = "plot_click")[3]功能類似. 本範例說110年2月年粗出生率(%)之互動式繪圖, 資料來源[4].

使用觀念

  1. crosstalk 套件使用 R 的API稱為「SharedData R6 class」。
  2. 將SharedData物件傳遞給資料框或其他R物件,即可產生互動連結之效果。
  3. 支援使用 htmlwidget 套件客製化互動控制項。

關鍵字
#shiny
#crosstalk
#interactive plot
#leaflet

套件
library(readxl)    # read_excel 匯入 Excel 資料檔
library(crosstalk) # SharedData 互相聯動效果
library(leaflet)   # leaflet    互動式地圖
library(DT)        # datatable  互動式表格
library(htmltools) # tags       HTML標籤

執行結果











R程式碼下載


R程式碼


# date     : 2021.03.16
# author   : Ming-Chang Lee
# email    : alan9956@gmail.com
# RWEPA    : http://rwepa.blogspot.tw/
# Encoding : UTF-8

library(readxl)    # read_excel
library(crosstalk) # SharedData
library(leaflet)   # leaflet
library(DT)        # datatable
library(htmltools) # tags

# 匯入資料
# https://github.com/rwepa/DataDemo/blob/master/taiwan_birth.xlsx
birth <- read_excel("taiwan_birth.xlsx")

# 將資料轉換為 SharedData 物件
df <- SharedData$new(birth[c("city", "annua_crude_birth_rate", "longitude", "latitude")])

# 建立標題標題
rr <- tags$div(
  HTML('<a href="http://rwepa.blogspot.com/">110年2月粗出生率%</a>')
)

# 建立2行互相聯動效果
bscols(widths = c(5, 7),
  leaflet(df, height=700) %>% 
    addTiles() %>% 
    addMarkers(popup = ~city) %>% 
    setView(lng = 120.9876, lat = 23.8387, zoom = 7) %>%
    addControl(rr, position = "topright"),
  datatable(df,
            rownames = FALSE,
            colnames = c("地區","110年2月台灣粗出生率%", "經度", "緯度"),
            options = list(pageLength = 10,
                           searching = FALSE),
            height=600)
)
# end

2021年3月2日 星期二

Python統計分析與最佳化計算課程

課程介紹:

課程採用免費Python軟體為主,主題包括基礎的資料結合、資料摘要與繪圖技巧、統計檢定、邏輯斯迴歸、高維度影像資料計算、線性與非線性最佳化應用。課程將透過案例示範與實際操作練習以增進學習成效。

課程目標:

使學員熟悉Python語言進行統計分析與最佳化計算,課程包括Python與資料庫連結、資料摘要與繪圖技巧、統計分析、線性與非線性模型最佳化計算。

報名: https://www.beclass.com/rid=2443e965faa62b76ba01



2021年1月21日 星期四

水文建模shiny應用 (Hydrological Modelling with shiny app)

主題

R在水文模型具有廣泛應用,包括水文資料的讀取與下載,水文資料分析與建模,詳細可參考 CRAN-Hydrology  [https://cran.r-project.org/web/views/Hydrology.html]。 其中 AirGR套件提供水文建模工具,該套件包括降雨徑流模型,積雪和融雪模型以及相關的校準和評估功能。另 airGRteaching套件,將建模結果以互動式網頁呈現,提供使用者更進一步的水文分析與應用。如果需修改為職場的內容,可修改 "C:\Users\88697\Documents\R\win-library\4.0\airGRteaching\ShinyGR"資料夾的 ui.R,server.R 二個檔案。

執行結果1: 水文模型


執行結果2: 水文模型綱要圖





關鍵字

# Hydrological Modelling 
# shiny

套件

1. airGR 套件 - 建立水文建模 https://cran.r-project.org/web/packages/airGR/index.html
2. airGRteaching 套件- 提供水文建模互動式網頁呈現 https://cran.r-project.org/web/packages/airGRteaching/index.html
3. shiny套件 - 提供互動式網頁功能 https://cran.r-project.org/web/packages/shiny/index.html

R程式碼下載


R程式碼

# title   : 水文建模shiny應用
# author  : Ming-Chang Lee
# email   : alan9956@gmail.com
# RWEPA   : http://rwepa.blogspot.tw/
# GitHub  : https://github.com/rwepa
# resource: https://rwepa.blogspot.com/2021/01/shiny-hydrological-modelling-with-shiny.html

# 安裝2個基本套件
# install.packages(c("airGR", "airGRteaching"))

# 使用remotes套件, 安裝最新版htmlwidgets套件
# 一定要安裝最新版htmlwidgets套件, 否則會有錯誤.
# install.packages("remotes")
# remotes::install_github("ramnathv/htmlwidgets")

library(airGRteaching)

# 載入L0123001資料集
data(L0123001, package = "airGR")

# 顯示物件清單
ls()
# [1] "BasinInfo" "BasinObs"

head(BasinObs)
# "DatesR": 年-月-日, POSIXct物件
# "P"  : average precipitation [mm/time step] 每日平均降雨量
# "T"  : catchment average air temperature [℃]  平均氣溫
# "E"  : catchment average potential evapotranspiration [mm/day] 平均潛在蒸發量
# "Qls": outlet discharge [l/s] 出水口排出量
# "Qmm": outlet discharge [mm/day] 出水口排出量

# 建立低地盆地資料
BV_L0123001 <- BasinObs[0001:6000, c("DatesR", "P", "E", "Qmm", "T")]

# 建立多山盆地資料
BV_L0123002 <- BasinObs[5000:9999, c("DatesR", "P", "E", "Qmm", "T")]
BI_L0123002 <- BasinInfo

# 建立互動式網頁資料分析
ShinyGR(ObsDF = list("Low-land basin" = BV_L0123001, "Mountainous basin" = BV_L0123002),
        ZInputs = list(NULL, median(BI_L0123002$HypsoData)),
        HypsoData = list(NULL, BI_L0123002$HypsoData),
        NLayers = list(5, 5),
        SimPer = list(c("1994-01-01", "1998-12-31"), c("2004-01-01", "2006-12-31")),
        theme = "United")

# theme 預設值為 "RStudio",可修改為以下值:
# ["Cerulean", "Cyborg", "Flatly", "Inrae", "Saclay", "United" or "Yeti"]