用 Shiny 探索数据集

案例研究:使用 R 的 Shiny 构建 Web 应用

Dean Attali

Shiny Consultant

用 Shiny 探索数据集

数据集

+ 交互环境

+ 查看数据

+ 筛选数据

+ 下载数据

= Shiny 应用

案例研究:使用 R 的 Shiny 构建 Web 应用

将数据可视为表格

country continent year lifeExp pop gdpPercap
Afghanistan Asia 1952 28.801 8425333 779.445315
Afghanistan Asia 1957 30.332 9240934 820.85303
Afghanistan Asia 1962 31.997 10267083 853.10071
Afghanistan Asia 1967 34.02 11537966 836.197138
Afghanistan Asia 1972 36.088 13079460 739.981106
Afghanistan Asia 1977 38.438 14880372 786.11336
Afghanistan Asia 1982 39.854 12881816 978.011439
Afghanistan Asia 1987 40.822 13867957 852.39595
Afghanistan Asia 1992 41.674 16317921 649.34140
Afghanistan Asia 1997 41.763 22227415 635.34135
案例研究:使用 R 的 Shiny 构建 Web 应用

Shiny 中的表格

  • 表格是输出

    • 在 UI 中用输出占位函数:
    tableOutput("my_table")
    
    • 在 server 中用 render 函数:
    output$my_table <- renderTable({
        gapminder
    })
    
案例研究:使用 R 的 Shiny 构建 Web 应用

筛选表格数据

  • 可用输入进行筛选

    • 在 UI 中添加输入:
    selectInput("country", "Country",
                choices = levels(gapminder$country))
    
    • 用输入筛选数据:
  output$my_table <- renderTable({
      subset(gapminder, country == input$country)
  })
案例研究:使用 R 的 Shiny 构建 Web 应用

选择输入的可选项

  • selectInput() 的 choices 参数可为任意字符串列表

    • choices 可取变量的子集
    selectInput("country", "Country",
                choices = levels(gapminder$country)[1:10])
    
    • choices 也可扩展以添加新值
    selectInput("country", "Country",
                choices = c("any", levels(gapminder$country)))
    
案例研究:使用 R 的 Shiny 构建 Web 应用

¡Vamos a practicar!

案例研究:使用 R 的 Shiny 构建 Web 应用

Preparing Video For Download...