Installing program (dot) on system search path in R - r

I'm a social scientist having a hard time struggling through the technical instructions for integrating the graphing program dotdocumented here with the sem package documented here.
I have successfully run the command pathDiagram in the sem package, which outputs code that is presumably supposed to be read into dot. The documentation for the sem package says
"To obtain graphics output directly, the dot program must be on the system search path."
I'm sure that this is a totally mundane question, but I'd really appreciate help. I'm lost.

In R execute this command:
Sys.which("dot")
This will tell you whether a system call will be able to find your 'dot' installation. You did install 'dot', right?
The Graphviz pkg file for MacOS greater than or equal to 10.6 (and your ML is 10.8) can be obtained from AT&T labs: http://www.graphviz.org/Download.php
After installing from its .pkg file (with the same Graphviz version 2.30.1 on OSX 10.6.8) I get:
> Sys.which(c("dot"))
dot
"/usr/local/bin/dot"
This does NOT open up dot. It merely confirms that the dot program can be found on by R. Running the first example on hte ?pathDiagram page only outputs a source file to the console which could be used for dot to the console, because no file argument is given, but if you add a file argument it creates a pdf file using dot with a message to the console screen:
pathDiagram(sem.dhp, min.rank='RIQ, RSES, RParAsp, FParAsp, FSES, FIQ', file="test",
max.rank='ROccAsp, REdAsp, FEdAsp, FOccAsp', output.type="graphics", graphics.fmt="pdf")
#Running dot -Tpdf -o test.pdf test.dot
This produced a file in my working directory that could be viewed with Preview.app. If you want to use dot on its own, you will need to read its manual and follow the instructions for using it in a standalone manner.

Update as per comments:
If when you type which dot in terminal, you get a response with a path, then it is safe to say that graphviz is installed.
As per pathDiagram's manual (ie, ?pathDiagram), it is not intended to return a value.
Instead, use the file argument to specify where to save the output to.
pathDiagram(model=SomeModelInR, file="path/to/file", ...)
This seems to be more of a general mac os x question, but anyway, here we go:
In your applications > utilities folder, open up terminal.
(i'm assuming dot is called dot if not please modify accordingly)
Type which dot
- this will show you the path to dot, perhaps something like /usr/bin/dot
- in this case, the path you are looking for in the next part is /usr/bin
Type echo $PATH
- this will show you several paths separated by :
- If the path you got with which dot is listed as one of those paths, then you are all set.
- If not, please see the link below on how to modify
Also see:
How To Edit Your PATH Environment Variables On Mac

Related

How to run Python, R, Octave or Julia using BowPad?

I try using a very light-weighted editor BowPad to edit and run codes.
Take running R as an example, which has been added to Windows PATH Environment Variable D:\R\R-3.6.2\bin\x64
I try to take some following steps:
I built a test.r writing some testing line codes as 1+2; plot(1:10)
Menu --> Run --> Configure custom commands
Command Name: run R
Command Line: D:\R\R-3.6.2\bin\R.exe $(SEL_TEXT)
$(LINE) The line where the cursor is
$(POS) the position where the cursor is
$(TAB_PATH) the path to the file of the active tab
$(TAB_NAME) the file name without extension of the current tab
$(TAB_EXT) the file extension of the current tab
$(TAB_DIR) the directory of the file of the active tab
$(SEL_TEXT) the selected text or the word at the cursor position
$(SEL_TEXT_ESCAPED) like $(SEL_TEXT) but escaped, useful for urls
When I press to choose run R to run the code, something wrong happens!
It will open a Rterm(64-bit) window each time when I run R!
it shows RGUMENT '1+2' __ignored__ or ARGUMENT 'plot(1:10)' __ignored__
Did I missing something ? BTW, can we set some shortcuts keys for running Python, R, Octave or Julia?
Also, when I reach the homepage of BowPad for more information, little usage of this software can be found!
R.exe refers to Rterm. You can use Rgui.exe instead but that won't automatically process arguments. I think what you likely want is to send the selected text to a running R session rather than starting up a new R session each time.
To do that
download this file and optionally place it on your path https://raw.githubusercontent.com/ggrothendieck/batchfiles/master/clip2r.js
in the Bowpad Run | Configure window configure a Run R command as the following (or if clip2r.js is not on your path use the entire pathname). It should be just like this with no arguments.
clip2r.js
From the Windows cmd line if Rgui is running we see that tasklist | findstr Rgui finds Rgui as the R gui process and you can create additional js scripts by editing clip2r.js replacing Rgui in the js script with whatever is the appropriate word to locate python, julia, octave or other R front end assuming again that they are running.
Now to invoke it from within Bowpad:
ensure that your Rgui session is already running and
from within Bowpad select the code you want to run and copy it to the clipboard and then invoke Run R. For example, to run everything use ctrl A ctrl C ctrl R 0 assuming that Run R is in position 0 in the configure menu. Alternately use the mouse with the Ribbon.
Regarding the comment about difficulty finding help, it looks like Bowpad is based on Scintilla so the documentation for it and the related SciTE editor likely apply to Bowpad too. You can also check the Bowpad source code on github.

Error during initialization gnuplot with cygwin (terminal wxt)

I get an error, when I try to start gnuplot via cygwin (see below): Gnuplot does not seem to know the wxt-term, but according to this page (https://cygwin.com/pipermail/cygwin-announce/2020-July/009620.html), gnuplot should be able to know it.
After this I can use gnuplot by hand, e.g. typing in comments and it works normal.
But usually a have automatic plot programs and the error will cause the program to crash, so I can't work with it properly.
Is there any way to force gnuplot to ignore the error? Or can I start gnuplot directly with a certain terminal (e.g. png)?
Thanks
Sandra
$ gnuplot
G N U P L O T
Version 5.4 patchlevel 0 last modified 2020-07-13
Copyright (C) 1986-1993, 1998, 2004, 2007-2020
Thomas Williams, Colin Kelley and many others
gnuplot home: http://www.gnuplot.info
faq, bugs, etc: type "help FAQ"
immediate help: type "help" (plot window: hit 'h')
Terminal type is now 'unknown'
set term wxt
^
unknown or ambiguous terminal type; type just 'set terminal' for a list
WARNING: Error during initialization
gnuplot>```
Not tested but I suspect you need to use the gnuplot-wx
package as there are several gnuplot packages:
gnuplot-base A command-line driven interactive function plotting utility
gnuplot-doc A command-line driven interactive function plotting utility
gnuplot-qt5 A command-line driven interactive function plotting utility
gnuplot-wx A command-line driven interactive function plotting utility
gnuplot-X11 A command-line driven interactive function plotting utility
https://cygwin.com/packages/summary/gnuplot-wx.html
"Is there any way to force gnuplot to ignore the error?" No. When a gnuplot executable is built, someone has to choose which terminal types to include. This usually depends on what support libraries they have available to them when building it. Apparently your gnuplot executable was built without the wxt terminal. If you really need that particular output mode you will need to find a different gnuplot executable or build one yourself.
On the other hand - "can I start gnuplot directly with a certain terminal (e.g. png)?". Yes. There are at least three ways to do this.
Define an environmental variable GNUTERM. For example
export GNUTERM="png truecolor size 700,500"
(But that's a linux command. I don't whether there is an equivalent on Windows)
Edit the initialization file to contain a line such as
set term png truecolor size 700,500
From the "help" text:
When gnuplot is run, it first looks for a system-wide initialization file
gnuplotrc. The location of this file is determined when the program is built
and is reported by show loadpath. The program then looks in the user's HOME
directory for a file called .gnuplot on Unix-like systems or GNUPLOT.INI on
other systems. (OS/2 will look for it in the directory named in
the environment variable GNUPLOT; Windows will use APPDATA).
Give the "set term" command as part of the command line when you run gnuplot:
gnuplot -e "set term png truecolor size 700,500"

R system() not working with MODIS Reprojection Tool

I have a command that works when typed into the Terminal on a Mac (OSX El Cap), but the same command fails when called from R using system().
I am trying to batch-process MODIS satellite files using the MODIS Reprojection Tool. My software is all up to date.
This is a simple example in which I mosaic two files. The names of the two files are in a text input file called input.list. The command just tells the mrtmosaic routine where to find the input list and where to put the output.
This command works correctly in the Terminal:
/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i ~/temp/input.list -o ~/temp/output.hdf
However, if I put exactly the same string into a variable and run it from R (using RStudio), it fails:
comstring<-"/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i ~/temp/input.list -o ~/temp/output.hdf"
system(comstring)
Warning: gctp_call : Environmental Variable Not Found:
MRT_DATA_DIR nor MRTDATADIR not defined
Error: GetInputGeoCornerMosaic : General Processing Error converting lat/long coordinates to input projection coordinates.
Fatal Error, Terminating...
The strange thing is that the system knows what the environment variables are. In the terminal, the command
echo $MRT_DATA_DIR
shows the correct directory: /Applications/Modis_Reprojection_Tool/data
I don't see why it would have trouble finding the variables from an R system() call when it has no trouble in the Terminal. I'm very stumped!
I posted this question to the R help list, and two people there helped me solve the problem. They suggested (if I understood right) that the problem was that OSX El Capitan was not successfully passing environment variables to R. In any case:
These do not work:
Setting the environment variable in my .bash_profile (for example by adding and exporting MTR_DATA_DIR="/Applications/MRT/data"); or
Setting the environment variable by adding the same line to the .Rprofile file in my home directory.
These do work:
Setting the environment variable at the R command line by typing Sys.setenv(MRT_DATA_DIR="/Applications/MRT/data"); or
Setting the environment variable in the .Renviron file (which is in my home directory) by adding MRT_DATA_DIR="/Applications/MRT/data" to it; or
Typing MRT_DATA_DIR="/Applications/MRT/data" open -a Rstudio in the Terminal. This last method is an effective workaround and a useful tool in a bag of tricks, but is slightly clumsier since one has to remember to open RStudio this way each time.

Building R packages - using environment variables in DESCRIPTION file?

At our site, we have a large amount of custom R code that is used to build a set of packages for internal use and distribution to our R users. We try to maintain the entire library in a versioning scheme so that the version numbers and the date are the same. The problem is that we've gotten to the point where the number of packages is substantial enough that manual modification of the DESCRIPTION file and the package .Rd file is very time consuming, and it would be nice to automate these pieces.
We could write a pre-script that goes through the full set of files and writes the current data and version number. This could be done with out a lot of pain, but it would modify our current build chain and we would have to adapt the various steps.
Is there a way that this can be done without having to do a pre-build file modification step? In other words, can the DESCRIPTION file and the .Rd file contain something akin to an environment variable that will be substituted with the current information when called upon by R CMD build ?
You cannot use environment variables as R, when running R CMD build ... or R CMD INSTALL ..., sees the file as fixed.
But the no problem that cannot be fixed by another layer of indirection saying remains true. Your R source code could simply be files within another layer in which you text substitution according to some pattern. If you like autoconf, you could just have DESCRIPTION.in and have a configure script query the environment variables, or a meta-config file or database, or something else, and have that written out. Similarly you could have a sed or perl or python or R or ... script doing the textual substitution.
I used to let svn fill in the argument to Date: in DESCRIPTION, and also encoded revision numbers in an included header file. It's all scriptable to your heart's content.

How to find compiled function source code in R package?

For example, I am looking into a R function PTdensity.R source code in a package called DPpackage, where I found the author called a fortran function ptdensityu :
foo <- .Fortran("ptdensityu", ...
The thing is how to find source code for ptdensityu subroutine. It may contains in a certain fortran file in /src/ directory, but how do I know which file is it. (Actually I found it by manually check each file under /src/ and found it is in the /src/PTudensity.f.)
Quick link for the package : link
PS: I used to use this link to search source code, but somehow it does not work any more.
On a linux box, you use the grep command. In emacs, you build a tags file. In other editors there's probably similar functions. In Windows, can't you right click on a folder, hit Search... and fill in the 'A word or phrase in the file" box. Or install cygwin and use the grep command.
Amazes me that people are using computers without basic skills such as finding a string in a file...
Have you untarred this...............?
(It was the first subroutine in PTudensity.f)

Resources