LoginSignup
0
2

More than 3 years have passed since last update.

R shiny でデータの基礎分析用アプリの作成②

Last updated at Posted at 2021-03-05

はじめに

R shiny でデータの基礎分析用アプリの作成①の続き。タブを作ったので、統計量を計算して表示させるところから。

統計量の計算と表示用のデータフレームの作成

平均値や標準偏差などの統計量の計算と、それをまとめたデータフレームを作成する関数を定義していく。ついでに量的変数のみで相関係数を計算する関数も追加しておく。それらを一つのRスクリプトとして作成する。

helpers.R
library(dplyr)

ana_stat <-function(df) {
  classes <- df %>% summarise_all(class) %>% t()
  means <- df %>% select(!where(is.character)) %>% summarise_all(mean, na.rm=TRUE) %>% t()
  vars <- df %>% select(!where(is.character)) %>% summarise_all(var, na.rm=TRUE) %>% t()
  sds <- df %>% select(!where(is.character)) %>% summarise_all(sd, na.rm=TRUE) %>% t()
  qs <- df %>% select(!where(is.character)) %>% summarise_all(quantile, na.rm=TRUE) %>% t()
  colnames(qs) <- c("min", "q14", "median", "q34", "max")
  is.nas <- df %>% is.na %>% as.data.frame %>% summarise_all(sum) %>% t()

  stat.1 <- data.frame(
    rownames=rownames(classes),
    classes=classes,
    is.nas=is.nas,
    is.not.nas=nrow(df)-is.nas
  )

  stat.2 <- data.frame(
    rownames=rownames(means),
    means=means,
    vars=vars,
    sds=sds,
    qs
  )

  stat <- full_join(stat.1, stat.2, by="rownames")

  return(stat)
}

ana_cor <- function(df) {
  r <- df %>% select(!where(is.character)) %>% cor
  return(r)
}

メインパネルへの表示

helpers.Rを読み込みつつ、データの統計量を表示してくれるよう改訂したアプリが以下のものになる。

app.R
# Load packages ----
library(shiny)
library(DT)

# Source helpers ----
source("helpers.R")

# User interface ----
ui <- fluidPage(
  titlePanel("Basic analysis"),

  sidebarLayout(
    sidebarPanel(
      fileInput("file",
                label = "File input",
                accept = c("text/csv"),
                multiple = FALSE,
                width = "80%")
    ),

    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Data", dataTableOutput("outFile")),
                  tabPanel("Statistic", dataTableOutput("outStat")),
                  tabPanel("Correlation", dataTableOutput("outCor")),
                  tabPanel("Visualize", h1("Plot"))
      )
    )
  )
)

# Server logic
server <- function(input, output) {

  inFile <- reactive({
    tmp <- input$file
    if (is.null(tmp)){
      return(NULL)
    } else {
      df <- read.csv(tmp$datapath, header=TRUE)
      return(df)
    }
  })

  stat <- reactive({
    tmp <- inFile()
    if (is.null(tmp)) {
      return(NULL)
    } else {
      res <- ana_stat(tmp)
      return(res)
    }
  })

  r <- reactive({
    tmp <- inFile()
    if (is.null(tmp)) {
      return(NULL)
    } else {
      res <- ana_cor(tmp)
      return(res)
    }
  })

  output$outFile <- DT::renderDataTable({
    data.frame(inFile())
  }, extensions = c('Buttons'), 
  options = list(dom = 'Blfrtip',
                 buttons = c('csv', 'excel', 'pdf'))
  )

  output$outStat <- DT::renderDataTable({
    data.frame(stat())
  }, extensions = c('Buttons'), 
  options = list(dom = 'Blfrtip',
                 buttons = c('csv', 'excel', 'pdf'))
  )

  output$outCor <- DT::renderDataTable({
    data.frame(r())
  }, extensions = c('Buttons'), 
  options = list(dom = 'Blfrtip',
                 buttons = c('csv', 'excel', 'pdf'))
  )

}

# Run the app
shinyApp(ui, server)

analysis_app_3.gif

明示的にDT::renderDataTableを使い、各出力が保存できるようにCSV, Excel, PDFの出力ボタンも付け加えた。

おわりに

基本統計量が出力できたので、次はいよいよ可視化の部分である。プロットの種類、軸、などを選んでプロットが作れるようにしたいと思う。

R shiny でデータの基礎分析用アプリの作成③

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2