Shiny 아키텍처 이해

Shiny 아키텍처를 이해합니다. UI, server, 입력 위젯(input widget), 출력 위젯(output widget), 렌더링(Rendering) 정도는 숙지해야 합니다.

유충현 https://choonghyunryu.github.io (한화생명)
2022-02-05

들어가기

본 핸즈온(튜토리얼)은 Shiny를 이해하려는 대상의 OJT를 염두로 작성되었습니다. 초심자 대상이기 때문에, Shiny의 ABC만 다룹니다. 그 이상의 학습을 원하는 분들은 Resouces 페이지를 참고하십시요.
학습 내용은 Shiny in seven lessons ( https://shiny.rstudio.com/tutorial/ ) 을 참조하여 작성하였습니다.

Shiny 아키텍처

UI와 Server의 상호작용

UI와 Server의 상호작용

UI와 server의 상호작용 예시

UI와 Server의 상호작용 예시

UI와 server의 상호작용 표준화

UI와 server의 상호작용을 shiny 스크립트 관점에서 표준화해 봅니다.

  1. 위젯들은 개별 위젯을 인식하도록 아이디를 부여해야 합니다.
    • 입력 위젯은 inputId,
    • 출력 위젯은 outputId
  2. 입력 위젯의 이름은 “기능명 + Input” 포맷으로 정의됩니다.
    • sliderInput = slider + Input,
    • 즉, 슬라이더로 사용자 입력을 받아서 서버로 전달하는 위젯
  3. 출력 위젯의 이름은 “기능명 + Output” 포맷으로 정의됩니다.
    • plotOutput = plot + Output,
    • 즉, 시각화된 R 플롯을 출력하는 위젯
  4. 렌더링 함수 이름은 “render + 기능명” 포맷으로 정의됩니다.
    • renderPlot = render + Plot,
    • 즉, 플롯으로 렌더링하는(플롯을 그리는) 함수

Shiny 첫 예제

히스토그램 그리기 예제

UI와 Server의 상호작용 예시를 구현한 shiny 프로그램은 다음과 같습니다.

눈으로 한번 쭉 훑어보세요. 이해할 것 같으면서도 어려운 겁니다.

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

예제 실행해 보기

아키텍처를 이해하려는 목적으로 결과를 보고 프로그램도 살펴 보세요.

shiny::runExample("01_hello")

tutorial

Shiny 공식 tutorial 페이지

Shiny 공식 tutorial 페이지를 살펴보고, 앱을 실행시키는 방법을 숙지하세요.

https://shiny.rstudio.com/tutorial/written-tutorial/lesson1/

Citation

For attribution, please cite this work as

유충현 (2022, Feb. 5). 애플리케이션 서버 구축을 위한 R 워크샾: Shiny 아키텍처 이해. Retrieved from https://choonghyunryu.github.io/workshop_lecture/introduce_shiny

BibTeX citation

@misc{유충현2022shiny,
  author = {유충현, },
  title = {애플리케이션 서버 구축을 위한 R 워크샾: Shiny 아키텍처 이해},
  url = {https://choonghyunryu.github.io/workshop_lecture/introduce_shiny},
  year = {2022}
}