시작하기무료로 시작하기

나만의 첫 shinyApp

이전 예제에서 입력과 출력을 shinyApp 안에서 어떻게 결합하는지 보셨고, 수면 연구 결과를 전달하도록 shinyApp을 구성하는 방법도 살펴보셨습니다.

이제 목표는 두 가지 주요 결과를 보여주는 shinyApp을 만드는 것입니다:

  1. 수면 시간의 분포
  2. 연령대별 수면 시간의 중앙값

이번 연습에서는 데이터를 sleep이라는 dplyr 데이터 프레임으로 제공하며, shinytidyverse 라이브러리는 이미 로드되어 있습니다.

이제 여러분 차례예요. 직접 shinyApp을 만들어 보세요!

이 연습은 강의의 일부입니다

shinydashboard로 대시보드 만들기

강의 보기

연습 안내

  • mainPanel에 "histogram"과 "barchart"라는 두 개의 플롯 출력을 추가하세요.
  • checkboxGroupInput()choiceNames 인자에서 내용을 "calendar", "briefcase", "gift" 아이콘으로 바꾸세요.
  • histogrambarchart라는 두 개의 출력을 정의하세요.
  • shinyApp() 함수를 사용해 shiny 앱을 렌더링하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  fluidRow(
  # Place two plots here, called "histogram" and "barchart"
  mainPanel(___("histogram"), ___("barchart")), 
  inputPanel(sliderInput("binwidth", 
                         label = "Bin width", 
                         min = 0.1, max = 2, 
                         step = 0.01, value=0.25),
             checkboxGroupInput("days", "Choose types of days:",
                                # Replace the list elements with icons called "calendar", "briefcase" and "gift"
                                choiceNames = list("All days", 
                                                   "Non-holiday weekdays", 
                                                   "Weekend days/holidays"), 
                                choiceValues = list("All days", 
                                                    "Nonholiday weekdays", 
                                                    "Weekend days and holidays"))), 
  "In general, across the different age groups, Americans seem to get adequate daily rest." ))

server <- function(input, output, session) {
  # Define the histogram and barchart
  output$histogram <- ___({
    ggplot(sleep, aes(x=`Avg hrs per day sleeping`)) + 
    geom_histogram(binwidth = input$binwidth, col='white') + 
    theme_classic()
  })
  ___ <- ___({
    filter(sleep, `Type of Days` %in% input$days) %>%
      group_by(`Type of Days`, `Age Group`) %>%
      summarize(`Median hours` = median(`Avg hrs per day sleeping`)) %>%
      ggplot(aes(x = `Median hours`, y = `Age Group`, fill = `Type of Days`)) +
      geom_col(position = 'dodge') + theme_classic()
  })
}

# Use shinyApp() to render the shinyApp
___
코드 편집 및 실행