시작하기무료로 시작하기

대화형 요소가 있는 첫 번째 shinydashboard

이전 단원에서 shinydashboard에 대화형 요소를 추가하는 방법과, 대화형 데이터 테이블을 추가하는 방법을 살펴봤어요.

이번 연습에서는 Airbnb 리스트가 listings라는 데이터 프레임에 저장되어 있고, 지리공간 지도는 m_london이라는 leaflet 객체에 저장되어 있어요. 헤더와 사이드바는 각각 header, sidebar로 저장되어 있어요.

사용자 정의 도우미 함수:

  • make_plots: 상자 그림(box plot) 또는 바이올린 플롯을 그려요
  • num_listings: 선택한 범위를 기준으로 리스트 개수를 계산해요
  • num_private_rooms: 선택한 범위를 기준으로 전체 리스트 대비 개인실 비율(%)을 계산해요
  • median_price: 선택한 범위를 기준으로 중간 가격을 계산해요 도우미 함수에 대해 더 알아보려면 콘솔에서 해당 함수를 실행해 보세요(예: make_plots).

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

shinydashboard로 대시보드 만들기

강의 보기

연습 안내

  • "plots"라는 이름의 plotly 상자 그림/바이올린 플롯을 추가하세요.
  • "table"이라는 이름의 데이터 테이블을 추가하세요.
  • "map"이라는 이름의 leaflet 지도를 추가하세요.
  • dashboardPage()에서 올바른 인자를 설정하세요.

실습형 인터랙티브 연습

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

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "charts",
            fluidRow(
              valueBoxOutput(outputId = "count"), valueBoxOutput(outputId = "prop"),valueBoxOutput(outputId = "med") ),
            fluidRow(
              tabBox(side = "left", id = "tabset", height = "500px",
                     tabPanel("Charts", 
                              # Place plotly object here
                              fluidRow(box(___("plots", height = 500, width = 600)) ) ), 
                     # Place dataTable object here
                     tabPanel("Data table", height = "500px", ___("table")) ), 
              box(side = "right", height = "200px", title = "Welcome to London!",),
              box(side = "right", height = "385px", title = "Controls",
                  sliderInput(inputId = "range", label = "Select price range:",
                              min = 0, max = 25000,value = c(0,2500)),
                  selectInput(inputId = "select", label = "Select group:", 
                              choices = c("Box plots", "Violin plots")) ) ) ),
    tabItem(tabName = "map",
            # Place leaflet object here
            fluidRow(box(title = "Map of listings in London", ___("map", height = 600, width = 700))) ) ) ) 

# Set the correct arguments for dashboardPage()
ui <- dashboardPage(___, ___, ___) 

server <- function(input, output) {
  output$count <- renderValueBox(valueBox("Number of listings", num_listings(input$range), 
                                        icon = icon("house-user") ))
  output$prop <- renderValueBox(valueBox("Private rooms", paste0(num_private_rooms(input$range), "% of all listings"),
                                        icon = icon("eye"), color = "orange") )
  output$med <- renderValueBox(valueBox("Median price", paste0(median_price(input$range), "£"), 
                                        icon = icon("money-bill-alt"), color = "olive") )
  output$plots <- renderPlotly(make_plots(input$range, input$select))
  output$table <- renderDataTable(filter(listings, price >= input$range[1], price <= input$range[2]) %>% select(c(name, neighbourhood, room_type, price)), 
                                  options = list(lengthMenu = c(5, 30, 50)))
  output$map <- renderLeaflet(m_london)
}

shinyApp(ui, server)
코드 편집 및 실행