테이블 출력 추가하기
Shiny 앱에 어떤 결과물을 추가하려면 다음 단계가 필요해요:
- 결과물(플롯, 테이블, 텍스트 등)을 만듭니다.
- 적절한
render___함수를 사용해 결과물 객체를 렌더링합니다. - 렌더링된 객체를
output$x에 할당합니다. - 적절한
___Output함수를 사용해 UI에 결과물을 추가합니다.
이번 연습에서는 앞에서 만든 아기 이름 탐색기 앱에 테이블 결과물을 추가해 보겠습니다. render___ 함수 안의 코드는 중괄호로 감싸야 한다는 점을 잊지 마세요(예: renderPlot({...})).
이 연습은 강의의 일부입니다
R로 Shiny 웹 애플리케이션 만들기
연습 안내
table_top_10_names라는 이름의 테이블 출력을 만들어, 성별과 연도별 상위 10개 인기 이름을 표시하세요. 데이터 프레임은top_10_names()함수를 사용해 생성할 수 있어요.- UI에 테이블을 표시하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
ui <- fluidPage(
titlePanel("What's in a Name?"),
# Add select input named "sex" to choose between "M" and "F"
selectInput('sex', 'Select Sex', choices = c("F", "M")),
# Add slider input named "year" to select year between 1900 and 2010
sliderInput('year', 'Select Year', min = 1900, max = 2010, value = 1900)
# CODE BELOW: Add table output named "table_top_10_names"
)
server <- function(input, output, session){
# Function to create a data frame of top 10 names by sex and year
top_10_names <- function(){
babynames %>%
filter(sex == input$sex) %>%
filter(year == input$year) %>%
slice_max(prop, n = 10)
}
# CODE BELOW: Render a table output named "table_top_10_names"
}
shinyApp(ui = ui, server = server)