Nhiều cách xem dữ liệu: vẽ và tải xuống

Nghiên cứu tình huống: Xây dựng ứng dụng web với Shiny trong R

Dean Attali

Shiny Consultant

Vẽ dữ liệu

  • Biểu đồ là bước đầu khi khám phá bộ dữ liệu mới

    • Biểu đồ là output
    • Hàm chỗ giữ chỗ cho output biểu đồ trong UI:
    plotOutput("my_plot")
    
    • Hàm render biểu đồ trong server:
  output$my_plot <- renderPlot({
      # code for a plot
  })
Nghiên cứu tình huống: Xây dựng ứng dụng web với Shiny trong R

Tải dữ liệu xuống

  • Hỗ trợ tải xuống bằng nút tải

    chapter3_2_more_ways_to_view_data_plot_and_download.009.png

    • Có thể tạo mọi loại tệp để tải
    • tệp ảnh, tệp văn bản, tệp CSV
Nghiên cứu tình huống: Xây dựng ứng dụng web với Shiny trong R

Tệp CSV

  • Comma Separated Values

  • Lưu trữ bộ dữ liệu nhỏ–vừa

  • CSV của gapminder:

    country,continent,year,lifeExp,pop,gdpPercap  
    Afghanistan,Asia,1952,28.801,8425333,779.4453145  
    Afghanistan,Asia,1957,30.332,9240934,820.8530296  
    Afghanistan,Asia,1962,31.997,10267083,853.10071  
    Afghanistan,Asia,1967,34.02,11537966,836.1971382
    
  • Tạo tệp CSV:

write.csv(gapminder, "myfile.csv")
Nghiên cứu tình huống: Xây dựng ứng dụng web với Shiny trong R

Tải dữ liệu trong Shiny

  • Nút tải xuống được coi là output

    • Thêm nút tải xuống vào UI: (giống các hàm output)
    downloadButton(outputId = "download_data", 
                   label = "Download data")
    
    • Thêm download handler trong server: (giống các hàm render)
    output$download_data <- downloadHandler(
        filename = "data.csv",
        content = function(file) {
            # Code that creates a file in the path <file>
            write.csv(gapminder, file)
        }
    )
    
Nghiên cứu tình huống: Xây dựng ứng dụng web với Shiny trong R

Download handler

  output$download_data <- downloadHandler(
      filename = "data.csv",
      content = function(file) {
          # code that creates a file in the path <file>
          write.csv(gapminder, file)
      }
  )
  • downloadHandler() có hai đối số
    • filename
      • Tên tệp tải xuống
    • content(file)
      • Hàm với 1 đối số
      • Tạo tệp để tải, đối số là đường dẫn tệp
Nghiên cứu tình huống: Xây dựng ứng dụng web với Shiny trong R

Ayo berlatih!

Nghiên cứu tình huống: Xây dựng ứng dụng web với Shiny trong R

Preparing Video For Download...