RMarkdown can Knit but cannot run: could not find function "read_csv" - r

This issue is really strange, I want to read a csv file and after getting rid of all unnecessary parts my entire code boils down to this two-liner:
library(tidyverse)
read_csv('data1.csv')
If I knit the Rmd file, it works and a new webpage opens as usual:
However, if I run it either by (1) clicking the green play button; or (2) clicking Run -> Run All button:
Then it just doesn't work (In case you are wondering whether or not there is a third line of code, I make the scope of the screenshot larger). The code is so short that I have no idea what could possibly be wrong.

Following the comment from #user12728748, I changed
read_csv('data1.csv')
to
readr::read_csv('data1.csv')
and it works! But this is still odd since my understanding is that suppose there aren't namespace conflicts prepending namespace is not needed.
Loading readr explicitly, regardless of the order, does not work since it is loaded by tidyverse already:

Related

Using VS Code with Bookdown

I have installed the R extension for VS Code. I am creating a book with bookdown, which is an extension of RMarkdown.
There is a Knit option (Ctrl + Shift + K). By default, it runs
rmarkdown::render("path/to/current/file", encoding = 'UTF-8').
[#CarlosLuisRivera pointed out that I had render_site here, rather than render before.]
In order to make bookdown work, I just need it to run
rmarkdown::render_site() or bookdown::render_book().
The encoding is the default, so that isn't needed. bookdown. Importantly, though, there shouldn't be a filename. I can't for the life of me work out how to adjust the settings to stop it from putting the filename in.
There is a setting r.rmarkdown.knit.command. This doesn't seem to do anything when the active file is called index.Rmd. Otherwise, whatever I write there gets appended with (path/to/current/file, encoding = 'UTF-8').
Even if I could get this fixed, I wanted to use VS Code for its preview—the RStudio in-built one is poor. When I try to preview, it can't compile anything, throwing an error:
The path C:/Users/USERNAME/Documents/Maths/Teaching/ST333/LectureNotes/libs/jquery-3.6.0 does not appear to be a descendant of C:/Users/USERNAME/vscode-R/tmp/.
The log also includes
output file: C:/Users/USERNAME/.vscode-R/tmp/index.knit.md
so I guess it's trying to make it in the wrong place.
Any help would be appreciated!

RDCOMClient log file

I have been using RDCOMClient for a while now to interact with vendor software. For the most part it has worked fine. Recently, however, I have the need to loop through many operations (several hundred). I am running into problems with the RDCOM.err file growing to a very large size (easily GBs). This file is put in C: with no apparent option to change that. Is there some way that I can suppress this output or specify another location for the file to go? I don't need any of the output in the file so suppressing it would be best.
EDIT: I tried to add to my script a file.remove but R has the file locked. The only way I can get the lock released is to restart R.
Thanks.
Setting the permissions to read only was going to be my suggested hack.
A slightly more elegant approach is to edit one line of the C code in the package in src/RUtils.h from
\#define errorLog(a,...) fprintf(getErrorFILE(), a, ##__VA_ARGS__); fflush(getErrorFILE());
to
\#define errorLog(a, ...) {}
However, I've pushed some simple updates to the package on github that add a writeErrors() function that one can use to toggle whether errors are written or not. So this allows this to be turned on and off dynamically.
So
library(RDCOMClient)
writeErrors(FALSE)
will turn off the error logging to the file.
I found a work around for this. I created the files C:\RDCOM.err and C:\RDCOM_server.err and marked them both as read-only. I am not sure if there is a better way to accomplish this, but for now I am running without logging.

roxygen2 not generating .Rd file

When I try to use roxygen2::roxygenize() in RStudio, I get an error message. The message I am coming across is this:
Warning message:
trainModel.Rd not generated by roxygen2. Skipped.
I have tried so much to fix this, but it just won't get fixed unless I remove all of the #' comments out (which would remove the purpose of using roxygen2 in the first place). I even took out everything except for the parts describing the title, description, and details and the error still persists (which makes me imagine that the error is in one of those three parts. But how?). My other document's Rd file gets generated just fine, but this one does not. Can somebody please help?
Try deleting the current Rd file.
I replicated this warning by creating file with your function, roxygenizing the code, and then removing the lines from the resulting Rd file
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/trainmode.R
It looks like roxygen2 doesn't want to overwrite files it didn't create. If you delete the current trainmode.Rd file, roxygen2 will generate a new .Rd file the next time roxygenize your code.

ESS & Knitr/Sweave: How to source the Rnw file into an interactive session?

This is a terribly simple request, and I can't believe I haven't found the solution to this yet, but I've been searching for it far and wide without luck.
I have an .Rnw file loaded up in Emacs, I use M-n s to compile it.
Everything works well, and it even opens an R buffer. Great. But that buffer
is entirely useless: it doesn't contain the objects that I just sourced!
Example minimal .Rnw file:
\documentclass{article}
\begin{document}
<<>>=
foo <- "bar"
#
\end{document}
Using M-n s, I now have a new R-buffer with a session loaded up, but:
> foo
Error: object 'foo' not found
That is disappointing. I would like to play around with the data interactively.
How do I achieve that? I don't want to be sourcing the file line-by-line, or
region-by-region with C-c C-c or something similar every time I change my code.
Ideally, it should be just like RStudio's source function, that leaves me with
a fully prepared R session.
I haven't tried this with sweave yet, only with knitr.
EDIT: the eval=TRUE chunk option does not seem to result in the correct behaviour.
This behaviour was recently changed in ESS. Now sweave and knitr are executed directly in the global environment, as if when you write it yourself at command line. So wait for a couple of more weeks till ESSv13.09 is out or use the development version.
Alternatively, you can also set ess-swv-processing-command to "%s(%s)" and you will get the same result, except automatic library loading.
For the record, knitr (in contrast to sweave) evaluates everything in it's own environment unless you instruct it otherwise.
[edit: Something went wrong. I don't see the correct .ess_weave any more. Probably some git commit messup again. So it is not fixed in 13.09. Fixing it now. Sorry.]
Open an interactive R session, and then call Sweave directly, I believe like this (untested though). knitr works in the same way, though you need to load the knitr library first.
> Sweave("yourfile.Rnw")
There is some potential for peril here, though. If you call Sweave in a session after doing other things, your code can use things previously in the workspace, thus making your results unreproducible.

Sweave syntax highlighting in output

Has anyone managed to get color syntax-highlighting working in the output of Sweave documents? I've been able to customize the output style by adding boxes, etc. in the Sweave.sty file as follows:
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontseries=bc,frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{frame=leftline}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontseries=bc}
And I can get the minted package to do syntax highlighting of verbatim-code blocks in my document like so:
\begin{minted}{perl}
use Foo::Bar;
...
\end{minted}
but I'm not sure how to combine the two for R input sections. I tried the following:
\DefineVerbatimEnvironment{Sinput}{minted}{r}
\DefineVerbatimEnvironment{Scode}{minted}{r}
Any suggestions?
Yes, look at some of the vignettes for Rcpp as for example (to pick just one) the Rcpp-FAQ pdf.
We use the highlight by Romain which itself can farm out to the hightlight binary by Andre Simon. It makes everything a little more involved---Makefiles for the vignettes etc pp---but we get colourful output from R and C/C++ code. Which makes it worth it.
I have a solution that has worked for me, I have not tried it on any other systems though so things may not work out of the box for you. I've posted some code at https://gist.github.com/797478 that is a set of modified Rweave driver functions that make use of minted blocks instead of verbatim blocks.
To use this driver just specify it when calling the Sweave function with the driver=RweaveLatexMinted() option.
Here's how I've ended up solving it, starting from #daroczig's suggestion.
\usepackage{minted}
\renewenvironment{Sinput}{\minted[frame=single]{r}}{\endminted}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{frame=leftline}
\DefineVerbatimEnvironment{Scode}{Verbatim}{}
While I was at it, I needed to get caching working because I'm using large data sets and one chunk was taking around 3 minutes to complete. So I wrote this zsh shell function to process an .Rnw file with caching:
function sweaveCache() {
Rscript -e "library(cacheSweave); setCacheDir(getwd()); Sweave('$1.Rnw', driver = cacheSweaveDriver)" &&
pdflatex --shell-escape $1.tex &&
open $1.pdf
}
Now I just do sweaveCache myFile and I get the result opened in Preview (on OS X).
This topic on tex.StackExchange might be interesting for you, as it suggest loading the SweaveListingUtils package in R for easy solution.

Resources