[R]::Introduction to Shiny

YOKK m.
2 min readJun 26, 2018

--

Rstudio defines Shiny as a R package that makes it easy to build interactive web apps straight from R.

Introduction

App Template : ประกอบไปด้วย 4 ส่วนที่สำคัญ
1. library(shiny) — การเรียก package shiny
2. ui <- fluidPage() — เรียกง่ายๆก็คือ user interface คือส่วนที่เอาไว้แสดงผล อยากให้หน้าเพจแสดงผลอะไรก็บอกมันไว้ตรงนี้
3. server<-function(input,output){} — อธิบายง่ายๆก็เหมือนส่วนของbackend อยากให้Appทำอะไรได้บ้างก็ใส่functionไว้ในนี้
4.shinyApp(ui,server) — คำสั่งสำหรับการRun app ในscript

(left) App template, (right) what it generates — blank page
Example : หากต้องการให้หน้า webpage แสดงคำว่า “Hello World”

Create first *Input, *Output for dropdownlist

(uiOutput()← →renderUI())

Create first *Input, *Output
  1. ui <- fluidPage(uiOutput(“firstUiOutput”))
    * ui <- fluidPage(uiOutput(“firstUiOutput”)) — การเปิดหน้าเปล่า UI
    * ui <- fluidPage(uiOutput(“firstUiOutput”)) — การประกาศว่าจะเรียก function uiOutput()
    *ui <- fluidPage(uiOutput(“firstUiOutput”)) — outputID (จะแสดงอะไรเรียกIDนั้นๆ) ในที่นี้ตั้งชื่อid แรกว่า firstUiOutput
  2. server<-function(input,output) {…..}
    *output$firstUiOutput(output$outputId) — เป็นการประกาศค่าว่า เจ้าIDนี้คือใครและทำอะไร
    *output$firstUiOutput<-renderUI(selectInput(…)) — เรียก ui ขึ้นมาแสดงผล ซึ่งสามารถเป็นอะไรก็ได้เช่น textInput(),radioButtons(),… ในที่นี้เราจะเรียก selectInput ขึ้นมา(คำสั่งการสร้างdrop downlist )
    * selectInput(inputId = “firstUiOutput”,label=”First”
    ,choices = c(‘Dropdown1’,’Dropdown2',’Dropdown3')
    ,selected = ‘Dropdown1’))
    — inputId: ID สำหรับinputนี้ (หมายความว่า inputที่เรากำลังจะสร้างนั้นชื่อว่าอะไร ), label : จะเขียน labelว่าอะไร, choices: ตัวเลือกมีอะไรบ้าง,selected: default ค่าแรกที่เลือกคืออะไร

Create *Input, *Output for dropdownlist & range

--

--

YOKK m.
YOKK m.

Written by YOKK m.

a data scientist — Master of Data science is the degree, master of none is also me

No responses yet