I've made a presentation about FRBF, an algorithm that I've just implemented in R, and I've used a small trick: I've hidden the warnings.
This is useful for demos and presentations because this way people are not distracted by non-relevant information and they can focus on what is really important.
All that is required is to use options(warn=-1) in the beginning of the script.
./M6
Showing posts with label r. Show all posts
Showing posts with label r. Show all posts
Clear R Console
I've been searching for a way to clear the R console, something like a cls() function.
But it seems R does not provide one.
I've found and tried a solution for Windows that emulated the CTRL+L command, which clears the console, but it simply did not work.
So, after some more research I've found this extremely easy, quick and dirty solution: cat(rep("\n",128)).
It does precisely what I need and is system independent.
./M6
But it seems R does not provide one.
I've found and tried a solution for Windows that emulated the CTRL+L command, which clears the console, but it simply did not work.
So, after some more research I've found this extremely easy, quick and dirty solution: cat(rep("\n",128)).
It does precisely what I need and is system independent.
./M6
Reorder columns in R
I got the need to reorder the column order or a data frame in R and after searching a little, I've found a very easy way to do it.
Having a data frame named test like
all that is required is to reassign the data frame using a new column order, like
And that solves the problem.
This seems to work both with a data frame and a matrix.
./M6
Having a data frame named test like
a b c d e
[1,] 1 11 21 31 41
[2,] 2 12 22 32 42
[3,] 3 13 23 33 43
[4,] 4 14 24 34 44
[5,] 5 15 25 35 45
all that is required is to reassign the data frame using a new column order, like
new_column_order <- c("b", "c", "a", "e", "d")
test <- test[,new_column_order]
And that solves the problem.
This seems to work both with a data frame and a matrix.
./M6
Introduction to the R Programming Environment
I've found an easy to follow and understand R introduction: An Introduction to the R Programming Environment.
./M6
./M6
Current time as numeric value in R
To get the current time, just use the Sys.time() function, see the Sys package for more information.
To get the current time as a numeric value in R, just use unclass(Sys.time()).
This seems to be higher accurate, 0.01 seconds.
./M6
To get the current time as a numeric value in R, just use unclass(Sys.time()).
This seems to be higher accurate, 0.01 seconds.
./M6
R Package with S4 Objects
I've finished the development of the (beta version) clustering algorithm on R for my MSc thesis and I'm releasing it for tests.
I've developed it mainly in Windows, but when it comes to the packaging part, I've switched to Linux. This is mainly because R packaging in Windows implies to install a lot of base Linux applications and tools on Windows. For those that wish to go that road, here's what you have to do: read Making tutorial about R Packages Under Windows: A Tutorial by Peter Rossi.
For everyone else, just boot your Linux.
Before I start, I took a look at Writting R Extensions. This is the first step for all that wish to build an R extension. A fast and easy way to know how to pack, is to read the An Introduction to the R package mechanism.
My implementation uses S4 objects, and that was a problem, since I had all kinds of warnings and errors during the package tests and installation. Here's the transcriptions of the post I've sent to the R-Help list asking for help:
But, unfortunately, no help came from there...
I've never stopped the search for the answer, and I've finally found it. It's actually quite easy.
Instead of packing it from the current environment, I've just passed the package.skeleton the list of source files to build the package. Here's the small R script I've done to automate the packaging procedure for my "Remora" package:
from now on {package}, and edit the following files:
/man/ are tex files and will be compiled to provide the functions help when invoked by the user.
It's only necessary to document the classes and functions that will be exported, i. e. exported in the NAMESPACE file, since all the others will not be visible to the user. All the other .Rd files may be deleted.
After the package has been created, I've tested with the "R CMD check {package} " command. My package name is "remora", so my command was " sudo R CMD check remora".
I had to run this command with the administration role, so I prefixed it with the "sudo " command. I believe this is a characteristic of the Kubuntu distribution.
Finally I've build the package with the "R CMD build {package}" command that created the tar.gz file for distribution.
To install it, just use the "R CMD INSTALL {package}" command. I've entered R and it worked fine.
To uninstall just execute "R CMD REMOVE {package}".
./M6
I've developed it mainly in Windows, but when it comes to the packaging part, I've switched to Linux. This is mainly because R packaging in Windows implies to install a lot of base Linux applications and tools on Windows. For those that wish to go that road, here's what you have to do: read Making tutorial about R Packages Under Windows: A Tutorial by Peter Rossi.
For everyone else, just boot your Linux.
Before I start, I took a look at Writting R Extensions. This is the first step for all that wish to build an R extension. A fast and easy way to know how to pack, is to read the An Introduction to the R package mechanism.
My implementation uses S4 objects, and that was a problem, since I had all kinds of warnings and errors during the package tests and installation. Here's the transcriptions of the post I've sent to the R-Help list asking for help:
Here's what I do:
*1.* in R console, I do and get:
> package.skeleton(name='remora')remora-package
Creating directories ...
Creating DESCRIPTION ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './remora/Read-and-delete-me'.
Warning messages:
1: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R", :
deparse of an S4 object will not be
source()able
2: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R", :
deparse of an S4 object will not be
source()able
3: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R", :
deparse of an S4 object will not be
source()able
4: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R", :
deparse of an S4 object will not be source()able
I don't know why I get these warnings. I've followed R
implementation rules and the S4 objects work fine.
*2.* Performing the 'R CMD build remora' command I get:
* checking for file 'remora/DESCRIPTION' ... OK
* preparing 'remora':
* checking DESCRIPTION meta-information ... OK
* removing junk files
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building 'remora_1.0.tar.gz'
And the remora_1.0.tar.gz file seems ok.
*
3.* Performing the 'R CMD check remora' command I get:
* checking for working pdflatex ...sh: pdflatex: not found
NO
* checking for working latex ...sh: latex: not found
NO
* using log directory '/home/fmm/thesis/R/src/remora.Rcheck'
* using R version 2.8.1 (2008-12-22)
* using session charset: UTF-8
* checking for file 'remora/DESCRIPTION' ... OK
* checking extension type ... Package
* this is package 'remora' version '1.0'
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking for executable files ... OK
* checking whether package 'remora' can be installed ...
ERROR
Installation failed.
See '/home/fmm/thesis/R/src/remora.Rcheck/00install.out'
for details.
*4.* the log file contains:
* Installing *source* package 'remora'
...
**
R
**
data
** preparing package for lazy
loading
Error in parse(n = -1, file = file) : unexpected '<' at 745:
`.__C__remoraConfiguration` <- 746: <> -> code2LazyLoadDB
-> sys.source -> parse Execution halted
ERROR: lazy loading failed for package
'remora'
** Removing
'/home/fmm/thesis/R/src/remora.Rcheck/remora'
fmm@Darkmaster:~/thesis/R/src$ grep -i __C__remoraConfiguration *
fmm@Darkmaster:~/thesis/R/src$ grep -i __C__remoraConfiguration */*
remora.Rcheck/00install.out:745: `.__C__remoraConfiguration`
<- remoraConfiguration is a constructor for the
remoraConfiguration S4 object.
# # Class configuration definition.
# CLASS_REMORA_CONFIGURATION <- "remoraConfiguration" class_configuration
<- setClass(CLASS_REMORA_CONFIGURATION, representation(
number_clusters = "numeric", class_name = "character",
weighting_function="character", scale_variance="logical", s="numeric",
d="numeric", alfa="numeric", eta = "numeric", niter="numeric",
niter_changes="numeric", perform_sum="logical", verbose="logical"),
prototype = list(number_clusters=numeric(), class_name=character(),
weighting_function=character(), scale_variance=logical(),
s=numeric(), d=numeric(), alfa=numeric(), eta=numeric(), niter=numeric(),
niter_changes=numeric(), perform_sum=logical(), verbose=logical()))
# # Builds a Remora configuration object.
# # [...]
# remoraConfiguration <- function(number_clusters, class_name,
weighting_function = FUNCTION_REMORA_EUCLIDEAN,
scale_variance=TRUE, s_value = 0.001, d = 0.3, alfa = 0.9, eta = 0.01,
niter=-1, niter_changes=5, perform_sum = TRUE, verbose=FALSE)
{
result_configuration <- new(CLASS_REMORA_CONFIGURATION)
[...]
result_configuration
}
But, unfortunately, no help came from there...
I've never stopped the search for the answer, and I've finally found it. It's actually quite easy.
Instead of packing it from the current environment, I've just passed the package.skeleton the list of source files to build the package. Here's the small R script I've done to automate the packaging procedure for my "Remora" package:
Now it's time to go to the directory created, with the name of the package,
cat('\nPacking Remora...\n')
file_lst <- character(5)
file_lst[1] <- '/home/m6/thesis/R/src/1_classes.r'
file_lst[2] <- '/home/m6/thesis/R/src/2_common.r'
file_lst[3] <- '/home/m6/thesis/R/src/3_model.r'
file_lst[4] <- '/home/m6/thesis/R/src/4_predict.r'
file_lst[5] <- '/home/m6/thesis/R/src/5_main.r'
package.skeleton(name = "remora", force = TRUE, namespace = TRUE,
code_files = file_lst)
cat('\nDone.\n')
{package} /DESCRIPTION , the description of the package;- {package}
/NAMESPACE , the list of functions and classes to export to the user; - {package}
/ man/ {package}-package.Rd, the package help file, the \examples section must provide executable code, since R check command will execute this code; - {package}
/man/ {class_name}-class.Rd, the classes help files; - {package}
/man/ {function_name}.Rd, the functions help files;
It's only necessary to document the classes and functions that will be exported, i. e. exported in the
After the package has been created, I've tested with the "R CMD check {package}
I had to run this command with the administration role, so I prefixed it with the
Finally I've build the package with the
To install it, just use the
To uninstall just execute
./M6
Debug in R
Development in R usually is performed using JGR, or Tinn-R, editors and the R console.
Since these are no IDEs for R development, this is usually the best one can have.
So, how does one debug a program in R?
The usual answer is: with print.
But the correct answer is: using the browser()function.
The browser() function works as a break point marker.
Just write browser() where a break point is needed and run the program.
When browser() is interpreted, the program hang its execution and a new command line prompt will be provided, Browse[1]>.
This command line allows one to interact with the variables available in the scope where the program is temporary hanged.
This debug command line accepts the following debug commands:
./M6
Since these are no IDEs for R development, this is usually the best one can have.
So, how does one debug a program in R?
The usual answer is: with print.
But the correct answer is: using the browser()function.
The browser() function works as a break point marker.
Just write browser() where a break point is needed and run the program.
When browser() is interpreted, the program hang its execution and a new command line prompt will be provided, Browse[1]>.
This command line allows one to interact with the variables available in the scope where the program is temporary hanged.
This debug command line accepts the following debug commands:
- n: next, advances to the next line, allows step-by-step.
- c: continue, continues the execution, until the program ends or a new browser() function is found.
- traceback(), shows the stack trace function call.
- Q: quit, stop the program execution.
./M6
R on Kubuntu
I'm using R on my MSc thesis, which is about a new clustering algorithm, and I found out that I was unable to install it via Adept.
It may be my fault, but there's other things I seem not to find in Adept.
So, I had to do it "manually". It was easy to find the official information, and it was also easy to install and use it.
To get R, just update the repositories and the get r-base:
$ sudo apt-get update
$ sudo apt-get install r-base
I'm not sure that I really need it, but since I'm going to develop a package, I've also installed the development stuff:
$ sudo apt-get install r-base-dev r-recommended
I've also wanted an R editor, so I've used JGR. In order to use it, I've followed the official installation under Linux.
Java is required, so when there's none, Sun's Java should be installed:
$ sudo apt-get install sun-java6-jdk
To install JGR, it's necessary to enable Java support in R:
$ sudo R CMD javareconf
And then install the JGR library:
$ sudo R
> install.packages('JGR')
After installed, run it:
> library(JGR)
> JGR()
JGR It has a small problem though, it has been installed as root and I seem unable to use it with any other user, so root will be.
So, I've automated the startup of R with JGR with a simple shell script, named runr.sh:
sudo R -f /home/m6/msc/jgr.r
While the jgr.r file contains the JGR start commands
library(JGR)
JGR()
Now, running ./runr.sh starts R and JGR all in one. It's still necessary to login as root, though.
There are alternatives to JGR that require a lot less stress. Tinn-R and R Commander are alternatives, in fact Tinn-R is the editor I elected for R in Windows environment.
For know I'll just stick with JGR because I like to explore alternatives in order to decide which is best.
./M6
It may be my fault, but there's other things I seem not to find in Adept.
So, I had to do it "manually". It was easy to find the official information, and it was also easy to install and use it.
To get R, just update the repositories and the get r-base:
$ sudo apt-get update
$ sudo apt-get install r-base
I'm not sure that I really need it, but since I'm going to develop a package, I've also installed the development stuff:
$ sudo apt-get install r-base-dev r-recommended
I've also wanted an R editor, so I've used JGR. In order to use it, I've followed the official installation under Linux.
Java is required, so when there's none, Sun's Java should be installed:
$ sudo apt-get install sun-java6-jdk
To install JGR, it's necessary to enable Java support in R:
$ sudo R CMD javareconf
And then install the JGR library:
$ sudo R
> install.packages('JGR')
After installed, run it:
> library(JGR)
> JGR()
JGR It has a small problem though, it has been installed as root and I seem unable to use it with any other user, so root will be.
So, I've automated the startup of R with JGR with a simple shell script, named runr.sh:
sudo R -f /home/m6/msc/jgr.r
While the jgr.r file contains the JGR start commands
library(JGR)
JGR()
Now, running ./runr.sh starts R and JGR all in one. It's still necessary to login as root, though.
There are alternatives to JGR that require a lot less stress. Tinn-R and R Commander are alternatives, in fact Tinn-R is the editor I elected for R in Windows environment.
For know I'll just stick with JGR because I like to explore alternatives in order to decide which is best.
./M6
Subscribe to:
Posts (Atom)