From edd at debian.org  Tue Jun  3 20:49:40 2014
From: edd at debian.org (Dirk Eddelbuettel)
Date: Tue, 3 Jun 2014 13:49:40 -0500
Subject: [Rd] setting environmental variable inside R function to call
	C	function
In-Reply-To: <CANVKczMSHxjHb7rjLvUhMVC-zph4B-aOsD+338=VM6H05g+zjQ@mail.gmail.com>
References: <7dd62e4fe5734fe9af02cce995cfdf83@EX-1-HT0.lancs.local>
	<CANVKczMSHxjHb7rjLvUhMVC-zph4B-aOsD+338=VM6H05g+zjQ@mail.gmail.com>
Message-ID: <21390.6340.125923.394030@max.nulle.part>


I suspect the intend is to send a value from R to compiled code.  Which
happens to be a problem for which Rcpp offers a pretty convenient solution.  

And if you really, really want to, you can even program in C inside of an
Rcpp interface:

R> cppFunction("int foo(std::string txt) { Rprintf(\"Var is %s\\n\", txt.c_str()); return 0; }")
R> foo("A")
Var is A
[1] 0
R> 

Dirk



On 3 June 2014 at 19:06, Barry Rowlingson wrote:
| What does c_fun look like? Here's mine:
| 
| #include <stdio.h>
| #include <stdlib.h>
| void c_fun(){
|   printf("TMP is %s\n", getenv("TMP"));
|     }
| 
| and I then do this at the shell prompt:
| 
| R CMD SHLIB c_fun.c
| 
| and this at the R prompt:
| 
| dyn.load("c_fun.so")
| wrapper()
| 
| and I get:
| 
| > wrapper()
| [1] "A"
| TMP is A
| list()
| 
| Is that not what you want?
| 
| tldr; works for me.
| 
| 
| On Tue, Jun 3, 2014 at 5:55 PM, Bowen Meng <bowen.m04 at gmail.com> wrote:
| > wrapper <- function(){
| >   Sys.setenv(TMP="A")
| >   print(Sys.getenv("TMP"))
| >
| >   .C("c_fun")
| > }
| >
| >
| > As the example above, I hope to set an env var $TMP inside the R function
| > "wrapper", which affects the functionality of the C function call "c_fun".
| > Also the print line shows that $TMP is set to be "A", but the function call
| > of "c_fun" was not affected.
| >
| > Does anyone know why and how to correctly set the env var for subsequent
| > functions?
| >
| > Thanks,
| > BW
| >
| >         [[alternative HTML version deleted]]
| >
| > ______________________________________________
| > R-devel at r-project.org mailing list
| > https://stat.ethz.ch/mailman/listinfo/r-devel
| 
| ______________________________________________
| R-devel at r-project.org mailing list
| https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



[Rd] setting environmental variable inside R function to call C function
Dirk Eddelbuettel