I would like to be able to open up an .Rd documentation file and preview it in R.
For example, I can create a data documentation file using promptData:
df <- data.frame(var1=1:5,var2=6:10)
promptData(df,filename = "df_doc.Rd")
which will produce a documentation file "df_doc.Rd" in the working directory.
In order to preview this file, I can open it up in the RStudio editor and then hit "Preview", which will open up df_doc properly formatted in the Help window. However, I'd like to be able to do that with code rather than having to open up the file and hit the Preview button in the RStudio GUI. Something like a preview("df_doc.Rd") function.
I'm aware that there are ways to 'install' the documentation files so R knows where to find them. But I'm writing some code that will generate these files automatically and preview them (hopefully without having to load in the dev tools that install the documentation files), so I'm specifically hoping to be able to preview these directly from file. Is that possible?
Man, the documentation for this one was pretty well hidden! To be fair, "Rd" isn't exactly Googleable, nor is documentation about documentation. But I managed to scrounge it up.
What I've been looking for is the
previewRd('df_doc.Rd')
command in the rstudioapi library. Unfortunately, this only works in RStudio, so if I want it to be generally usable I'll need to write HTML directly instead of Rd and open that in a browser.
According to 'Writing R Extensions', run:
R CMD Rdconv -t html filename.Rd > filename.html
in the command line. See also:
R CMD Rd2pdf --help
In R: system("R CMD Rdconv -t html filename.Rd > filename.html | chromium-browser")
Related
I'm trying to use the SemiMarkov package and I want to change one small line of code in there. I've done some digging via:
getAnywhere("semiMarkov")
& I've identified that I want to change this line:
hessian <- diag(ginv(hessian(V, solution)))
to try something like:
hessian <- diag(ginv(pracma::hessian(V, solution)))
How do I go about this? Do I need to rebuild the package from scratch, and if so do I need rTools etc for this, or is there a simple-ish workaround (I'm a relevant R novice)? I've done some searching online and can't find anything obvious. Any ideas/pointers gratefully appreciated.
If you'd like to simply test out the effect of that change in an interactive R session, you can do so using trace(). Here's how:
Type trace("semiMarkov", edit=TRUE)
In the text editor that that launches, edit the line of interest.
Save the modified file.
Close the text editor
Back in R, use the modified function.
Linux environment
Starting with downloading the package source from CRAN.
This is the landing page: https://cran.r-project.org/web/packages/SemiMarkov/index.html
This is the package source: https://cran.r-project.org/src/contrib/SemiMarkov_1.4.2.tar.gz
Download and extract the source:
wget https://cran.r-project.org/src/contrib/SemiMarkov_1.4.2.tar.gz
tar -xvzf SemiMarkov_1.4.2.tar.gz
This should result in a directory named SemiMarkov. Open up the source (cd SemiMarkov), and modify as necessary.
Next, build the changes:
cd ..
R CMD build SemiMarkov/
This will result in a new archive file named SemiMarkov_1.4.2.tar.gz.
Lastly, install your modified archive:
R CMD INSTALL SemiMarkov_1.4.2.tar.gz
Windows environment
I'm less familiar with the Windows platform. *nix tooling is available in Cygwin, but it's painful. Instead, as Josh O'Brien points out, you should follow the Windows-specific instructions in the R Installation and Administration manual.
I have downloaded PDFtoText in mac and wrote following code to convert pdf files to text:
pdf_to_load =("~/my_directory/my.pdf")
system(paste('pdftotext', pdf_to_load))
The code runs well but I am not able to see my.txt in the source directory nor it has been saved anywhere in the folders. Where I went wrong?
One of my mentors were able to run the same code in his computer and he was able to see the converted .txt file.
Kindly guide.
You get a wrong result if the default PDF extraction engine is not found on your computer, see ?tm::readPDF. Those engines are not part of R or of the tm package, and it depends on your computer whether the necessary programs are already installed.
The easiest solution is to install the programs pdftotext and pdfinfo (you'll need both), which you can obtain as precompiled binaries here.
Once these programs are correctly installed, you should be able to extract the text of the PDF file without a system call, by using the readPDF() function of the tm package
library(tm)
my_pdf_txt <- readPDF(control=list(text="-layout"))(elem=list(uri="~/my_directory/my.pdf"), language="en")
i have a knitr based Rnw file that is compiling to pdf perfectly fine in RStudio on mac (v0.97.316) and knitr (v1.1) but in a windows enviornment (same versions) i get a compilation error. I've checked the options in RStudio in both environments and they are consistent.
It appears that the windows setup is always injecting: "\SweaveOpts{concordance=TRUE}" into the ".tex" file even though i have no such flag in the Rnw file, and/or if i toggle the settings in the preferences, and/or if i add the "opts_knit$set(concordance=FALSE)" options to a knitr settings chunk. If i drop the line from the ".tex" file and compile it manually at the cmd prompt the output is generated as expected.
I'm not sure if this is an RStudio or knitr problem, but any pointers would be appreciated.
Note, i've also posted this question on the RStudio support board (http://support.rstudio.org/help/discussions/problems/5039-knitr-compile-problems-with-rstudio-windows?unresolve=true).
The reason that it was injecting \SweaveOpts{concordance=TRUE} is likely to be your weaver was Sweave instead of knitr, and you also enabled Rnw concordance: http://www.rstudio.com/ide/docs/authoring/rnw_weave But I cannot say for sure it is not a bug for the Windows version of RStudio. Anyway, it is easy to verify if it is an RStudio problem -- open the Compile PDF panel and you should see the log like this:
If your weaver was Sweave, you will see something like this instead:
This is my configuration (I'm under Ubuntu):
If you are using knitr indeed, and RStudio still inserts \SweaveOpts{concordance=TRUE}, I guess it must be a bug of RStudio.
I encountered this problem with "R CMD build KFAS-master" downloaded 2016-04-24 as "KFAS-master.zip" clicking "Download ZIP" from KFAS on GitHub (https://github.com/helske/KFAS). I'm unable to replicate the problem, but the work around I used was to find the line mentioned in the pop-up (copied below), and comment it out by preceding it with "%%".
After downloading it, I unzipped it and changed the name of the folder to "KFAS". Then I got a command prompt and navigated to the directory containing the KFAS-master folder renamed as KFAS. This time "R CMD build KFAS" worked as expected. Then I opened ~KFAS/vignettes/KFAS.Rmw in RStudio 0.99.893, the current version as of 2016-04-26. I changed something and saved the result. When I repeated "R CMD build KFAS", I got the problem portrayed in the image below.
Jouni Helske directed me to this post. I commented out line 69 mentioned in the screen shot below by prepending "%%" to "\SweaveOpts{concordance=TRUE}". I saved the result, and "R CMD build KFAS" worked.
Something seems to occasionally insert a line like "\SweaveOpts{concordance=TRUE}" into a *.Rnw file, but I don't know what it is. I'm running R 3.2.4 on a MacBook Pro with OS X 10.11.2.
Thanks to Yihui and others for earlier posts in this thread.
screen shot showing pop-up complaining, "It seems you are using the Sweave-specific syntax in line(s) 69... ."
I had the same problem even when knitr was set for "Weave Rnw files using" by default.
I solved it by going to Tools/Global Options/Sweave and setting "Weave Rnw files using" to Sweave and pressing Ok. Then, I went back and set it back to knitr, and everything works fine now.
I guess it was indeed Rstudio bug
i am creating a package in R language, everything is running properly, but when i run R CMD check , it shows an error message while running examples.. i.e.
"can't open the file." "No such file or directory"
actually my function needs a PubMed text file containing abstracts from the PubMed, i have placed my text file in every sub-directory of my package, but its not working. showing same error again and again.
so please suggest me the right way how to put a text file in a package which can be used by examples to run properly.
i will be very thankful to you.
Usually you put such data in the /inst folder. E.g.:
<packageRoot>/inst/pubmed/myfile
After the package is build you can access the content of this folder from within the package like this:
system.file( "pubmed/myfile", package="<package>" )
See for more information http://cran.r-project.org/doc/manuals/r-release/R-exts.pdf (1.1.5 Data in packages).
I suggest you to use devtools and roxygen2 packages. Basically, you just need to prepare description and .R files.
see more details in this brilliant answer :devtools roxygen package creation and rd documentation
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
}