그래프 문법

ggplot
그래프 문법
esquisse
저자

이광춘

공개

2026-07-06

노트데이터셋

팔머펭귄 데이터셋 영문 csv 파일 혹은 한국 R 사용자회에서 한글로 번역한 csv 다운로드 받아 사용한다.

#| label: shinylive-esquisse
#| viewerHeight: 700
#| standalone: true

library(esquisse)
library(shiny)
library(ggplot2)

library(bslib)
library(showtext)
tryCatch(sysfonts::font_add_google("Nanum Myeongjo", "kr"), error = function(e) NULL)
showtext_auto()

# ── Tufte 팔레트 · 테마 (자동 주입) ──────────────────────────
bit_cream <- "#fffff8"; bit_ink <- "#111111"; bit_rust <- "#8a1500"
bit_muted <- "#4a4a44"; bit_grid <- "#e8e6dc"; bit_axis <- "#8a8578"
theme_tufte_bit <- function(base_size = 13) {
  ggplot2::theme_minimal(base_size = base_size) +
    ggplot2::theme(
      text = ggplot2::element_text(family = "kr", colour = bit_ink),
      plot.title = ggplot2::element_text(family = "kr", size = base_size, hjust = 0),
      plot.background = ggplot2::element_rect(fill = bit_cream, colour = NA),
      panel.background = ggplot2::element_rect(fill = bit_cream, colour = NA),
      panel.grid.major = ggplot2::element_line(colour = bit_grid, linewidth = 0.3),
      panel.grid.minor = ggplot2::element_blank(),
      axis.line = ggplot2::element_line(colour = bit_axis, linewidth = 0.3),
      axis.ticks = ggplot2::element_line(colour = bit_axis, linewidth = 0.3)
    )
}
if (requireNamespace("ggplot2", quietly = TRUE)) ggplot2::theme_set(theme_tufte_bit())
bit_theme <- bs_theme(
  version = 5, bg = bit_cream, fg = bit_ink, primary = bit_rust,
  base_font = font_collection("Palatino Linotype", "Georgia", "Times New Roman", "serif"),
  "card-box-shadow" = "none"
)
# ─────────────────────────────────────────────────────────────

ui <- fluidPage(
  theme = bit_theme,
  titlePanel("esquisse로 그래프 만들기"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "CSV 파일 선택",
                accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")),
      radioButtons(
        inputId = "data",
        label = "사용할 데이터 선택:",
        choices = c("mpg", "diamonds", "economics", "업로드한 데이터" = "uploaded_data")
      )
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "esquisse",
          esquisse_ui(
            id = "esquisse",
            header = FALSE # dont display gadget title
          )
        ),
        tabPanel(
          title = "출력",
          tags$b("코드:"),
          verbatimTextOutput("code"),
          tags$b("필터:"),
          verbatimTextOutput("filters"),
          tags$b("데이터:"),
          verbatimTextOutput("data")
        )
      )
    )
  )
)

server <- function(input, output, session) {
  data_r <- reactiveValues(data = iris, name = "iris")

  observeEvent(input$file, {
    req(input$file)
    data_r$data <- read.csv(input$file$datapath)
    data_r$name <- "uploaded_data"
  })

  observe({
    if (input$data != "uploaded_data") {
      data_r$data <- get(input$data)
      data_r$name <- input$data
    }
  })

  results <- esquisse_server(
    id = "esquisse",
    data_rv = data_r
  )

  output$code <- renderPrint({
    results$code_plot
  })

  output$filters <- renderPrint({
    results$code_filters
  })

  output$data <- renderPrint({
    str(results$data)
  })
}

shinyApp(ui, server)

라이센스