1

I'm working on making my CV with LaTeX, but I'm having an issue with including some R code.

I'm using knitr and have made a user command for TeXStudio as follows:

/Library/Frameworks/R.framework/Versions/3.2/Resources/bin/Rscript -e "knitr::knit2pdf('%.Rnw')"

If I'm just using base R then there's no problem:

\documentclass{article}
\begin{document}
<<>>=
n_cites = 44 + 7
@

I have \Sexpr{n_cites} citations.
\end{document}

But if I use the get_profile() function from the scholar package then I encounter an error.

Here's a MWE:

\documentclass{article}
\begin{document}
<<>>=
library(scholar)
prof = get_profile('IIMda24AAAAJ')
n_cites = prof$total_cites
print(n_cities)
@
\end{document}

And the error:

error

If I compile this same document in RStudio, everything runs fine.

How can this problem be solved?

4
  • What is the actual error you get? – Tyler Feb 22 '16 at 17:51
  • Question is updated with error message. Also, it's just scholar not cranlogs that leads to an error. – CephBirk Feb 22 '16 at 17:56
  • Hmm. If you drop the inline R code (\Sexpr), does the chunk itself work? Try adding print(n_cites) at the end of the chunk, maybe we'll get a clue. – Tyler Feb 22 '16 at 18:03
  • Good idea. That changed things. Updated question above. – CephBirk Feb 22 '16 at 18:07
1

Thanks to the answer at https://stackoverflow.com/questions/34347806/troubleshooting-r-script-called-from-rscript-exe

it's a very simple fix. I just need to add library(methods) to my code chunk.

\documentclass{article}
\begin{document}
<<>>=
library(methods)
library(scholar)
prof = get_profile('IIMda24AAAAJ')
n_cites = prof$total_cites
print(n_cites)
@
\end{document}

Apparently, while the methods package is automatically loaded in RStudio, it needs to be explicitly loaded for TeXstudio.

1
  • 1
    Actually, this is an issue with interactive vs batch mode, regardless of editor. From the Advanced R book: All S4 related code is stored in the methods package. This package is always available when you’re running R interactively, but may not be available when running R in batch mode. For this reason, it’s a good idea to include an explicit library(methods) whenever you’re using S4. – Tyler Feb 22 '16 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.