How to globally save the output of a function in R?

Asked

Viewed 101 times

4

I have trouble saving function output in R. In the following code, for example, I create an array representing a sudoku game:

quantLinhas <- readline(prompt = 'insira o número de linhas do sudoku: ') #para saber a dimensão do sudoku
quantLinhas <- as.numeric(quantLinhas) #readline salva como string, transferindo para numérico


construcaoSudoku <- function(){
  matrizSudoku <- NA   #atribuindo NA para o rbind funcionar corretamente
  
  for(i in seq(1, quantLinhas)){
    linhaEntrada <- scan()
    matrizSudoku <- rbind(matrizSudoku, linhaEntrada) 
  }
  matrizSudoku <- matrizSudoku[-1,]    #retirando a primeira linha, composta por NAs
  sudoku <- return(matrizSudoku)
}

notice the last line, where I try to return to matrizSudoku and save it in the variable sudoku. Turns out it doesn’t work, apparently, the variable sudoku is generated locally, but not globally. I need it saved globally because it will be used further by another function

  • Apparently, after creating the function, I can run it by assigning it to a variable, in this way: sudoku <- construcaoSudoku() But there is no way to do this within the function itself?

1 answer

7


The principle to follow is this:

In R the functions return the result of last instruction.

So to return a variable, just put it alone on the last instruction.

construcaoSudoku <- function(){
  matrizSudoku <- NA   #atribuindo NA para o rbind funcionar corretamente
  
  for(i in seq(1, quantLinhas)){
    linhaEntrada <- scan()
    matrizSudoku <- rbind(matrizSudoku, linhaEntrada) 
  }
  matrizSudoku <- matrizSudoku[-1,]    #retirando a primeira linha, composta por NAs
  matrizSudoku
}

Could also be return(matrizSudoku) but the function return is not necessary in this case.

To assign the function result in the .GlobalEnv is easy. Simply assign the function result, not within the function.

sudoku <- construcaoSudoku()

Finally, this answers the question as it was posed. But I would not use a variable, quantLinhas defined outside the function and that is not passed as argument. I believe this is bad practice, to avoid if possible.

The function would look better with an argument:

construcaoSudoku <- function(n){
  matrizSudoku <- NA   #atribuindo NA para o rbind funcionar corretamente

  for(i in seq_len(n)){
    linhaEntrada <- scan()
    matrizSudoku <- rbind(matrizSudoku, linhaEntrada) 
  }
  matrizSudoku <- matrizSudoku[-1,]    #retirando a primeira linha, composta por NAs
  matrizSudoku
}

sudoku <- construcaoSudoku(quantLinhas)

An equivalent but simpler function is the version below, with a cycle replicate.

construcaoSudoku2 <- function(n){
  matrizSudoku <- t(replicate(n, scan()))
  matrizSudoku
}

sudoku2 <- construcaoSudoku2(quantLinhas)

Or even, in a single line of code,

construcaoSudoku3 <- function(n) t(replicate(n, scan()))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.