
我正在使用 LaTeX 製作我的簡歷,但在包含一些 R 程式碼時遇到問題。
我正在使用 TeXStudioknitr
並為 TeXStudio 創建了一個用戶命令,如下所示:
/Library/Frameworks/R.framework/Versions/3.2/Resources/bin/Rscript -e "knitr::knit2pdf('%.Rnw')"
如果我只使用基礎 R 那麼沒有問題:
\documentclass{article}
\begin{document}
<<>>=
n_cites = 44 + 7
@
I have \Sexpr{n_cites} citations.
\end{document}
但是,如果我使用套件get_profile()
中的函數scholar
,則會遇到錯誤。
這是一個 MWE:
\documentclass{article}
\begin{document}
<<>>=
library(scholar)
prof = get_profile('IIMda24AAAAJ')
n_cites = prof$total_cites
print(n_cities)
@
\end{document}
和錯誤:
如果我在 RStudio 中編譯相同的文檔,一切都會正常運作。
如何解決這個問題呢?
答案1
感謝您的回答https://stackoverflow.com/questions/34347806/troubleshooting-r-script- Called-from-rscript-exe
這是一個非常簡單的修復。我只需要添加library(methods)
到我的程式碼區塊中。
\documentclass{article}
\begin{document}
<<>>=
library(methods)
library(scholar)
prof = get_profile('IIMda24AAAAJ')
n_cites = prof$total_cites
print(n_cites)
@
\end{document}
顯然,雖然該methods
套件在 RStudio 中自動加載,但在 TeXstudio 中需要明確加載。