Loading Rdata files from the file directory itself . R

Asked

Viewed 5,140 times

2

How to load Rdata files that are in the same directory as the file . R, in Rstudio?

Here, for example, 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 occur when I give the folder path: load('~/Downloads/teste.RData')

Why does this occur? It is normal? How to do to load without giving all the way?

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 reply, after comments:

  1. Open some text editor like Notepad++ and save as . Rprofile the content below (don’t 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")
      }
    }
    
  2. Open some text editor like Notepad++ or even the R script editor and save as . R the content below

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

  4. In possession of the directory, save in it the two files above.

  5. Now just open and close Rstudio.

ps.: See if everything works. I haven’t tested it here, but it should work if everything is done right. See particularly if Rprofile has the "dot" in front. I’ve had problems with this.

  • There is no way to put it right in the path? I downloaded a zip from a friend of mine and put a Rdata file and an R in the same directory. In R, there was already the load("arquivo.RData"). Even in this case, I need to set the working directory?

  • I’m not sure I understand your question. You can change the file. Rprofile, so it changes the default directory on startup. Is that what you want, for R to change the default directory every time you start? If it is, I edit my answer to explain how you do it.

  • R file is loaded into Rstudio. I want to know how to load the Rdata that is in the same directory as the R file, without having to refer directly to which directory is in the line load.

  • 3

    an alternative to changing . Rprofile is to make use of Rstudio’s R Project. One of the things it does is to set the folder associated with the project as the default directory, plus other advantages.

  • Very good, @Athos

Browser other questions tagged

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