Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make object created within function usable outside

Tags:

I created a function which produces a matrix as a result, but I can't figure out how to make the output of this function usable outside of the function environment, so that I could for instance save it in csv file.

My code for function is the following:

created function which takes url's from specific site and returns page title:

getTitle <- function(url) {   webpage <- readLines(url)   first.row <- webpage[1]   start <- regexpr("<title>", first.row)   end <- regexpr("</title>", first.row)   title <- substr(first.row,start+7,end-1)   return(title) } 

created function which takes vector of urls and returns n*2 matrix with urls and page titles:

getTitles <- function(pages) {   my.matrix <- matrix(NA, ncol=2, nrow=nrow(pages))   for (i in seq_along(1:nrow(pages))) {     my.matrix[i,1] <- as.character(pages[i,])     my.matrix[i,2] <- getTitle(as.character(pages[i,])) }   return(my.matrix)   print(my.matrix)} 

After running this functions on a sample file from here http://goo.gl/D9lLZ which I import with read.csv function and name "mypages" I get the following output:

getTitles(mypages)      [,1]                                               [,2]                                                 [1,] "http://support.google.com/adwords/answer/1704395" "Create your first ad campaign - AdWords Help"       [2,] "http://support.google.com/adwords/answer/1704424" "How costs are calculated in AdWords - AdWords Help" [3,] "http://support.google.com/adwords/answer/2375470" "Organizing your account for success - AdWords Help" 

This is exactly what I need, but I'd love to be able to export this output to csv file or reuse for further manipulations. However, when I try to print(my.matrix), I am getting an error saying "Error: object 'my.matrix' not found"

I feel like it's quite basic gap in my knowledge, but have not been working with R for a while and could not solve that.

Thanks! Sergey

like image 235
Sergey Samusev Avatar asked Apr 14 '13 00:04

Sergey Samusev


People also ask

What is global variable in r?

Variables that are created outside of a function are known as global variables. Global variables can be used by everyone, both inside of functions and outside.

How do I create a data object in R?

Creating a new object is as easy as typing the object's name and assigning a value to it. There are multiple ways to assign values to objects in R. As in many computer languages you can use the equal sign (=) as an assignment operator.

How do you define a function in R?

To declare a user-defined function in R, we use the keyword function . The syntax is as follows: function_name <- function(parameters){ function body } Above, the main components of an R function are: function name, function parameters, and function body.


1 Answers

That's easy: use <<- for assignment to a global.

But then again, global assignment is evil and not functional. Maybe you'd rather return a list with several results from your function? Looking at your code, it seems that your second function may confuse the return and print. Make sure you return the correct data structure.

like image 144
Dirk Eddelbuettel Avatar answered Oct 13 '22 10:10

Dirk Eddelbuettel