【R】pixels
2020年9月29日
1. はじめに
pixels
は、htmlwidget や Shiny Gadget で簡単にピクセルデータを描画できるパッケージです。
2. インストール
CRANからインストールできます。
install.packages("pixels")
3. つかってみる
まずは、簡単な例として描いた文字、図のピクセルデータを返してくれる関数です。
library(pixels)
get_pixels()
library(pixels)
get_pixels()
library(pixels) get_pixels()
実行すると、Viewerペインに下図の画面が表示されます。28×28の領域に絵や文字をかけます。かいた後にDoneボタンを押すと、ピクセルデータを返してくれます。

[1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[20] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[39] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[58] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[77] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[96] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[115] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 2.1 1.8
[134] 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[153] 0.0 0.0 0.0 0.0 0.0 1.5 3.3 3.5 3.0 2.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[172] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 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0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[685] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[704] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[723] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[742] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[761] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[780] 0.0 0.0 0.0 0.0 0.0
次のコードはShinyアプリで、描いた画像をMNISTデータセットのように、/digitsフォルダ以下にデータとして保存してくれます。ですので、自分で画像認識用のデータセットを作れることになります。
library(shiny)
ui <- fluidPage(
tags$head(
tags$style(HTML("
#pixels {
height: 270px !important;
margin-top: 10px;
}
"))
),
titlePanel("Digit Capture Application"),
textOutput("prompt"),
shiny_pixels_output("pixels"),
actionButton("captureDigit", "Capture")
)
server <- function(input, output) {
output$pixels <- shiny_render_pixels(
show_pixels()
)
digit <- reactiveVal(floor(runif(1, 1, 10)))
output$prompt <- renderText(paste0("Please draw number ", digit(), ":"))
observeEvent(input$captureDigit, {
digit_path <- file.path("digits", digit())
if (!dir.exists(digit_path)) dir.create(digit_path, recursive = TRUE)
saveRDS(input$pixels, paste0(digit_path, "/", as.numeric(Sys.time()), ".rds"))
digit(floor(runif(1, 1, 10)))
output$pixels <- shiny_render_pixels(
show_pixels()
)
})
}
shinyApp(ui = ui, server = server)
library(shiny)
ui <- fluidPage(
tags$head(
tags$style(HTML("
#pixels {
height: 270px !important;
margin-top: 10px;
}
"))
),
titlePanel("Digit Capture Application"),
textOutput("prompt"),
shiny_pixels_output("pixels"),
actionButton("captureDigit", "Capture")
)
server <- function(input, output) {
output$pixels <- shiny_render_pixels(
show_pixels()
)
digit <- reactiveVal(floor(runif(1, 1, 10)))
output$prompt <- renderText(paste0("Please draw number ", digit(), ":"))
observeEvent(input$captureDigit, {
digit_path <- file.path("digits", digit())
if (!dir.exists(digit_path)) dir.create(digit_path, recursive = TRUE)
saveRDS(input$pixels, paste0(digit_path, "/", as.numeric(Sys.time()), ".rds"))
digit(floor(runif(1, 1, 10)))
output$pixels <- shiny_render_pixels(
show_pixels()
)
})
}
shinyApp(ui = ui, server = server)
library(shiny) ui <- fluidPage( tags$head( tags$style(HTML(" #pixels { height: 270px !important; margin-top: 10px; } ")) ), titlePanel("Digit Capture Application"), textOutput("prompt"), shiny_pixels_output("pixels"), actionButton("captureDigit", "Capture") ) server <- function(input, output) { output$pixels <- shiny_render_pixels( show_pixels() ) digit <- reactiveVal(floor(runif(1, 1, 10))) output$prompt <- renderText(paste0("Please draw number ", digit(), ":")) observeEvent(input$captureDigit, { digit_path <- file.path("digits", digit()) if (!dir.exists(digit_path)) dir.create(digit_path, recursive = TRUE) saveRDS(input$pixels, paste0(digit_path, "/", as.numeric(Sys.time()), ".rds")) digit(floor(runif(1, 1, 10))) output$pixels <- shiny_render_pixels( show_pixels() ) }) } shinyApp(ui = ui, server = server)

4. さいごに
ピクセルデータを簡単に生成できるので、自分でデータセットを作るには最適のパッケージですね。