Loading RData files from the directory itself .R

2

How to load RData files that are in the same directory as the .R file in RStudio?

Here, for example, there is the line: load('teste.RData') When loaded, it returns ...

Erro em readChar(con, 5L, useBytes = TRUE) : 
  não é possível abrir a conexão
Além disso: Mensagens de aviso perdidas:
In readChar(con, 5L, useBytes = TRUE) :
  não foi possível abrir o arquivo comprimido 'teste.RData', motivo provável 'Arquivo ou diretório não encontrado'

This does not happen when I give the folder path: load('~/Downloads/teste.RData')

Why does this occur? It's normal? How do I load without giving it all the way?

    
asked by anonymous 23.09.2014 / 19:12

1 answer

1

R looks for the file in its default directory. To change the default directory, see

?setwd.

In your case, try:

setwd('/Downloads/')

or

setwd('~/Downloads/')

Update my response after comments:

  • Open some text editor such as notepad ++ and save as .Rprofile the contents below (do not forget the "." in the file name).

    .First <- function(){
      cat("\nWelcome to R!\n",sep="")
      cat("---------------\n\n",sep="")
      if(file.exists("~/novodiretorio.r")){
        source("~/novodiretorio.r")
        cat("novodiretorio.r foi carregado, mudando o diretório padrão do R")
      }
    }
    
  • Open some text editor such as notepad ++ or even the R script editor and save as .R the content below

    setwd("caminho") ## coloque o caminho onde está seu arquivo
    
  • Open Rstudio and type getwd () into the console to get the default R directory

  • With the directory, save the two files above.

  • Now just open and close Rstudio.

  • ps: See if everything works. I have not tested here, but it should work if everything is done right. See particularly if Rprofile has the "dot" in front. I've had trouble with that.

        
    23.09.2014 / 21:41