ååã¯ãesa API ã¨Pandocã使ã£ã¦ãShinyä¸ã§esaè¨äºãWordã¨PDFãã¡ã¤ã«ã«å¤æãããã¼ã«ãä½ãã¾ããã
ãã ããWordãã¡ã¤ã«ã®è¦ãç®ã¯ããã©ã«ãã®ã¾ã¾ã§ç¹ã«ããã£ã¦ãã¾ããã§ããã
ä»åã¯ããã³ãã¬ã¼ããã¡ã¤ã«ã使ããã¨ã§ããªãªã¸ãã«ã®ã¹ã¿ã¤ã«ã®Wordãã¡ã¤ã«ãä½æãã¦ã¿ã¾ãã
ä¾ãã°ãä¼ç¤¾ã§æå®ããããã©ã¼ãããã使ããªããã°ãªããªãå ´åãªã©ã«åèã«ãã¦ãã ããã
ãªããPDFã®ã¬ã¤ã¢ã¦ãããããã®ã¯ããªãé£ãããã ã£ãã®ã§ãä»åã¯Wordã®ã¿ã§ãã
æ§ææ¦è¦
æ§æã¯ååã¨ã»ã¼ä¸ç·ã§ãã使ç¨ããããããã«Web ã¢ããªã±ã¼ã·ã§ã³ã¨ãã¦ãR Shinyã使ã£ã¦ãã¾ãããç¹ã«æãã¯ãªãã§ãã
Shinyããã¦ã¼ã¶ã«ã¢ã¯ã»ã¹ãã¦ããããå
é¨ã§ã¯Pandocãå©ããã¨ã§WordãPDFãã¡ã¤ã«ãä½æãã¾ãã
ããã¦ãåååæ§ãã¹ã¦Dockerä¸ã§ä½æãã¦ããã¾ãã
å®æã¤ã¡ã¼ã¸
esaã®è¨äºidãå
¥åãããMarkdownãåå¾ããã¿ã³ãæ¼ãã¨ãè¨äºå
容ã®åå¾ãè¡ãã¾ãã
ï¼ããã¾ã§ã¯åååæ§ï¼
ãã®å¾ããã³ãã¬ã¼ãç¨ã®docxãã¡ã¤ã«ï¼ã¿ã¤ãã«ãæ¬æã»åè¦åºãã®ã¹ã¿ã¤ã«ãè¨å®ããããã®ï¼ãã¢ãããã¼ãããããã«æ²¿ã£ãã¹ã¿ã¤ã«ã§Wordãã¡ã¤ã«ãä½æãããã¨ãã§ãã¾ãã
ããã¦ããDownload WordãããDownload PDFããã¯ãªãã¯ãããã¨ã§ããã¦ã³ãã¼ãã§ãã¾ãã
ãªããã¦ã¼ã¶ã¼ããã³ãã¬ã¼ããã¡ã¤ã«ãç¨æããªãã¦ããã©ã«ãã®è¨å®ã§Wordãã¡ã¤ã«ãä½æã§ããããã«ã¯ãªã£ã¦ãã¾ãã
ä»åãã³ã¼ãã¯GIthubã«ç½®ãã¦ããã¾ãã
github.com
ãã³ãã¬ã¼ãã¨ã¯
docxãã¡ã¤ã«ãWordã§éãã¨ãåè¦åºããæ¬æã®ã¹ã¿ã¤ã«ãè¨å®ãããã¨ãã§ãã¾ãã詳ããã¯å ¬å¼ãµã¤ããåç §ãã¦ãã ããã
å¤ãã®æ¹ãä¸åº¦ã¯è¨å®ãããã¨ãããã¨æãã¾ãã
ãã®ããã«ãã©ã®è¦ç´ ã«ã©ããªã¹ã¿ã¤ã«ãé©ç¨ãããè¨å®ãããã¡ã¤ã«ãPandocã§ã¯ãã³ãã¬ã¼ãã¨å¼ãã§ãã¾ãã
Pandocã§ã¯ãå°ç¨ã®ãã³ãã¬ã¼ããã¡ã¤ã«ãç¨æãããã¨ã§ãããã«æ²¿ã£ãdocxãã¡ã¤ã«ãçæãã¦ããã¾ãã
ãã¼ã¹ã®ãã³ãã¬ã¼ãåå¾
Pandocç°å¢ãããã°ããã¼ã¹ã¨ãªããã³ãã¬ã¼ããã¡ã¤ã«ãç°¡åã«åå¾ãããã¨ãã§ãã¾ããååç«ã¡ä¸ããã³ã³ããã«ãã°ã¤ã³ãã¦ããã³ãã¬ã¼ã ãã¡ã¤ã«ãåå¾ãã¾ãããã
以ä¸ãå®è¡ããã¨ãã³ã³ããã«ãã°ã¤ã³ã§ãã¾ãã
ï¼XXXXXX é¨åã¯ç«ã¡ä¸ããã³ã³ããã®IDãæå®ãã¦ãã ãããï¼
$ docker exec -it XXXXXX /bin/bash
ãã°ã¤ã³ãã¦ããç¶æ ã§ã以ä¸ãå®è¡ããã¨ãreference.docx ã¨ããååã§ãã¼ã¹ã¨ãªã ãã³ãã¬ã¼ããã¡ã¤ã«ãçæããã¾ãã
$ pandoc --print-default-data-file reference.docx > reference.docx
ãã¡ãã®ãã¡ã¤ã«ããã¹ãå´ã«æã£ã¦ãã¦ãã ãããï¼ãããã¯ããã¦ã³ããã¦ãããã©ã«ãã§ä¸è¨ãå®è¡ãã¦ãã ãããï¼
ãã¡ã¤ã«ãéãã¨ã以ä¸ã®ãããªãµã³ãã«ã®ã¹ã¿ã¤ã«ãé©ç¨ããã¦ãããã¹ããè¨è¿°ããã¦ãã¾ãã
ä¾ãã°ããHeading 1ãã«ã¯ãWordã§ããè¦åºã1ã®ã¹ã¿ã¤ã«ãè¨å®ããã¦ãã¾ãã
ãã®è¦åºã1ã®ã¹ã¿ã¤ã«ãå¤æ´ããã¨ãesaå
ã§ã#ã1ã¤ããå§ã¾ãè¦åºãé¨åãå¤æ´ããã¾ãã
ã½ã¼ã¹ã³ã¼ãç´¹ä»
ãã¡ã¤ã«æ§æã§ãããååã®ãã®ã«ãshiny-src-template/ããã£ã¬ã¯ã㪠ãå ãã¦ä»¥ä¸ã®ããã«ãã¾ããã- shiny-src/ - esa_info.csv - ui.R - server.R - www/ - shiny-src-template/ - esa_info.csv - ui.R - server.R - www/ - reference.docx - docker-compose.yaml - Dockerfile
shiny-src-template/www/ ãã£ã¬ã¯ã㪠以ä¸ã«ãå ã»ã©åå¾ããreference.docxãç½®ãã¦ããã¦ãã ããã
Dockerfileã¨esa_info.csvã«ã¤ãã¦ã¯ååã¨åæ§ãªã®ã§å²æãããã®ä»ã以ä¸ç¯ã§èª¬æãã¦ããã¾ãã
docker-compose.yaml
docker-compose.yamlã¯ããã¦ã³ããã£ã¬ã¯ããªãä¸ã¤å¢ããã¦ä»¥ä¸ã®ããã«ãã¾ãããæçµè¡ã追å ããã¦ãã¾ãã
version: '3' services: esa-shiny: container_name: esa-shiny image: esa-shiny:1.0 ports: - 3838:3838 # environment: # - ESA_API_KEY=XXXXXXXXXXXXXXXX # - ESA_TEAM_NAME=XXXXXX volumes: - $PWD/shiny-src:/srv/shiny-server/esa - $PWD/shiny-src-template:/srv/shiny-server/esa-word-template
ui.Rã¨server.R
ä»åããåååæ§ ui.Rã¨server.Rã«åãã¦ãã¾ãã
ui.R
llibrary(shiny) shinyUI(fluidPage( titlePanel("esaè¨äºãWord or PDFã§åºå"), sidebarLayout( sidebarPanel( numericInput("numericInput_data", "esaã®è¨äºIDãå ¥å", min = 1, value = 1), actionButton("render_md", "Markdownãåå¾"), br(), h3("ãã³ãã¬ã¼ãç¨ã®ãã¡ã¤ã«ãã¢ãããã¼ããã¦ãã ãã"), a(href="reference.docx", target="blank", "ãµã³ãã«ã®ãã³ãã¬ã¼ããã¡ã¤ã«", download="reference.docx"), p("ä¸ã®ãã¡ã¤ã«ããã¦ã³ãã¼ããã¦ãWordã§éãè¦åºãçã®ãã©ã³ããå¤æ´ãã¦ãã ãã"), fileInput("template", "Choose Template Docx File", multiple = FALSE, accept = c(".docx")), uiOutput("download_files") ), mainPanel( verbatimTextOutput("text") ) ) ))
ååã¨ã®éãã¨ãã¦ãã¦ã¼ã¶ã¼ãä½æãããªãªã¸ãã«ã®ãã³ãã¬ã¼ããã¡ã¤ã«ãå
¥åã§ããããã«ãfileInput()é¢æ°ã使ã£ã¦ãã¾ãã
fileInputã§ãã¡ã¤ã«ãåãåãããã®å
容ãå
ã«Pandocã§docxãã¡ã¤ã«ãä½æãã¾ãã
fileInputã«ã¤ãã¦ã¯ã以ä¸æ¸ç±ã
Rã¨Shinyã§ä½ãWebã¢ããªã±ã¼ã·ã§ã³
- ä½è :æ¢ æ´¥ éä¸,ä¸é è²´åº
- çºå£²æ¥: 2018/11/07
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
www.randpy.tokyo
server.R
library(shiny) library(httr) library(dplyr) esa_info <- read.csv("esa_info.csv") headers <- httr::add_headers(`Authorization` = paste("Bearer", esa_info[1, 1]), `Content-Type` = "application/json") team_name <- esa_info[1, 2] shinyServer(function(input, output, session) { output$text <- renderText({ paste(markdown_text()) }) markdown_text <- reactive({ input$render_md input_id <- isolate(input$numericInput_data) api_result <- GET("http://api.esa.io", path = paste("v1/teams", team_name, "posts", input_id, sep = "/"), config = headers) if (api_result %>% status_code() != 200){ return(paste("API Error:", api_result %>% status_code())) } contents <- api_result %>% content() write(contents$body_md, paste("www/sample_", input_id, ".md", sep="")) if (reference_word_file() == ""){ reference_word_file_path <- "www/reference.docx" } else { reference_word_file_path <- reference_word_file() } system(paste("pandoc -f markdown -t docx www/sample_", input_id, ".md -o www/sample_", input_id, ".docx --reference-doc=", reference_word_file_path, sep="")) system(paste("pandoc -t latex www/sample_", input_id, ".docx -o www/sample_", input_id, ".pdf --pdf-engine=xelatex -V documentclass=bxjsarticle -V classoption=pandoc", sep="")) return(contents$body_md) }) reference_word_file <- reactive({ inFile <- input$template if (is.null(inFile)){ return("") } return(inFile$datapath) }) output$download_files <- renderUI({ input_id <- input$numericInput_data tagList( h3(tags$a(href=paste("sample_", input_id, ".docx", sep=""), target="blank", "Download Word", download=paste("sample_", input_id, ".docx", sep=""))), h3(tags$a(href=paste("sample_", input_id, ".pdf", sep=""), target="blank", "Download PDF", download=paste("sample_", input_id, ".pdf", sep=""))), ) }) })
ååã¨ã®éãã¨ãã¦ãui.Rãããã³ãã¬ã¼ããã¡ã¤ã«ãåãåããããã«ã以ä¸ã®å¦çã追å ãã¦ãã¾ãã
reference_word_file <- reactive({ ... })
ã¾ããpandocã®ã³ãã³ãã«ãã--reference-doc=ããªãã·ã§ã³ãä»ãã¦ãã¾ãã
ããããããã¨ã§ãã¦ã¼ã¶ã¼ãç¨æãããã³ãã¬ã¼ããã¡ã¤ã«ãé©ç¨ããã¾ãã
ã³ã³ããèµ·åæ¹æ³
ã³ã³ããã®èµ·åæ¹æ³ã¯ååã¨åæ§ã§ã以ä¸ãå®è¡ããã ãã§ãã$ docker-compose up -d
ãã©ã¦ã¶ã§ããlocalhost:3838/esa-word-templateããããã㯠ãä»èµ·åãã¦ãããã½ã³ã³ã®IPã¢ãã¬ã¹:3838/esa-word-templateãã«ã¢ã¯ã»ã¹ï¼ä¾ 192.168.1.100:3838/esa-word-templateï¼ãã¦ãã ããã
ãªãªã¸ãã«ã®ãã³ãã¬ã¼ããã¡ã¤ã«ãç¨æããçæãããWordãã¡ã¤ã«ãå¤åãããã¨ã確èªãã¦ãã ããã
ã¾ã¨ã
ã¨ãããã¨ã§ãååã«å¼ãç¶ããesaã®è¨äºãAPIçµç±ã§åå¾ãã¦ãWordãPDFãã¡ã¤ã«ãçæãããã¼ã«ãä½ã£ã¦ã¿ã¾ããããã¹ã¦Dockerå ã«å®çµããã¦ããã®ã§ãå°å ¥ããããã®ã§ã¯ã¨æãã¾ãã
ãã¼ãºã¨ãã¦çãããããã¾ããããå²ã¨ãã®ãããªãã¼ã«ããã£ã¨æ¬²ããã£ãã¨ãã人ãå¤ãã®ã§ã¯ã¨æãã¾ãã
ãããããããã¼ã¹ã«ä½ãå¥ã®ãã¼ã«ãä½ã£ã¦ã¿ããªã©ãã£ãããæ¯éæãã¦ãã ããã