I created a local R package using roxygen2 and devtools. It seem work well but I would like to know how I get the complete PDF documentation then. Since the DESCRIPTION and .Rd files are created by roxygen2 automatically, I'd think there's a easy way to translate them into a single PDF document.
I think there's a command here for you: R CMD Rd2pdf. From its help:
edd#max:~$ R CMD Rd2pdf --help
Usage: R CMD Rd2pdf [options] files
Generate PDF output from the Rd sources specified by files, by
either giving the paths to the files, or the path to a directory with
the sources of a package, or an installed package.
Unless specified via option '--output', the basename of the output file
equals the basename of argument 'files' if this specifies a package
or a single file, and 'Rd2' otherwise.
The Rd sources are assumed to be ASCII unless they contain \encoding
declarations (which take priority) or --encoding is supplied or if using
package sources, if the package DESCRIPTION file has an Encoding field.
The output encoding defaults to the package encoding then to 'UTF-8'.
Files are listed in the order given: for a package they are in alphabetic
order of the \name sections.
Options:
-h, --help print short help message and exit
-v, --version print version info and exit
--batch no interaction
--no-clean do not remove created temporary files
--no-preview do not preview generated PDF file
--encoding=enc use 'enc' as the default input encoding
--outputEncoding=outenc
use 'outenc' as the default output encoding
--os=NAME use OS subdir 'NAME' (unix or windows)
--OS=NAME the same as '--os'
-o, --output=FILE write output to FILE
--force overwrite output file if it exists
--title=NAME use NAME as the title of the document
--no-index don't index output
--no-description don't typeset the description of a package
--internals typeset 'internal' documentation (usually skipped)
The output papersize is set by the environment variable R_PAPERSIZE.
The PDF previewer is set by the environment variable R_PDFVIEWER.
Report bugs at bugs.r-project.org .
edd#max:~$
Related
I am working on a package in R, and am looking for a way to document the sources of external data stored in the inst/extdata folder. I know that data in the /data folder can be documented with roxygen as per this SO post.
The trouble seems to be that external data is not exported into the namespace of the package, and therefore tying an roxygen help document to it poses an issue. Is there a way to overcome this and to document external data similar to the method for items in the /data folder?
Here is the reproducible (on linux) example of solution I provided in comments.
Code below will create minimal package
defining dummy DESCRIPTION file
example ext/data/data1.csv csv data file
example man/data1.Rd R documentation file
Then it will build and install package.
mkdir -p my.pkg
cat > my.pkg/DESCRIPTION <<EOL
Package: my.pkg
Type: Package
Title: My pkg
Version: 1.0.0
Description: Example my pkg.
License: GPL-3
EOL
mkdir -p my.pkg/inst/extdata
cat > my.pkg/inst/extdata/data1.sv <<EOL
a,b,c
1,a,2.5
2,b,5.5
EOL
mkdir my.pkg/man
cat > my.pkg/man/data1.Rd <<EOL
\name{data1}
\alias{data1}
\alias{data5}
\title{
my data
}
\description{
desc of data.
}
EOL
R CMD build my.pkg
R CMD INSTALL my.pkg_1.0.0.tar.gz
Now see that manual can be found
Rscript -e 'library(my.pkg); ?data1'
Rscript -e 'library(my.pkg); ?data5'
As you can see we can refer to documentation of extdata using any name defined in alias inside Rd file.
I am trying to run R scripts via BAT files on Windows Command Prompt.
The scripts require a few R packages such as data.table, tidyR, etc.
For operational reasons, all required R packages and dependencies (including data.table) are installed at C:\Users\username\Documents\R\R-3.5.1\library. I am not allowed to install RStudio in this environment.
When I try
"C:\Program Files\R\R-3.5.1\bin\x64\Rscript.exe" script.R, I get an error similar to
Error in library(data.table) : there is no package called 'data.table'
Execution halted
How can I set the .libPaths via Command Prompt to point to the correct location of the packages (i.e. to C:\Users\username\Documents\R\R-3.5.1\library)?
Thank you in advance.
Disclaimer: I'm unfamiliar with R.
From R: Search paths :
The library search path is initialized at startup from the environment
variable R_LIBS (which should be a colon-separated list of directories
at which R library trees are rooted) followed by those in environment
variable R_LIBS_USER. Only directories which exist at the time will be
included.
By default R_LIBS is unset, and R_LIBS_USER is set to directory
‘R/R.version$platform-library/x.y’ of the home directory (or
‘Library/R/x.y/library’ for CRAN macOS builds), for R x.y.z.
An environment variable can be created with set VARIABLE_NAME=YOUR_VALUE batch command.
So your batch file should probably be something like this:
cd /d "C:\INSERT_PATH_TO_DIRECTORY_CONTAINING_script.R"
set "R_LIBS=C:\Users\username\Documents\R\R-3.5.1\library"
"C:\Program Files\R\R-3.5.1\bin\x64\Rscript.exe" script.R
However for portability reasons (let's say a collegue asks for a copy of your script or your computer dies) I suggest putting the script, R library and batch file in a single directory, let's say C:\Users\username\Documents\R. The batch file C:\Users\username\Documents\R\script.bat becomes:
cd /d "%~dp0"
set "R_LIBS=%~dp0R-3.5.1\library"
"%PROGRAMFILES%\R\R-3.5.1\bin\x64\Rscript.exe" "%~dpn0.R"
%PROGRAMFILES% environment variable expands to full path of program files folder, %~dp0 parameter expands to full path of a directory that holds your batch file, and %~dpn0 is a batch-file full path without extension.
Notice that %~dp0R-3.5.1 is not a typo because %~dp0 includes trailing backslash.
This way you can copy C:\Users\username\Documents\R to D:\Users\SOMEOTHERNAME\Documents\R and the script will still run.
If you create another version of your script, just copy the batch file so that it has same filename as your script but .bat extension instead of .R and it should call the new script - this has proven to be very handy when debugging and distributing scripts.
Alternatively, if you would rather install libraries separately you may want to use %HOMEDRIVE%%HOMEPATH% which expands to C:\Users\username.
Extracting proper Documents folder path, as well as R installation path is possible but requires reading the registry and thus is a bit more complicated.
I am trying to use the Treetag function in the koRpus package.
The code I have used is
tagged.text <-treetag("C:/Rec_By_Others.txt",treetagger="manual",lang="en",TT.options=list(path="C:\\Program Files\\TreeTagger", preset="en"))
But I keep encountering with the following error.
Error in matrix(unlist(strsplit(tagged.text, "\t")), ncol = 3, byrow = TRUE, :
'data' must be of a vector type, was 'NULL'
What do I do ?
Your code seems correct to me, but I had the same error message. I could not find any solution for this problem until today. I finally found that I had a problem with the PERL installation, so I reinstalled a new version of PERL. Then, I checked if TreeTagger worked properly by applying the README TreeTagger instruction, that is:
Installation
Install a Perl interpreter (if you have not already installed one). You can download a Perl interpreter for Windows for free at http://www.activestate.com/activeperl/
Extract the zip file (if it was not extracted yet) and move the TreeTagger directory to the root directory of drive C:.
Download the parameter files for the languages you need, decompress them (e.g. using Winzip or 7zip) and move them to the subdirectory TreeTagger/lib. Rename the parameter files to -utf8.par Example: Rename french-par-linux-3.2-utf8.bin to french-utf8.par Non-UTF8 parameter files are not supported anymore.
Add the path C:\TreeTagger\bin to the PATH environment variable. The necessary steps differ from one Windows version to the other.
Open a command prompt window and type the command set PATH=C:\TreeTagger\bin;%PATH%
Go to the directory C:\TreeTagger cd c:\TreeTagger
Now you can test the tagger, e.g. by analyzing this file with the command tag-english INSTALL.txt If you install the TreeTagger in a different directory, you have to modify the first path in the batch files tag-*.bat using an editor such as Wordpad.
Note also that:
if you install the TreeTagger in a different directory, you have to
modify the first path in the batch files tag-.bat using an editor
such as Wordpad.
I hope this help.
Every package on CRAN seems to provide a reference manual but in contrast to vignettes they do not seem to get downloaded or built when installing packages. Can this be done so that I can access them from the command line in R or can I only access them on CRAN?
From a given package's sources, you can build this via R CMD Rd2pdf:
edd#max:~$ R CMD Rd2pdf --help
Usage: R CMD Rd2pdf [options] files
Generate PDF output from the Rd sources specified by files, by
either giving the paths to the files, or the path to a directory with
the sources of a package, or an installed package.
Unless specified via option '--output', the basename of the output file
equals the basename of argument 'files' if this specifies a package
or a single file, and 'Rd2' otherwise.
The Rd sources are assumed to be ASCII unless they contain \encoding
declarations (which take priority) or --encoding is supplied or if using
package sources, if the package DESCRIPTION file has an Encoding field.
The output encoding defaults to the package encoding then to 'UTF-8'.
Files are listed in the order given: for a package they are in alphabetic
order of the \name sections.
Options:
-h, --help print short help message and exit
-v, --version print version info and exit
--batch no interaction
--no-clean do not remove created temporary files
--no-preview do not preview generated PDF file
--encoding=enc use 'enc' as the default input encoding
--outputEncoding=outenc
use 'outenc' as the default output encoding
--os=NAME use OS subdir 'NAME' (unix or windows)
--OS=NAME the same as '--os'
-o, --output=FILE write output to FILE
--force overwrite output file if it exists
--title=NAME use NAME as the title of the document
--no-index do not index output
--no-description do not typeset the description of a package
--internals typeset 'internal' documentation (usually skipped)
--build_dir=DIR use DIR as the working directory
The output papersize is set by the environment variable R_PAPERSIZE.
The PDF previewer is set by the environment variable R_PDFVIEWER.
Report bugs at bugs.r-project.org .
edd#max:~$
I am writing a package in R and I would appreciate some example bash code to process the Rd files to latex and then to pdf.
It is in the directory ~/mypkg/dev/. I have generated the file structure and Rd templates.
from ~/mypkg/dev/man, I have tried
R CMD Rdconv -o mypkg-package.tex --type=latex mypkg-package.Rd
mypkg-package.tex file is generated, but
pdflatex mypkg-package.tex
generates tex without any preamble.
I have read the documentation in "Writing R extensions" and "R CMD Rdconv --help" on this subject, but no examples are provided.
Thank you
There are two issues here:
First, the Rdconv command only transforms one Rd file at a time; your question suggests that you want the full manual.
Second, the Rd2dvi command is your friend. I just ran the following on a local package:
R CMD Rd2dvi --pdf --title='Test of foo' -o /tmp/foo.pdf man/*.Rd
That should be what you asked for.
Try this. It worked for me.
found from Making an R package PDF manual using devtools
pack <- "name_of_your_package"
path <- find.package(pack)
system(paste(shQuote(file.path(R.home("bin"), "R")),"CMD", "Rd2pdf", shQuote(path)))
0. From question, it is assumed that you have .Rd files.
You may have obtained these .Rd files via roxygen package or some other ways. You need .pdf of your package. One way to do this is from Windows's command line.
1. Open Windows Command Prompt (in Administrator mode, if possible).
Start - type "cmd" - (optional: right click the appearing icon - Run as administrator)
2. Pass to the R's current working directory (it can be found via "getwd()" in R's console) in the command propmt. R's current working directory contains the folder with your package source.
cd C:\Users\erdogan\Documents\Revolution
3. For the sake of argument, say the package folder was called "causfinder". Run the following command in Windows command prompt:
(be sure that you have not got any causfinder.pdf in R's working directory (You may have obtained some R-development-incompatible causfinder.pdf with some other ways outside R). If there exists, delete causfinder.pdf first.
Otherwise, you get this error: "file 'causfinder.pdf' exists; please remove it first")
R CMD Rd2pdf causfinder/
This command performs .Rd --> LateX --> .pdf process automatically. You obtain causfinder.pdf in R's working directory.
This is further described in the manual on Writing R Extensions under the section on "Processing documentation files"
I have created a basic bash function, so I can just run rdoc from my command line. It generates the Rd files, creates the pdf, and opens it. I only use it for testing, not for creating the final documentation. You can add it by adding the following function to your .bashrc (or whichever you use) file
rdoc() {
echo -e "devtools::document()" | R --no-save
rm /tmp/rdoc.pdf
R CMD Rd2pdf -o /tmp/rdoc.pdf man/*.Rd
open /tmp/rdoc.pdf
}