2

I want to develop a variable as a parameter in a function, like

W<-"Hi I am working on R"  #W is the variable, it is the text file

The function I want to develop is like getText=function(W){} this function get call another script.

Then W can work as a global variable everywhere in the script appearing W can show the "Hi I am working on R"

llrs
  • 3,308
  • 35
  • 68
Eva
  • 917
  • 4
  • 18
  • 23
  • Is `?source` what you are looking for? You can define W in a given file and then in your other script include `source("scriptThatContainsW.R")`. Alternatively - you can define `W` in your `Rprofile` page so it's always available.. – Chase Jun 13 '11 at 12:40
  • You can use `assign(..., pos = globalenv())` to assign variables with their corresponding values to the global environment that is accessible from "everywhere". – Roman Luštrik Jun 13 '11 at 12:50
  • this assign(..., pos = globalenv()) works, I have tried thx! – Eva Jun 14 '11 at 02:10

2 Answers2

4

I think you should be able to achieve this by using the following syntax:

W<<-"Hi I am working on R"

using <<- instead of <- allows you to access your variable from anywhere unless you mask it in a function with another variable

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
Jorjani
  • 827
  • 1
  • 16
  • 31
2

There are multiple possibilities to achieve this, depending on how far you want to go. The most easy way is simply to assign the variable W in the global environment, and call the other scripts from there. This does defeat the purpose of a function though.

The most straight-forward solution would to use the option local=TRUE in the source call. This allows the sourced script to search in the local environment of the function.

An illustration : given a file Myscript.r :

cat("In script : ",x," ",W,"\n")

afunc <- function(y){
  cat("within function : ",y," ",W,"\n")
}
afunc(x)

You can define a function that sources it locally :

mynewfunc <- function(x){
    W <- "My W"
    source("Myscript.R",local=TRUE)
}

avar <- "Working in R"
mynewfunc(avar)

And does what it needs to do : It finds the x that got passed from the global environment to mynewfunc, and it finds the W assigned in the local environment of mynewfunc :

In script :  Working in R   My W 
within function : Working in R   My W 

The most robust option would be though to do this in a package. Basically you declare them in one of the source files of your package. The constant W will be loaded and can be used by all functions in that package. If you work with namespaces, you can decide whether you want to export the constant, or keep it hidden so it can only be accessed by your own functions. More information on this you'll find in "Writing R extensions".

Sourcing script files within functions is a less robust way of dealing with different functions. You can get very unexpected results, depending on the vars in your workspace. If you want to try this, I definitely suggest you to start looking into constructing your own packages. It's pretty straightforward if there's no C code involved.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263