Themes
Shiny giúp bạn tùy chỉnh giao diện (theme) của ứng dụng một cách dễ dàng. Các hàm UI trong Shiny sử dụng Twitter Bootstrap, một framework phổ biến để xây dựng ứng dụng web. Bootswatch mở rộng Bootstrap bằng cách giúp bạn “khoác áo” cho ứng dụng chỉ với rất ít thay đổi trong mã.
Trong bài tập này, bạn sẽ thêm một title panel cho ứng dụng, dùng trình chọn theme để khám phá các theme khác nhau, và sau đó áp dụng theme bạn thích.
Bài tập này là một phần của khóa học
Xây dựng ứng dụng web với Shiny trong R
Hướng dẫn bài tập
- Chạy ứng dụng và dùng trình chọn theme ở góc trên bên phải để khám phá các theme của Bootswatch.
- Thêm một tiêu đề phù hợp cho trang.
- Thay
themeSelector()bằng một theme do bạn chọn.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
ui <- fluidPage(
# CODE BELOW: Add a titlePanel with an appropriate title
# REPLACE CODE BELOW: with theme = shinythemes::shinytheme("")
shinythemes::themeSelector(),
sidebarLayout(
sidebarPanel(
selectInput('name', 'Select Name', top_trendy_names$name)
),
mainPanel(
tabsetPanel(
tabPanel('Plot', plotly::plotlyOutput('plot_trendy_names')),
tabPanel('Table', DT::DTOutput('table_trendy_names'))
)
)
)
)
server <- function(input, output, session){
# Function to plot trends in a name
plot_trends <- function(){
babynames %>%
filter(name == input$name) %>%
ggplot(aes(x = year, y = n)) +
geom_col()
}
output$plot_trendy_names <- plotly::renderPlotly({
plot_trends()
})
output$table_trendy_names <- DT::renderDT({
babynames %>%
filter(name == input$name)
})
}
shinyApp(ui = ui, server = server)