記錄:篩選寫書工具的艱苦經(jīng)歷簡要記錄的自己嘗試幾種工具的艱苦努力省艳,最后的結(jié)論是"Word + Calibre" 制造常用的電子書(DOCX, PDF, EPUB捂贿,MOBI和AWZ3等)還是夠用的纠修。
自己制作本地的Jupyter Book簡單介紹了如何使用 Jupyter Book create
和 build
生成Web 書(當(dāng)時是以 Python 語言為例)。
使用R BookDown 來寫書 - PDF, EPUB和Web版厂僧。還是算了吧!記錄了使用 R Bookdown 制作 PDF, EPUB和Web 書的經(jīng)歷扣草。不過,R Bookdown 生成的 Web 書不支持交互式算法展示 - PageDown 想完成這一點颜屠。調(diào)試過程中的麻煩真是不能忍受 - 絕對不比寫書得麻煩來的少辰妙!
想著 看到過 Jupyter Notebook 支持很多語言,所以甫窟,進(jìn)一步試一試 Jupyter NoteBook 如何支持R密浑。
需要設(shè)置Jupyter Notebook 以包含 R kernel
需要提前安裝好 R 和 R Studio
建議將Conda 包源改成國內(nèi)的源
在Conda Prompt 中conda install -c r r-irkernel
安裝 R Kernel
進(jìn)入 R CLI,運行IRkernel::installspec(user=FALSE)
以設(shè)定全部用戶可用
運行 Jupyter NoteBook粗井,可以看到有了R Kernel 的支持
試試尔破,支持了 R 代碼 – 簡單的可以
但是,調(diào)用library 就不行了
試了很多方式浇衬,不是前面沒安裝好懒构,其實就是沒有那些library而已
在 R Console 里走一遍那些命令
install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))
過程中彈出個窗口讓你選擇mirror – 我選了lanzhou。然后耘擂,很快就安裝完了
devtools::install_github('IRkernel/IRkernel') – 依賴Git 程序
安裝完“Git-2.25.0-64-bit.exe”胆剧,再運行此命令就可以了
IRkernel::installspec()
和IRkernel::installspec(user = FALSE)
都沒成 – 就算了
簡單的R 命令沒問題,加載包就不行了梳星。
開始還以為是加載 R Kernel 進(jìn) Jupyter Notebook
http://www.reibang.com/p/8b90c2f12856
library("ggplot2")
ggplot(data = mtcars, aes(x = wt, y = mpg, color = cyl)) + geom_point() +
geom_smooth(method="lm") +
labs(main="Regression of MPG on Weight",
xlab="Weight", ylab="Miles per Gallon")
調(diào)用 library()
赞赖,彈出了已安裝的包滚朵,里面確實沒有 ggplot2
倒是有 datasets 包
在 Jupyter NoteBook 中試下library(datasets) head(iris) – 可以的
也就是在 R 中能運行的冤灾,在Jupyter Notebook 的 R kernel 中也能正常運行
查看R 支持的包是關(guān)鍵。那么辕近,怎么添加一下包呢韵吨?
Data.table 和 Ggplot2 的問題的解決 – 就是沒有安裝那些包而已
install.packages('data.table')
install.packages('scorecard')
就順帶安裝了ggplot2
不過,上面是在R 程序里加載了 ggplot2, data.table等移宅,還需要在 Anaconda 的 Jupyter 中的 R kernel 中也加載
在 R 程序加載了scorecard包后归粉,Jupyter 的 R kernel 中是沒有 的
還得在 Anaconda Jupter 中也加載一遍
然后椿疗,不用再啟動 Jupyter 就能使用那些包了
我按照前面最開始安裝的 R Kernel 是 3.6.1 版本。而由上面的截圖看糠悼,data.table 是 R 3.6.3 的届榄。不過,仍然可行
library(data.table)
head(iris)
summary(iris)
試試 Jupyter Book 來寫R編程書 - 靜態(tài)Web書沒問題倔喂;交互铝条?夠嗆!
開始應(yīng)該還是 Jupyter Book 生成Demo
然后修改其中的配置和ipynb文件 - 不過席噩,這時ipynb要New R Notebook
- 然后執(zhí)行一遍
Jupyter Book Build
班缰,進(jìn)而 bundle exec jekyll serve
- 生成的靜態(tài)Web還是不錯的(跟Notebook的效果一致)
但是,R代碼交互就不成了 - "Waiting for kernel" ...
應(yīng)該是 Jupyter Book 默認(rèn)只是連接Python kernel. R Kernel? 不知道呢悼枢!
查了很久埠忘,沒有找到解決辦法,覺得有必要提交給Jupyter Book 的開發(fā)人員 - 不過馒索,不確定是否人家解決了
了解到: 有將ipynb提交至在線Jupyter Notebook Hub 或第三方的方案莹妒,不過,畢竟還是效率很低
問題1: 前面的 open(xxx,'rb')
解決中文問題
問題2: 就是前面指出的"Jupyter Book不能使用R Kernel"
Jupyter Notebook 甚至支持同一個ipynb文件有不同語言的代碼運行 - rpy2
- 備存
rpy2
https://cloud.google.com/ai-platform/notebooks/docs/r-python-same-notebook
Create a notebook for use with R and Python
To use rpy2 to work with both R and Python in the same notebook, create a Python 3 notebook. To create the notebook:
-
Go to the AI Platform Notebooks page in the Google Cloud Console.
Select Open JupyterLab for the R instance that you want to open.
-
Select File -> New -> Notebook. Select the Python 3 kernel for your new notebook.
Select File -> Rename notebook and change the name of the untitled notebook to something meaningful, such as "rpy2.ipynb."
The notebook is ready for you to import rpy2 and use R and Python in the same workbook.
Use rpy2 to import R objects
To import R objects with rpy2:
-
In the notebook's first code cell, enter the following:
import rpy2.robjects as robjects
.Enter the import statement in the first cell -
Click the run button. Python imports rpy2's functions for accessing and manipulating R objects.
The run button -
To add a code cell to the notebook, click the notebook's + button.
The + button In the new code cell, enter the following:
pi = robjects.r['pi']
.Click the run button. Python stores an R pi object.
-
To print the value of pi, in a new code cell, enter
pi[0]
and click the run button.pi = robjects.r['pi'] pi[0]
Example
https://stackoverflow.com/questions/39008069/r-and-python-in-one-jupyter-notebook
Yes, it is possible! Use rpy2.
You can install rpy2 with: pip install rpy2
Then run %load_ext rpy2.ipython
in one of your cells. (You only have to run this once.)
Now you can do the following:
Python cell:
# enables the %%R magic, not necessary if you've already done this
%load_ext rpy2.ipython
import pandas as pd
df = pd.DataFrame({
'cups_of_coffee': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
'productivity': [2, 5, 6, 8, 9, 8, 0, 1, 0, -1]
})
R cell:
%%R -i df -w 5 -h 5 --units in -r 200
# import df from global environment
# make default figure size 5 by 5 inches with 200 dpi resolution
install.packages("ggplot2", repos='http://cran.us.r-project.org', quiet=TRUE)
library(ggplot2)
ggplot(df, aes(x=cups_of_coffee, y=productivity)) + geom_line()
And you'll get your pretty figure plotting data from a python Pandas DataFrame.
小結(jié)
R, RStudio 和 Jupyter R Kernel 是相互獨立的
安裝完畢后绰上,Jupyter Notebook 對R的支持跟R Project 是一樣的
使用 Jupyter Book 寫 R 算法書:
靜態(tài)Web版 - 跟Jupyter Notebook是一致的
但是动羽,不能運行Web上嵌入的R代碼 - 而嵌入Python時是可以的,雖然花的時間長些
關(guān)于寫書和寫論文
到目前為止渔期,寫書和論文的工具真是用了很多了运吓,有如下的總結(jié):
寫書 - PDF, EPUB, AZW3, MOBI
我覺得還是 Word + Calibre 就可以了。
- 活用 Word 的大綱模式疯趟,就能夠編纂頁數(shù)很多的書(我編纂過僅2000頁的書拘哨,沒有問題)
- 要解決很多自動問題:圖、表和公式的自動編碼和引用信峻,參考文獻(xiàn)的自動引用等(已解決倦青,另文撰述)
- Word 本身對PDF 的支持就很不錯。
- 使用Calibre 導(dǎo)入 Word 文檔盹舞,經(jīng)過一些簡單的設(shè)置就能夠?qū)С龈袷讲诲e的EPUB产镐,AZW3, MOBI等(參看)
不足的就是不能生成算法交互的Web。
寫書 - 算法交互的Web書
覺得 Jupyter NoteBook 就可以了:能夠支持很多語言踢步。要做的就是講對應(yīng)語言的Kernel 加載進(jìn) Jupyter Notebook癣亚。
論文 - Word + LaTeX
- 中文論文,還是使用Word 吧获印。主要的挑戰(zhàn)就是要解決圖述雾、表、公式的自動編號和引用,以及參考文獻(xiàn)的格式和引用玻孟。
- 英文論文唆缴,自然是
不可。