--- disqus: ahb0222 GA : G-VF9ZT413CG --- # R使用Quarto自動化生成報告_以Word為例  > [color=#40f1ef][name=LHB阿好伯, 2022/06/27][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `R` `Word` [TOC] # Quarto Quarto是一個基於Pandoc構建的文件生成套件 是RMarkdown的下一代版本 與RMD有許多相同的功能 可以使用 Python、R、Julia 和 Observable 建立動態內容 MarkDown或 Jupyter notebook 製作文件 以 HTML、PDF、MS Word、ePub 等格式發佈高品質的文章、報告、演示文稿、網站和書籍 使用MarkDown進行包括方程式、引文、交叉引用、圖形面板、標註、高級佈局 https://quarto.org/docs/tools/rstudio.html 最近研究過後來分享一些使用心得 # 安裝說明 https://quarto.org/docs/get-started/hello/rstudio.html ```r install.packages("tidyverse") install.packages("palmerpenguins") ``` ## 建立範本 Quarto支援使用既有Word文件作為樣式的範本 可以用以下程式生成一個範本文件 ``` quarto pandoc -o custom-reference-doc.docx \ --print-default-data-file reference.docx ``` 依照需求修改Word中的樣式 在上傳到工作資料夾中  YAML中加入範本依據的語法去定義使用的檔案 :::success ``` YAML header 一個(可選)YAML 標頭,兩端由三個短劃線分隔。--- ``` ::: ```yaml --- format: docx: reference-doc: /cloud/project/custom-reference-doc.docx --- ``` # Quarto 建立 MS Word 輸出選項 https://quarto.org/docs/reference/formats/docx.html 添加`format: docx`定義輸出為Word 也可以用來定義檔案基本資訊 最後會生成一簡單封面 ```yaml --- format: docx title: 文件標題 date: 文件日期 author: 文件的作者 --- ``` # 代碼塊_執行程式區 Quarto一樣支援執行R跟Python程式甚至是Julia、Observable JS都可以執行  ## 輸出選項 在Quarto 中其輸出定義是在代碼塊頂部輸入特殊註解  更多詳細定義可以查看 https://quarto.org/docs/reference/formats/docx.html # 交叉引用 MarkDown中定義 `{#label}`定義標籤名稱 ``` {#fig-elephant} ``` 代碼塊中定義 `#| label: XXX` 定義標籤名稱 MarkDown內文中引用 `@XXX` 引用標籤 ## 表格 表格預設是以knitr::kable()作為輸出 所以輸出設定就需要參考knitr的說明 https://bookdown.org/yihui/rmarkdown-cookbook/kable.html ```r #| echo: false #| warning: false #| df-print: kable #| label: tbl-1 #| tbl-cap: "各月份氣象平均" ```  ## 圖片 可以直接使用Base Plot 或是ggplot輸出 需要注意的就是輸出的尺寸 需要依自己需求在定義好 無法像是在Rstudio中直接拖拉大小 ``` #| echo: false #| warning: false #| fig-width: 8 #| fig-height: 6 #| fig-dpi: 100 #| label: fig-1 #| fig-cap: "四季風花圖" ``` ## 下一頁 添加`{{< pagebreak >}}`等同於Word的下一頁 {{< pagebreak >}} 簡單的使用Quarto簡單測試一下 直接從中央氣象局爬取十年來的氣象資料 中間並不需要再額外調整即可生成以下文件  >參考資料 https://ardata-fr.github.io/officeverse/index.html https://ardata-fr.github.io/officeverse/officedown-for-word.html https://bookdown.org/yihui/rmarkdown/word-document.html https://rmarkdown.rstudio.com/articles_docx.html 🌟 🌟全文可以至下方連結觀看或是補充 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) 
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.