\input texinfo
@c %**start of header
@setfilename rw-FAQ.info
@settitle R for Windows FAQ
@setchapternewpage on
@set VERSION @RWVER@
@set RVERSION @RVER@
@c %**end of header
@documentlanguage en
@documentencoding ISO-8859-1

@finalout

@macro newchap{}
@ifinfo
@sp 1
@end ifinfo
@ifhtml
@html
<hr>
@end html
@end ifhtml
@end macro

@titlepage
@title R for Windows FAQ
@subtitle Frequently Asked Questions on R for Windows
@subtitle Version @value{VERSION}
@author B. D. Ripley and D. J. Murdoch
@end titlepage

@ifinfo
R for Windows FAQ                           @*
Frequently Asked Questions on R for Windows @*
Version for @value{VERSION}                 @*
B. D. Ripley and D. J. Murdoch              @*

@sp 2
@end ifinfo

@ifhtml
@html
<h2>Version for <tt>@value{VERSION}</tt></h2>
<address>B. D. Ripley and D. J. Murdoch</address>
<hr>
@end html
@end ifhtml

@paragraphindent 0

@contents

@ifinfo
@sp 1
@end ifinfo


@node Top, Introduction, (dir), (dir)

@menu
* Introduction::                
* Installation and Usage::      
* Languages and Internationalization::  
* Packages::                    
* Windows Features::            
* Workspaces::                  
* The R Console::               
* Building from Source::        
@end menu


@newchap{}
@node Introduction, Installation and Usage, Top, Top
@chapter Introduction

This FAQ is for the Windows port of R: it describes features specific to
that version.  The main R FAQ can be found at

@display
@uref{http://CRAN.R-project.org/doc/FAQ/R-FAQ.html}.
@end display

The information here applies only to recent versions of R for Windows,
(@samp{2.3.0} or later).


@newchap{}
@node Installation and Usage, Languages and Internationalization, Introduction, Top
@chapter Installation and Usage

@menu
* Where can I find the latest version?::  
* How do I install R for Windows?::  
* How do I check an installation?::  
* Can I customize the installation?::  
* How do I run it?::            
* Can I run R from a CD or USB drive?::  
* How do I UNinstall R?::       
* What's the best way to upgrade?::  
* There seems to be a limit on the memory it uses!::  
* How can I keep workspaces for different projects in different directories?::  
* How do I print from R?::      
* Can I use R CMD BATCH?::      
* Can I use @RWVER@ with ESS and (X)emacs?::  
* What are HOME and working directories?::  
* How do I set environment variables?::  
* R can't find my file::        
* Does R use the Registry?::    
* Does R support automation?::  
* The Internet download functions fail.::  
* Entering certain characters crashes Rgui.::  
* What does 'DLL attempted to change FPU control word' mean?::  
* Other strange crashes.::      
* Why does R never use more than 50% of my CPU?::  
@end menu


@node Where can I find the latest version?, How do I install R for Windows?, Installation and Usage, Installation and Usage
@section Where can I find the latest version?

Go to any CRAN site (see @uref{http://cran.r-project.org/mirrors.html}
for a list), navigate to the @file{bin/windows/base} directory and
collect the file(s) you need.  The current release is distributed as an
installer @samp{@RWVER@-win32.exe} of about 28Mb.  This contains all the
components and allows as complete as installation as you choose.

There are also links on that page to the @samp{r-patched} and
@samp{r-devel} snapshots.  These are frequently updated builds of
development versions of R.  The @samp{r-patched} build includes bug
fixes to the current release, and @samp{r-devel} contains these as well
as changes that will eventually make it into the next @samp{x.y.0}
release.

@node How do I install R for Windows?, How do I check an installation?, Where can I find the latest version?, Installation and Usage
@section How do I install R for Windows?

You need Windows 95 or later.  There is no specific version for Windows
64, but the standard 32-bit version has been proven to work.  Some
features only work on an NT-based version of Windows (and some work
better on such an OS).  We do minimally test releases on Windows 98 and
occasionally on NT4, but only test thoroughly on XP and later.

Your file system must allow case-honouring long file names (as is likely
except perhaps for some network-mounted systems).  A full installation
takes up about 60Mb of disk space and a minimal one less than 20Mb.

If you want to be able to build packages from sources, we recommend that
you choose an installation path not containing spaces.  (Using a path
with spaces in will probably work, but is little-tested.)

To install use @samp{@RWVER@-win32.exe}.  Just double-click on the icon and
follow the instructions.  If you installed R this way you can uninstall
it from the Control Panel or Start Menu (unless you suppressed making a
group for R).

Choose a working directory for R.  You will have a shortcut to
@file{@RWVER@\bin\Rgui.exe} on your desktop and/or somewhere on the
Start menu file tree, and perhaps also in the Quick Launch bar.
Right-click each shortcut, select Properties... and change the `Start
in' field to your working directory.

You may also want to add command-line arguments at the end of the Target
field (@emph{after} any final double quote), for example @code{--sdi
--max-mem-size=1Gb}.  You can also set environment variables at the end
of the Target field, for example @code{R_LIBS=p:/myRlib}, and if you
want to ensure that menus and messages are in (American) English,
@code{LANGUAGE=en}.

It is also possible to install from an MSI file, which will be of interest
only for sysadmins.  For how to build an MSI file, see the `R
Installation and Administration Manual'.


@node How do I check an installation?, Can I customize the installation?, How do I install R for Windows?, Installation and Usage
@section How do I check an installation is not corrupt?

Run the program @file{bin\md5check.exe}.
This compares checksums on all the installed files with those put into
the installer, and will report any changed or missing files.

(It only works if R was installed from the Inno Setup installer.)

@node Can I customize the installation?, How do I run it?, How do I check an installation?, Installation and Usage
@section Can I customize the installation?

The normal way to customize the installation is by selecting components
from the wizards shown.  However, sysadmins might like to install R from
scripts, and the following command-line flags are available for use with
the installer.

@table @samp
@item /SILENT
only show the installation progress window and error messages.
@item /VERYSILENT
only show error messages.
@item /DIR="x:\dirname"
set the default installation directory
@item /GROUP="folder name"
set the default Start-menu group name
@item /COMPONENTS="comma separated list of component names"
set the initial list of components: Components are named @samp{main},
@samp{chtml}, @samp{html}, @samp{latex}, @samp{manuals}, @samp{refman},
@samp{libdocs}, @samp{devel}, @samp{tcl}, @samp{mbcs}, @samp{Rd} and
@samp{trans}.
@end table

@noindent
It is also possible to save the settings used to a file and later
reload those settings using

@table @samp
@item /SAVEINF="filename"
save the settings to the specified file.  Don't forget to use quotes if
the filename contains spaces.
@item /LOADINF="filename"
instructs the installer to load the settings from the specified file
after having checked the command line.
@end table

A successful installation has exit code 0: unsuccessful ones may give 1,
2, 3, 4 or 5.  See the help for Inno Setup
(@uref{http://jrsoftware.org/isinfo.php}) for details.

We have some facilities for building a customized installer, in
particular to add packages to the installer.  See the `R Installation
and Administration' manual in the subsection `Building the installers'.

@node How do I run it?, Can I run R from a CD or USB drive?, Can I customize the installation?, Installation and Usage
@section How do I run it?

Just double-click on the shortcut you prepared at installation.

If you want to set up another project, make a new shortcut or use the
existing one and change the `Start in' field of the Properties.

You may if you prefer run R from the command line of any shell you use,
for example an `MS-DOS window' (Windows 9x/ME), a `Command Prompt'
(NT-based versions) or a port of a Unix shell such as @command{tcsh} or
@command{bash}. (The command line can be anything you would put in the
Target field of a shortcut, and the starting directory will be the
current working directory of the shell.)

@node Can I run R from a CD or USB drive?, How do I UNinstall R?, How do I run it?, Installation and Usage
@section Can I run R from a CD or USB drive?

Yes, with care.  A basic R installation is relocatable, so you can burn
an image of the R installation on your hard disc or install directly
onto a removable storage device such as a flash-memory USB drive.  (If
you have installed packages into a private library, their absolute paths
will appear in the HTML packages list.)

Running R does need access to a writable temporary directory and to a
home directory, and in the last resort these are taken to be the current
directory.  This should be no problem on a properly configured NT-based
version of Windows, but otherwise does mean that it may not be possible
to run R without creating a shortcut in a writable folder.


@node How do I UNinstall R?, What's the best way to upgrade?, Can I run R from a CD or USB drive?, Installation and Usage
@section How do I UNinstall R?

Normally you can do this from the R group on the Start Menu or from the
@samp{Add/Remove Programs} in the Control Panel.  If it does not appear
there or if you want to remove an old version, run @file{unins000.exe}
in the top-level installation directory.  (There should be a separate
uninstall item in the R group for each installed version of R.)

Uninstalling R only removes files from the initial installation, not
(for example) packages you have installed or updated.

If all else fails, you can just delete the whole directory in which R
was installed.


@node What's the best way to upgrade?, There seems to be a limit on the memory it uses!, How do I UNinstall R?, Installation and Usage
@section What's the best way to upgrade?

That's a matter of taste.  For most people the best thing to do is to
uninstall R (see the previous Q), install the new version, copy any
installed packages to the library folder in the new installation, run
@code{update.packages()} in the new R (`Update packages...' from
the Packages menu, if you prefer) and then delete anything left of the
old installation.  Different versions of R are quite deliberately
installed in parallel folders so you can keep old versions around if you
wish.

Upgrading from R 1.x.y to R 2.x.y is special as all the packages
need to be reinstalled.  Rather than copy them across, make a note of
their names and re-install them from CRAN.


@node There seems to be a limit on the memory it uses!, How can I keep workspaces for different projects in different directories?, What's the best way to upgrade?, Installation and Usage
@section There seems to be a limit on the memory it uses!

Indeed there is.  It is set by the command-line flag
@option{--max-mem-size} (@pxref{How do I install R for Windows?}) and
defaults to the smaller of the amount of physical RAM in the machine and
1.5Gb.  It can be set to any amount between 32Mb and 3Gb.  Be aware though
that Windows has (in most versions) a maximum amount of user virtual
memory of 2Gb.

Use @code{?Memory} and @code{?memory.size} for information about memory
usage.  The limit can be raised by calling @code{memory.limit} within a
running R session.

R can be compiled to use a different memory manager which might be better
at using large amounts of memory, but is substantially slower (making R
several times slower on some tasks).

The executables @file{Rgui.exe} and @file{Rterm.exe} support up to 3Gb
per process under suitably enabled versions of Windows (see
@uref{http://www.microsoft.com/whdc/system/platform/server/PAE/PAEmem.mspx}:
even where this is supported it has to be specifically enabled).
On such systems, the default for @code{--max-mem-size} is the smaller of
the amount of RAM and 2.5Gb.

It is possible that the Windows API calls used will give misleading
results on machines with more than 4Gb of physical RAM (and later ones
cannot be used as R must run on Windows 2000 and earlier): for safety
set @code{--max-mem-size} explicitly on such machines.


@node How can I keep workspaces for different projects in different directories?, How do I print from R?, There seems to be a limit on the memory it uses!, Installation and Usage
@section How can I keep workspaces for different projects in different directories?

Create a separate shortcut for each project: see Q2.5.  All the paths to
files used by R are relative to the starting directory, so setting the
`Start in' field automatically helps separate projects.

Alternatively, start R by double-clicking on a saved @file{.RData} file
in the directory for the project you want to use, or drag-and-drop a
file with extension @file{.RData} onto an R shortcut.  In either case,
the working directory will be set to that containing the file.


@node How do I print from R?, Can I use R CMD BATCH?, How can I keep workspaces for different projects in different directories?, Installation and Usage
@section How do I print from R?

It depends what you want to print.

@itemize @bullet
@item You can print the graphics window from its menu or by using
@code{dev.print} with suitable arguments (see its help page: most likely
@code{dev.print(win.graph)} will work).

@item You can print from the R console or pager by @samp{File | Print}.
(This will print the selection if there is one, otherwise the whole
console or pager contents.)

@item You can print help files from the pager or HTML browser.

@item If you have configured @file{RHOME\bin\helpPRINT.bat} and have
LaTeX installed you can print help files by @code{help(fn_name, offline=TRUE)}.
@end itemize


@node Can I use R CMD BATCH?, Can I use @RWVER@ with ESS and (X)emacs?, How do I print from R?, Installation and Usage
@section Can I use @code{R CMD BATCH}?

Yes: use @code{R CMD BATCH --help} or @code{?BATCH} for full details.

You can set also up a batch file using @command{Rterm.exe}.  A sample
batch file might contain (as one line)

@example
path_to_R\bin\Rterm.exe --no-restore --no-save < %1 > %1.out 2>&1
@end example

@noindent
The purpose of @code{2>&1} is to redirect warnings and errors to the
same file as normal output, and users of Windows 95/98/ME's default
@code{command.com} `shell' will need to omit it.  (That program has no
means to redirect @file{stderr}, and @command{Rterm.exe} sends warnings and
errors to the normal output file on such systems.)


@node Can I use @RWVER@ with ESS and (X)emacs?, What are HOME and working directories?, Can I use R CMD BATCH?, Installation and Usage
@section Can I use @RWVER@ with ESS and (X)emacs?

Yes.  Recent versions of ESS (e.g. 5.2.x) come with support for this
version of R, and there is support for interrupting the R process from
ESS (by @code{C-c C-c}).

For help with ESS, please send email to @email{ESS-help@@stat.ethz.ch},
not the R mailing lists.


@node What are HOME and working directories?, How do I set environment variables?, Can I use @RWVER@ with ESS and (X)emacs?, Installation and Usage
@section What are HOME and working directories?

Several places in the documentation use these terms.

The working directory is the directory from which @code{Rgui} or
@command{Rterm} was launched, unless a shortcut was used when it is given
by the `Start in' field of the shortcut's properties.  You can find this
from R code by the call @code{getwd()}.

The home directory is set as follows: If environment variable
@env{R_USER} is set, its value is used.  Otherwise if environment
variable @env{HOME} is set, its value is used.  After those two
user-controllable settings, R tries to find system-defined home
directories.  It first tries to use the Windows "personal" directory
(typically @file{C:\Documents and Settings\username\My Documents} in
Windows XP).  If that fails, if both environment variables
@env{HOMEDRIVE} and @env{HOMEPATH} are set (and they normally are under
Windows NT/2000/XP/2003), the value is @file{$@{HOMEDRIVE@}$@{HOMEPATH@}}.
If all of these fail, the current working directory is used.

You can find this from R code by @code{Sys.getenv("R_USER")}.

@node How do I set environment variables?, R can't find my file, What are HOME and working directories?, Installation and Usage
@section How do I set environment variables?

Environment variables can be set for @command{Rgui.exe} and
@command{Rterm.exe} in three different ways.
@enumerate
@item
On the command line as name=value pairs.  For example in the shortcut to
@command{Rgui} you could have

@example
"path_to_R\bin\Rgui.exe" HOME=p:/ R_LIBS=p:/myRlib
@end example

@item
In an environment file @file{.Renviron} in the working directory
or your home directory, for example containing the line

@example
R_LIBS=p:/myRlib
@end example

If you have permission to do so, you can also create an environment file
@file{etc\Renviron.site} and set environmental variables in that file in
the same way.  This is useful for variables which should be set for all
users and all usages of this R installation.  (Their values can be
overridden in a @file{.Renviron} file or on the command line.)

See @code{?Startup} for more details of environment files, include
the site-wide environment file @file{Renviron.site}.

@item
For all applications via Windows.  How you set an environment variable
is system specific: in Windows 9x you can set them in
@file{autoexec.bat} or in an MS-DOS window from which you launch
@command{Rgui} / @command{Rterm}.  Under Windows NT/2000/XP/2003 you can use
the control panel or the properties of `My Computer'.  Under Windows ME
you can use the System Configuration Utility (under Programs,
Accessories, System Tools on the Start menu).  You may have to log out
or reboot for such changes to take effect.
@end enumerate

The order of precedence for environmental variables is the order in
which these options are listed, that is the command line then
@file{.Renviron} then the inherited environment.


@node R can't find my file, Does R use the Registry?, How do I set environment variables?, Installation and Usage
@section R can't find my file, but I know it is there!

How did you specify it?  Backslashes have to be doubled in R character
strings, so for example one needs
@samp{"d:\\@RWVER@\\library\\xgobi\\scripts\\xgobi.bat"}.  You can
make life easier for yourself by using forward slashes as path
separators: they do work under Windows.  You should include the file
extension (e.g. @samp{"xgobi.bat"} rather than just @samp{"xgobi"});
sometimes this isn't shown in Windows, but it is necessary in R.

A simple way to avoid these problems is to use the function
@code{file.choose()} to invoke the standard Windows file selection
dialog.  If you select a file there, the name will be passed to R in
the correct format.

Another possible source of grief is spaces in folder names.  We have
tried to make R work on paths with spaces in, but many people writing
packages for Unix do not bother.  So it is worth trying the alternative
short name (something like @samp{PROGRA~1}; you can get it as the
`MS-DOS name' from the Properties of the file on most versions of
Windows, and from @command{dir /X} in a @samp{Command Prompt} window on
2000/XP/2003).


@node Does R use the Registry?, Does R support automation?, R can't find my file, Installation and Usage
@section Does R use the Registry?

No, not when R itself is running.

When you run the R installer, there are options (under `Select Additional
Tasks') to `Save version number in registry' and `Associate R with .RData
files'.

If you tick the first option, the following string entries are added to the
Windows registry:
@itemize @bullet
@item  @code{HKEY_LOCAL_MACHINE\Software\R-core\R\Current Version}  
    contains the version number, currently @RVER@.
@item @code{HKEY_LOCAL_MACHINE\Software\R-core\R\[version]\InstallPath}
    (where @code{[version]} is currently @RVER@) contains the path to the R 
    home directory.
@end itemize
If you do not have administrative privileges on the machine while running
the installer, then the entries are created under the @code{HKEY_CURRENT_USER}
root.

If you tick the second option (`Associate R with .RData files') and have
administrative privileges, then entries are created under
@code{HKEY_CLASSES_ROOT\.RData} and @code{HKEY_CLASSES_ROOT\RWorkspace}.

After installation you can add the Registry entries in
@code{HKEY_LOCAL_MACHINE} by running @code{RSetReg.exe} in the
@code{bin} folder, and remove them by running this with argument
@code{/U}.  Note that this requires administrative privileges and
neither sets up nor removes the file associations.


@node Does R support automation?, The Internet download functions fail., Does R use the Registry?, Installation and Usage
@section Does R support automation (OLE, COM)?

Directly, no.

There is a (D)COM server written by Thomas Baier available on CRAN
(@uref{http://cran.r-project.org/other-software.html}) which works with
@file{Rproxy.dll} (in the R distribution) and @file{R.dll} to support
transfer of data to and from R and remote execution of R commands, as
well as embedding of an R graphics window.  An R-Excel interface making
use of the (D)COM server is included in the distribution.

Thomas Baier also has a package @code{rcom} available on CRAN (in the
usual way for a package) to provide COM client and server functionality
from within R.

Another (D)COM server is available from @uref{http://www.omegahat.org/},
which allows R objects to be exported as COM values.  That site also
has packages @code{RDCOMClient} and @code{SWinTypeLibs} which allow R
to act as a (D)COM client.


@node The Internet download functions fail., Entering certain characters crashes Rgui., Does R support automation?, Installation and Usage
@section The Internet download functions fail.

for example @code{update.packages()} and the menu items on the Packages menu.

We have had several reports of this, although they do work for us on
@emph{all} of our machines.  There are two known possible fixes.

(a) Use the alternative @file{internet2.dll} by starting R with the flag
@option{--internet2} (@pxref{How do I install R for Windows?}) which uses
the Internet Explorer internals (and so needs Internet Explorer 4 or
later installed).  Note that this does not work with proxies that need
authentication.

(b) A proxy needs to be set up: see @code{?download.file}.  Here are two
versions of an example (a real one, but from a machine that is only
available locally) of a command-line in a short cut:

@example
"path_to_R\bin\Rgui.exe" http_proxy=http://user:pass@@gannet:80/

"path_to_R\bin\Rgui.exe" http_proxy=http://gannet/ http_proxy_user=ask
@end example

@noindent
The second version will prompt the user for the proxy username and
password when HTTP downloads are first used.


@node Entering certain characters crashes Rgui., What does 'DLL attempted to change FPU control word' mean?, The Internet download functions fail., Installation and Usage
@section Entering certain characters crashes Rgui.

This used to happen occasionally, and all the occurrences we have solved
have been traced to faulty versions of @samp{msvcrt.dll}.  We have
installed a workaround that seems to avoid this.  A few other people
have discovered this was caused by desktop switcher and keyboard macro
programs, for example `Macro Magic' and `JS Pager'.

If it still happens, try extracting the @samp{msvcrt.dll} to be found in
the self-extracting archive
@uref{ftp://ftp.microsoft.com/softlib/mslfiles/msvcrt.exe} and put it in
the @file{@RWVER@\bin} directory.  Removing @samp{msvcrt.dll} from that
directory reverts to the standard behaviour.  It seems that on some
versions of Windows (but not 2000/XP/2003) you also need to put the
@file{@RWVER@\bin} directory early in your path.

This fix has solved other problems too, for example incorrect results in
the date-time functions.  However, you are probably better off
re-installing Windows.

@node What does 'DLL attempted to change FPU control word' mean?, Other strange crashes., Entering certain characters crashes Rgui., Installation and Usage
@section What does 'DLL attempted to change FPU control word' mean?

This is a @emph{warning} which indicates that R has taken action to
correct the action of some (non-R) DLL which has just been loaded and
has changed the floating point control word (in its initialization code)
to a setting incompatible with that needed for R.  This is not good
practice on the part of the DLL, and often indicates that it needs to be
updated.  (It was common with DLLs compiled in early versions of Visual C++.)

Unfortunately, because DLLs may load other DLLs it is not possible for R
to track which DLL caused the problem.

See also @code{?dyn.load)}.

@node Other strange crashes., Why does R never use more than 50% of my CPU?, What does 'DLL attempted to change FPU control word' mean?, Installation and Usage
@section Other strange crashes.

Some users have found that @code{Rgui.exe} fails to start, exiting with
a ``Floating-point invalid operation'' or other low level error.  This
error may also happen in the middle of a session.  In some cases where
we have tracked this down, it was due to bugs in the video driver on the
system in question: it makes changes to the floating point control word
which are incompatible with R.  (Good practice would restore the control
word to the state it was in when the driver code was called, and R
tries hard to correct this before running its own code.)  For example,
one user reported that the virtual screen manager JSP2 caused this
crash.

These errors are essentially impossible for us to fix or work around
beyond the measures already taken (which were increased in R 2.3.0).
The only solution we know of is for the user to replace the buggy system
component that is causing the error.


@node Why does R never use more than 50% of my CPU?,  , Other strange crashes., Installation and Usage
@section Why does R never use more than 50% of my CPU?

This is a misreading of Windows' confusing Task Manager.  R's
computation is single-threaded, and so it cannot use more than one CPU.
What the task manager shows is not the usage in CPUs (as better written
utilities do) but the usage as a percentage of the apparent total number
of CPUs.  We say `apparent' as it treats so-called `hyperthreaded' CPUs
as two CPUs even though there is only one physical CPU: dual-core CPUs
will also show as two CPUs (which they genuinely are).

Many benchmarks show hyperthreading reduces the overall workload, so you
may want to experiment with disabling it.

@newchap{}
@node Languages and Internationalization, Packages, Installation and Usage, Top
@chapter Languages and Internationalization

@menu
* The installer does not offer my language::  
* I want R in English!::        
* I want to run R in Chinese/Japanese/Korean::  
* I selected English for installation but R runs in Chinese::  
* I would like to be able to use Japanese fonts::  
* I don't see characters with accents at the R console::  
* The dialog buttons are not translated::  
@end menu

@node The installer does not offer my language, I want R in English!, Languages and Internationalization, Languages and Internationalization
@section The installer does not offer my language.

Two things may be happening.  First, only languages which can be
displayed using the current codepage are offered so you cannot install
in Japanese unless running Windows in Japanese.

Second, only a limited range of languages is supported (but still wider
than those for which R has translations), currently
Brazilian Portuguese,
Catalan,
both Simplified and Traditional Chinese,
Czech,
Danish,
Dutch,
Finnish,
French,
German,
Hungarian,
Italian,
Japanese,
Korean,
Norwegian,
Polish,
Portuguese,
Russian,
Slovenian
and Spanish (Spain).

@node I want R in English!, I want to run R in Chinese/Japanese/Korean, The installer does not offer my language, Languages and Internationalization
@section I want R in English (and not in French/Chinese/...)!

The default behaviour of R is to try to run in the language you run
Windows in.  Apparently some users want Windows in their native language,
but not R.  To do so, set @code{LANGUAGE=en} as discussed in Q2.2 and
Q2.15, or in the @file{Rconsole} file.

@node I want to run R in Chinese/Japanese/Korean, I selected English for installation but R runs in Chinese, I want R in English!, Languages and Internationalization
@section I want to run R in Chinese/Japanese/Korean.

All recent Windows versions of R support `East Asian' languages
(with a suitable version of Windows -- e.g. Western installations of
Windows XP do not by default have such support).

Both @command{Rterm.exe} and @command{RGui.exe} support single- and
double-width characters.  It will be necessary to select suitable fonts
in files @file{Rconsole} and @file{Rdevga} (see @code{?Rconsole} or the
comments in the files: the system versions are in the @file{etc}
folder); in the latter you can replace @code{Arial} by @code{Arial
Unicode MS}, and we tried @code{FixedSys} and @code{MS Mincho} in
@file{Rconsole}.  (Note that @file{Rdevga} only applies to Windows
graphics devices and not, say, to @code{pdf}.)

You do need to ensure that R is running in a suitable locale: use
@code{Sys.getlocale()} to find out.  (CJK users may be used to their
language characters always being available, which is the case for
so-called `Unicode' Windows applications.  However, R has to run on
Windows 9x and is not therefore `Unicode'.)  You can find suitable
locale names from
@uref{http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_language_and_country_strings.asp}:
beware that @code{"Chinese"} is Traditional Chinese (code page 950,
Big5) and @code{"chs"} is needed for Simplified Chinese (code page 936,
GB2312).  Note that this can be rather tricky: on Windows XP there are
several places to select the language under the `Regional and Language
Options' part of the control panel, and the appropriate language has to
be set under @emph{both} the `Regional Options' and `Advanced' tabs.

When using @command{Rterm} the window in which it is run has to be set up
to use a suitable font and a suitable codepage (which for the Windows
Cmd shell can be done by @command{chcp}).

@node I selected English for installation but R runs in Chinese, I would like to be able to use Japanese fonts, I want to run R in Chinese/Japanese/Korean, Languages and Internationalization
@section I selected English for installation but R runs in Chinese.

Precisely, you selected English @strong{for installation}!  The language
of the installer has nothing to do with the language used to run R: this
is completely standard Windows practice (and necessary as different
users of the computer may use different languages).

The language R uses for menus and messages is determined by the
@emph{locale}: please read the appropriate manual (the @emph{R
Installation and Administration Manual}) for the details.  You can
ensure that R uses English messages by appending @code{LANGUAGE=en} to
the shortcut you use to start R, or setting it in the @file{Rconsole} file.

@node I would like to be able to use Japanese fonts, I don't see characters with accents at the R console, I selected English for installation but R runs in Chinese, Languages and Internationalization
@section I would like to be able to use Japanese fonts.

for example, in the console and to annotate graphs.  Similar comments
apply to any non-Western European language.

This is possible by setting suitable fonts in the @file{Rconsole} and
@file{Rdevga} configuration files (see Q3.2 and the next Q).  You can
specify additional fonts in @file{Rdevga}, and use them by

@example
par(font=, font.lab=, font.main=, font.sub=)
@end example

Nineteen fonts are specified (as 1 to 19) by default: you can add to these
(up to 13 more) or replace them.  Also, you can make use of font
families in a way similar to other graphics devices: see the examples
for @code{windowsFonts}.

In addition, the Hershey vector fonts (see @code{?Hershey},
@code{?Japanese} and @code{demo(Japanese)}) can be used on any graphics
device to display Japanese characters.

To use non-Latin-1 characters in the @code{postscript} graphics device,
see its help page (which also applies to @code{pdf}).

@node I don't see characters with accents at the R console, The dialog buttons are not translated, I would like to be able to use Japanese fonts, Languages and Internationalization
@section I don't see characters with accents at the R console, for example in ?text.

You need to specify a font in @file{Rconsole} (see Q3.5) that supports
the encoding in use, in Western European languages Latin-1.  The default
font, @code{Courier New}, does on our systems, as does @code{FixedSys}.
This may be a problem in other locales, especially for non-Western
European languages.

Support for these characters within @command{Rterm} depends on the
environment (the terminal window and shell, including locale settings)
within which it is run as well as the font used by the terminal window.

If you are using a non-Latin-1 language, you do need to ensure that the
fonts you selected support the language.  For example, it was found by
one Czech user (under Windows 98) that he had to select @code{Times New
Roman CE} or @code{Courier} (not @code{Courier New}) to get certain
Czech characters displayed correctly.

@node The dialog buttons are not translated,  , I don't see characters with accents at the R console, Languages and Internationalization
@section The dialog buttons are not translated.

In most cases they actually are, but by Windows.  Setting the locale or
the @code{LANGUAGE} environment variable does not change the Windows
setting of its `UI language'.  Under (international) Windows XP there
are three tabs on the @emph{Regional and Language Options} applet in the
Control Panel.  The first tab controls the locale, the second the UI and
the third the fonts used for `non-Unicode' programs (but apparently also
for Unicode ones in many cases): see Q3.3.

If you have Windows running completely in say French or Chinese these
settings are likely to be consistent.  However, if you try to run
Windows in one language and R in another, you may find the way Windows
handles internationalization slightly odd.  For further details see
@uref{http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/nls_0ddl.asp}

@newchap{}
@node Packages, Windows Features, Languages and Internationalization, Top
@chapter Packages


@menu
* Can I install packages into libraries in this version?::  
* I don't have permission to write to the @RWVER@\library directory::  
* The packages I installed do not appear in the HTML help system::  
* My functions are not found by the HTML help search system.::  
* Loading a package fails.::    
* Package TclTk does not work.::  
* Hyperlinks in HTML do not work.::  
* update.packages() fails::     
* How do I add to the list of repositories?::  
@end menu


@node Can I install packages into libraries in this version?, I don't have permission to write to the @RWVER@\library directory, Packages, Packages
@section Can I install packages into libraries in this version?

Yes, but you will need a lot of tools to do so, unless the author or the
maintainers of the @file{bin/windows/contrib} section on CRAN have been
kind enough to provide a pre-compiled version for Windows as a
@code{.zip} file.

You can install pre-compiled packages either from CRAN or from a local
@code{.zip} file by using @code{install.packages}: see its help page.
There are menu items on the @code{Packages} menu to provide a
point-and-click interface to package installation.  The packages for
each minor (2.x) version will be stored in a separate area, so for R
2.4.? the files are in @file{bin/windows/contrib/2.4}.  You can try
those compiled for earlier versions (but not before 2.0.0), at your own
risk.

Note that the pre-compiled versions on CRAN are unsupported: see
@uref{http://cran.r-project.org/bin/windows/contrib/ReadMe}, which also
gives the locations of a few other pre-compiled packages.

If there is not a pre-compiled version or that is not up-to-date or you
prefer compiling from source, read the `R Installation and
Administration' manual section on `Add-on Packages'.  You need to make
sure you installed the necessary files, and you will need to collect and
install several tools: you can download them via the portal at
@uref{http://www.murdoch-sutherland.com/Rtools/}.  Once you have done
so, just run @command{R CMD INSTALL pkgname}.  To check the package
(including running all the examples on its help pages and in its test
suite, if any) use @command{R CMD check pkgname}: see the @emph{`Writing R
Extensions'} manual.

Note that this is rather tricky; please do ensure that you have followed
the instructions @strong{exactly}.  At least 90% of the questions asked
are because people have not done so.


@node I don't have permission to write to the @RWVER@\library directory, The packages I installed do not appear in the HTML help system, Can I install packages into libraries in this version?, Packages
@section I don't have permission to write to the @file{@RWVER@\library} directory.

You can install packages anywhere and use the environment variable
@env{R_LIBS} (@pxref{How do I set environment variables?}) to point to
the library location(s).

Suppose your packages are installed in @file{p:\myRlib}.  Then you can
EITHER

@example
set the environment variable R_LIBS to p:\myRlib before starting R
@end example

OR use a package by, e.g.

@example
library(mypkg, lib.loc="p:/myRlib")
@end example



@node The packages I installed do not appear in the HTML help system, My functions are not found by the HTML help search system., I don't have permission to write to the @RWVER@\library directory, Packages
@section The packages I installed do not appear in the HTML help system.

To update the HTML indices after you have installed a pre-compiled
package, run at the R prompt.

@example
> link.html.help()
@end example

This is done automatically when installing from the Packages menu or
by @code{install.packages()}, and when @code{help.start} is run,
@emph{provided} you have write permission in @file{@RWVER@}.  If you do not
have sufficient permission, you will get warnings and the packages you
install will not appear in the list of packages or the search system.


@node My functions are not found by the HTML help search system., Loading a package fails., The packages I installed do not appear in the HTML help system, Packages
@section My functions are not found by the HTML help search system.

The following conditions need to hold for functions in a package you installed.

@itemize @bullet
@item the package contains a @file{CONTENTS} file in its top-level directory.
@item you updated the indices as described in the answer to Q4.3 after
installing the package.
@end itemize

@noindent
If those hold, this works for us.  Note that if you were unable to
update the indices (for which you need write permission in the
@file{@RWVER@} directory), only the functions in packages installed in
the main library will be found.

If the help search system does not work @emph{at all}, this probably
indicates that Java support is either not installed or not enabled in
your browser.  The search page contains a link to the appropriate
section in the @emph{R Installation and Administration} manual.


@node Loading a package fails., Package TclTk does not work., My functions are not found by the HTML help search system., Packages
@section Loading a package fails.

Is the package compiled for this version of R?   Many of the packages
need to be compiled for a fairly recent version.

You can tell the version the package was compiled for by looking at the
@samp{Built:} line in its @file{DESCRIPTION} file or at the
@samp{Version} tab of its DLL in the @file{libs} directory.
(Right-click on the DLL in Windows Explorer and select @samp{Version}
tab of the @samp{Properties}, or use the @code{DLL.version} function
inside R.)


@node Package TclTk does not work., Hyperlinks in HTML do not work., Loading a package fails., Packages
@section Package TclTk does not work.

For package @code{tcltk} to work (try @code{demo(tkdensity)} or
@code{demo(tkttest)} after @code{library(tcltk)}) you need to have Tcl
installed.  This is an optional part of the installation although it is
selected by default.  If the message is
@example
Tcl/Tk support files were not installed
@end example
the optional files were not installed, and you need to go back to the
installer and install them.

Alternatively, if you have the environment variable @env{MY_TCLTK} set
to a non-empty value, it is assumed that you want to use a different
Tcl/Tk 8.4.x installation, and that this is set up correctly (with the
DLLs in your path and @env{TCL_LIBRARY} set).  In that case you do not
need the Tcl/Tk support files installed (but they can be).  Note that
you do need 8.4.x and not 8.3.x.  (If you build R from the sources
yourself you can configure it to use 8.3.x.)


@node  Hyperlinks in HTML do not work., update.packages() fails, Package TclTk does not work., Packages
@section Hyperlinks in HTML sometimes do not work.

They may well not work between packages installed in different
libraries.  This is solved under Unix using symbolic links which Windows
does not implement.

With HTML, @code{help.start()} fixes up links to the most of the
standard packages if it has write permission in the main library tree.
Not even these work for Compiled HTML help.

Currently links to the @code{base}, @code{datasets}, @code{utils},
@code{grDevices}, @code{graphics}, @code{methods} and @code{stats}
packages are fixed.


@node update.packages() fails, How do I add to the list of repositories?, Hyperlinks in HTML do not work., Packages
@section @code{update.packages()} fails.

You may not be able to update a package which is in use: Windows `locks'
the package's DLL when it is loaded.  So use @code{update.packages()}
(or the menu equivalent) in a new session.

If you put @code{library(foo)} in your @file{.Rprofile} you will need to
start R with @option{--vanilla} to be able to update package @code{foo}.
If you set @env{R_DEFAULT_PACKAGES} to include @code{foo}, you will
need to unset it temporarily.

It has been reported that some other software has interfered with the
installation process by preventing the renaming of temporary files,
@emph{Google Desktop} being a known example.


@node How do I add to the list of repositories?,  , update.packages() fails, Packages
@section How do I add to the list of repositories?

as shown in the @code{Select repositories...} item on the
@code{Packages} menu?

This reads from the tab-delimited file @file{R_HOME/etc/repositories},
which you can edit, or put a modified copy at @file{.R/repositories}
in your HOME directory (@pxref{What are HOME and working directories?}).

@newchap{}
@node Windows Features, Workspaces, Packages, Top
@chapter Windows Features

@menu
* What should I expect to behave differently from the Unix version::  
* I hear about some nifty features.::  
* Circles appear as ovals on screen::  
* How do I move focus to a graphics window or the console?::  
@end menu


@node  What should I expect to behave differently from the Unix version, I hear about some nifty features., Windows Features, Windows Features
@section  What should I expect to behave differently from the Unix version of R?

@itemize @bullet
@item R commands can be interrupted by @key{Esc} in @command{Rgui.exe}
and @key{Ctrl-break} or @key{Ctrl-C} in @command{Rterm.exe}:
@key{Ctrl-C} is used for copying in the GUI version.

@item Command-line editing is always available, but is simpler than the
readline-based editing on Unix.  For @command{Rgui.exe}, the menu item `Help |
Console' will give details.  For @command{Rterm.exe} see file
@file{README.rterm}.

@item Using @code{help.start()} does not automatically send help
requests to the browser: use @code{options(htmlhelp=TRUE)} to turn this on.

@item The HTML help system has limitations in supporting cross-library links.

@item Paths to files (e.g. in @code{source()}) can be specified with
either "/" or "\\".

@item @code{system()} is slightly different: see its help page and that
of @code{shell()}.
@end itemize


@node I hear about some nifty features., Circles appear as ovals on screen, What should I expect to behave differently from the Unix version, Windows Features
@section I hear about some nifty features: please tell me about them!

You have read the file @file{README.@RWVER@}? There are file menus on
the R console, pager and graphics windows.  You can source and save from
those menus, and copy the graphics to @code{png}, @code{jpeg},
@code{bmp}, @code{postscript}, @code{PDF} or @code{metafile}.  There are
right-click menus giving shortcuts to menu items, and optionally
toolbars with buttons giving shortcuts to frequent operations.

If you resize the R console the @code{options(width=)} is automatically
set to the console width (unless disabled in the configuration file).

The graphics has a history mechanism.  As @file{README.@RWVER@} says:

@quotation
`The History menu allows the recording of plots.  When plots have been
recorded they can be reviewed by @key{PgUp} and @key{PgDn}, saved and
replaced.  Recording can be turned on automatically (the Recording item
on the list) or individual plots can be added (Add or the @key{INS}
key).  The whole plot history can be saved to or retrieved from an R
variable in the global environment.
  The format of recorded plots may change between R versions.
  Recorded plots should @strong{not} be used as a permanent
  storage format for R plots.

There is only one graphics history shared by all the windows devices.'
@end quotation

@noindent
The R console and graphics windows have configuration files stored in
the @file{RHOME\etc} directory called @file{Rconsole} and @file{Rdevga};
you can keep personal copies in your @file{HOME} directory.  They contain
comments which should suffice for you to edit them to your
preferences.  For more details see @code{?Rconsole}.
There is a GUI preferences editor invoked from the @code{Edit} menu which
can be used to edit the file @file{Rconsole}.


@node Circles appear as ovals on screen, How do I move focus to a graphics window or the console?, I hear about some nifty features., Windows Features
@section Circles appear as ovals on screen.

The graphics system asks Windows for the number of pixels per inch in
the X and Y directions, and uses that to size graphics (which in R are
in units of inches).  Sometimes the answer is a complete invention, and
in any case Windows will not know exactly how the horizontal and
vertical size have been set on a CRT.  You can specify correct values
either in the call to @code{windows} or as options: see @code{?windows}.
(Typically these are of the order of 100.)

On one of our systems, the screen height was reported as 240mm, and the
width as 300mm in 1280 x 1024 mode and 320mm in 1280 x 960 and 1600 x 1200
modes.  In fact it is a 21" monitor and 400mm x 300mm!

@node How do I move focus to a graphics window or the console?,  , Circles appear as ovals on screen, Windows Features
@section How do I move focus to a graphics window or the console?

You may want to do this from within a function, for example when calling
@samp{identify} or @samp{readline}.  Use the function
@samp{bringToTop()}.  With its default argument it brings the active
graphics window to the top and gives it focus.  With argument @samp{-1}
it brings the console to the top and gives it focus.

This works for @command{Rgui.exe} in MDI and SDI modes, and can be used for
graphics windows from @command{Rterm.exe} (although Windows may not always
act on it).

@newchap{}
@node Workspaces, The R Console, Windows Features, Top
@chapter Workspaces


@menu
* My workspace gets saved in a strange place.  How do I stop this?::  
* How do I store my workspace in a different place?::  
* Can I load workspaces saved under Unix/GNU-Linux or MacOS?::  
@end menu


@node My workspace gets saved in a strange place.  How do I stop this?, How do I store my workspace in a different place?, Workspaces, Workspaces
@section My workspace gets saved in a strange place: how do I stop this?

Have you changed the working directory?: see Q6.2.


@node How do I store my workspace in a different place?, Can I load workspaces saved under Unix/GNU-Linux or MacOS?, My workspace gets saved in a strange place.  How do I stop this?, Workspaces
@section How do I store my workspace in a different place?

Use the `File | Change Dir...' menu item to select a new working
directory: this defaults to the last directory you loaded a file
from.  The workspace is saved in the working directory.  You can also
save a snapshot of the workspace from the `Save Workspace...' menu item.

From the command line you can change the working directory by the
function @code{setwd}: see its help page.


@node Can I load workspaces saved under Unix/GNU-Linux or MacOS?,  , How do I store my workspace in a different place?, Workspaces
@section Can I load workspaces saved under Unix/GNU-Linux or MacOS?

Yes.  All ports of R use the same format for workspaces, so they are
interchangeable (for the same 2.x.? version of R, at least).


@newchap{}
@node The R Console, Building from Source, Workspaces, Top
@chapter The R Console


@menu
* The output to the console seems to be delayed::  
* Long lines seem to be truncated.::  
@end menu




@node The output to the console seems to be delayed, Long lines seem to be truncated., The R Console, The R Console
@section When using Rgui the output to the console seems to be delayed.

This is deliberate: the console output is buffered and re-written in
chunks to be faster and less distracting.  You can turn buffering off or
on from the `Misc' menu or the right-click menu: @key{Ctrl-W} toggles
the setting.

If you are sourcing R code or writing from a function, there is another
option.  A call to the R function @code{flush.console()} will write out
the buffer and so update the console.


@node Long lines seem to be truncated.,  , The output to the console seems to be delayed, The R Console
@section Long lines in the console or pager are truncated.

They only @strong{seem} to be truncated: that $ at the end indicates you
can scroll the window to see the rest of the line.  Use the horizontal
scrollbar or the @key{CTRL + left/right arrow} keys to scroll
horizontally. (The @key{left/right arrow} keys work in the pager too.)



@newchap{}
@node Building from Source,  , The R Console, Top
@chapter Building from Source

@menu
* How can I compile R from source?::  
* Can I use a fast BLAS?::      
* How do I include compiled C code?::  
* How do I debug code that I have compiled and dyn.load-ed?::  
* How do I include C++ code?::  
* The output from my C code disappears.::  
* The output from my Fortran code disappears.::  
* The console freezes when my compiled code is running::  
@end menu


@node How can I compile R from source?, Can I use a fast BLAS?, Building from Source, Building from Source
@section How can I compile R from source?

See the `R Installation and Administration' manual.

Note that building under Windows 64 is not supported as no suitable
non-commercial compiler is yet available.  There is a glimmer of hope
that this will change in the not too far distance future: see
@url{http://sourceforge.net/mailarchive/forum.php?thread_id=30624855&forum_id=5119}.
You are of course welcome to try a commercial compiler, but we will not
be distributing a port for which users cannot easily add packages.

@node Can I use a fast BLAS?, How do I include compiled C code?, How can I compile R from source?, Building from Source
@section  Can I use a fast BLAS?

Fast BLAS (Basic Linear Algebra Subprograms,
@uref{http://www.netlib.org/blas/faq.html}) routines are used to speed
up numerical linear algebra.  There is support in the R sources for the
`tuned' BLAS called ATLAS (@uref{http://math-atlas.sourceforge.net}).
The savings can be appreciable: on a 2.6GHz P4 and a 1000 x 1000 matrix
@code{svd} took 16.2 sec with the standard BLAS and 7.8 sec with ATLAS.
Because ATLAS is tuned to a particular chip we can't use it generally:
the optimal routines for a P4 or an Athlon XP are quite different and
neither will run at all on a PII.

BLAS support is supplied by the single DLL @file{R_HOME\bin\Rblas.dll},
and you can add a fast BLAS just by replacing that.  Replacements for
some of the more common chips are available on CRAN in directory
@file{bin/windows/contrib/ATLAS}.  See the  @emph{R
Installation and Administration Manual} for how to build an ATLAS
@file{Rblas.dll} tuned to your system using the R sources.

An alternative for those building from sources is to use AMD's Core Math
Library (ACML) @url{http://www.amd.com/acml} (see the @emph{R
Installation and Administration Manual} for details).  This does seem to
run on a wide range of CPUs at speeds comparable to ATLAS, but the
license conditions mean we cannot redistribute it.


@node  How do I include compiled C code?, How do I debug code that I have compiled and dyn.load-ed?, Can I use a fast BLAS?, Building from Source
@section How do I include compiled C code?

We strongly encourage you to do this @emph{via} building an R package:
see the @emph{`Writing R Extensions'} manual.  In any event you should
install the parts of the R system for building R packages (installed by
default), and get and install the tools (including Perl) and compilers
mentioned in the @emph{`R Installation and Administration'} manual.
Then you can use
@example
...\bin\R CMD SHLIB foo.c bar.f
@end example
@noindent
to make @file{foo.dll}.  Use @command{...\bin\R CMD SHLIB --help} for
further options, or see @code{?SHLIB}.

If you want to use Visual C++, Borland C++ or other compilers, see the
appropriate section in @file{README.packages}.



@node How do I debug code that I have compiled and dyn.load-ed?, How do I include C++ code?, How do I include compiled C code?, Building from Source
@section How do I debug code that I have compiled and dyn.load-ed?

You will need a suitable version of @command{gdb}: we normally use that
from the Cygwin distribution.  Debugging under Windows is often a
fraught process, and sometimes does not work at all.  If all you need is
a @emph{just-in-time} debugger to catch crashes, consider
@code{Dr. Mingw} from the @code{mingw-utils} bundle on
@uref{http://www.mingw.org} (see also
@uref{http://jrfonseca.dyndns.org/projects/gnu-win32/software/drmingw/}).
That will be able to pinpoint the error, most effectively if you build a
version of R with debugging information as described below.

First, build a version of the R system with debugging information by

@example
make clean
make DEBUG=T
@end example

@noindent
and make a debug version of your package by either

@example
make pkgclean-mypkg
make DEBUG=T pkg-mypkg
@end example

@noindent
or

@example
Rcmd install -c mypkg
set DEBUG=T
Rcmd install mypkg
@end example

@noindent
Then you can debug by

@example
gdb /path/to/@RWVER@/bin/Rgui.exe
@end example

@noindent
However, note

@itemize @bullet
@item
@command{gdb} will only be able to find the source code if we run in the
location where the source was compiled (@file{@RWVER@/src/gnuwin32} for
the main system, @file{@RWVER@/src/library/mypkg/src} for a package),
unless told otherwise by the @command{directory} command.  It is most
convenient to set a list of code locations via @command{directory} commands
in the file @file{.gdbinit} in the directory from which @command{gdb} is
run.

@item
It is only possible to set breakpoints in a DLL after the DLL has
been loaded.  So a way to examine the function @code{tukeyline} in
package @code{stats} might be

@example
     gdb ../../../../bin/Rgui.exe
     (gdb) break WinMain
     (gdb) run
     [ stops with R.dll loaded ]
     (gdb) break R_ReadConsole
     (gdb) continue
     [ stops with console running ]
     (gdb) continue
     Rconsole> library(stats)
     (gdb) break tukeyline
     (gdb) clear R_ReadConsole
     (gdb) continue
     Rconsole> example(line)
     ...
@end example

@noindent
Alternatively, in Rgui you can use the `Misc|Break to debugger' menu item
after your DLL is loaded.  The C function call @code{breaktodebugger()}
will do the same thing.

@item
Fortran symbols need an underline appended.

@item
Windows has little support for signals, so the Unix idea of running a
program under a debugger and sending it a signal to interrupt it and
drop control back to the debugger does not work with a @code{mingw}
version of @command{gdb}.  It does often work with the @code{cygwin} version.

@item
Other debuggers than @command{gdb} can be used.  We have successfully
used the @command{insight} front-end to @command{gdb} (see
@uref{http://sources.redhat.com/insight/}: there is a @code{mingw} port
on their site), and Borland's debugger with a DLL compiled in a Borland
compiler.

@end itemize

See
@uref{http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/gdb.shtml}
for some further details.


@node  How do I include C++ code?, The output from my C code disappears., How do I debug code that I have compiled and dyn.load-ed?, Building from Source
@section  How do I include C++ code?

You need to do two things:

(a) Write a wrapper to export the symbols you want to call from R as
 @code{extern "C"}.

(b) Include the C++ libraries in the link to make the DLL.  Suppose
@file{X.cc} contains your C++ code, and @file{X_main.cc} is the wrapper,
as in the example in @emph{`Writing R Extensions'}.  Then build the DLL by
(@command{gcc})

@example
...\bin\R CMD SHLIB X.cc X_main.cc
@end example

@noindent
or (VC++, which requires extension @code{.cpp})

@example
cl /MT /c X.cpp X_main.cpp
link /dll /out:X.dll /export:X_main X.obj X_main.obj
@end example

@noindent
or (Borland C++, which also requires extension @code{.cpp})

@example
bcc32 -u- -WDE X.cpp X_main.cpp
@end example

@noindent
and call the entry point(s) in @code{X_R}, such as @code{X_main}.
Construction of static variables will occur when the DLL is loaded, and
destruction when the DLL is unloaded, usually when R terminates.

Note that you will not see the messages from this example in the GUI
console: see the next section.

This example used to be in package @file{cxx_0.0-x.tar.gz} in the
@file{src/contrib/Devel} section on CRAN, and could be compiled as a
package in the usual way on Windows.


@node  The output from my C code disappears., The output from my Fortran code disappears., How do I include C++ code?, Building from Source
@section  The output from my C code disappears.  Why?

The @command{Rgui.exe} console is a Windows application: writing to
@code{stdout} or @code{stderr} will not produce output in the
console.  (This will work with @command{Rterm.exe}.)  Use @code{Rprintf} or
@code{REprintf} instead.  These are declared in header file
@file{R_ext/PrtUtil.h}.

Note that output from the console is delayed (@pxref{The output to the
console seems to be delayed}), so that you will not normally see any
output before returning to the R prompt.


@node  The output from my Fortran code disappears., The console freezes when my compiled code is running, The output from my C code disappears., Building from Source
@section  The output from my Fortran code disappears.  Why?

Writing to Fortran output writes to a file, not the @command{Rgui} console.
Use one of the subroutines @code{dblepr}, @code{intpr} or @code{realpr}
documented in the @emph{`Writing R Extensions'} manual.

Note that output from the console is delayed (@pxref{The output to the
console seems to be delayed}), so that you will not normally see any
output before returning to the R prompt even when using the @code{xxxpr}
subroutines.


@node  The console freezes when my compiled code is running,  , The output from my Fortran code disappears., Building from Source
@section The console freezes when my compiled code is running.

The console, pagers and graphics window all run in the same thread
as the R engine.  To allow the console etc to respond to Windows events,
call @code{R_ProcessEvents()} periodically from your compiled code.
If you want output to be updated on the console, call
@code{R_FlushConsole()} and then @code{R_ProcessEvents()}.

@set LASTEDIT 2006 April 03
@ifhtml
@html
<hr WIDTH="100%">
<address>
Last edited @value{LASTEDIT}: comments to
<code><a href="mailto:Rwindows@@R-project.org">Rwindows@@r-project.org</a></code>
</address>
@end html
@end ifhtml
@ifinfo
@sp 2
Last edited @value{LASTEDIT}: comments to <Rwindows@@r-project.org>
@end ifinfo

@bye

@c Local Variables: ***
@c mode: TeXinfo ***
@c End: ***
