Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make object created within function usable outside
    primarykey
    data
    text
    <p>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. </p> <p>My code for function is the following:</p> <h2>created function which takes url's from specific site and returns page title:</h2> <pre><code>getTitle &lt;- function(url) { webpage &lt;- readLines(url) first.row &lt;- webpage[1] start &lt;- regexpr("&lt;title&gt;", first.row) end &lt;- regexpr("&lt;/title&gt;", first.row) title &lt;- substr(first.row,start+7,end-1) return(title) } </code></pre> <h2>created function which takes vector of urls and returns n*2 matrix with urls and page titles:</h2> <pre><code>getTitles &lt;- function(pages) { my.matrix &lt;- matrix(NA, ncol=2, nrow=nrow(pages)) for (i in seq_along(1:nrow(pages))) { my.matrix[i,1] &lt;- as.character(pages[i,]) my.matrix[i,2] &lt;- getTitle(as.character(pages[i,])) } return(my.matrix) print(my.matrix)} </code></pre> <p>After running this functions on a sample file from here <a href="http://goo.gl/D9lLZ">http://goo.gl/D9lLZ</a> which I import with read.csv function and name "mypages" I get the following output:</p> <pre><code>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" </code></pre> <p>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"</p> <p>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.</p> <p>Thanks! Sergey</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload