"source" can't read the other file

Hi people.

I have an application who needs to read other file to connect to database.

The main file calls "dados.R" and i have to carry the code coming from other file called "db.R".
I'm using the following code:

source("db.R")

But appears the following error:

Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'db.R': No such file or directory

The files are in the same directory and I checked the name correctly.

source%20error

diretorio

Have you checked your working directory via getwd() ? If this is different than the directory where db.R is then you need to change the path in the source function.

I don't know how to use the getwd(), but in the image that I posted here, show all the files in the same path.

How can I test this function?

Just type (or paste) it into the console and it will tell you the working directory.

I did this. It's all in the same directory.
The same path was returned.

Ok, that's eliminated a possible easy solution.

I'm afraid I cannot think of any other obvious avenues based on the error message. Hopefully somebody else can help you.

1 Like

Anyway, thanks for the help. :wink:

I'm not sure that I have a better suggestion than @martin.R, but one additional way of testing is to type list.files() in the console and check that both dados.R and db.R show up. If not, you're definitely having issues with the paths.

1 Like

It seens ok.

Do I have to put the "source" code in an especific file? Like in UI.R or Server.R... or can I put it in any file?

You can put your source code in any file. ui.R and server.R has special meaning for shiny apps (they are the ones being executed by runApp()), but they can in turn source code from any file. I guess you somewhere in server.R has a source("dados.R") statement?

With the risk of beating a dead horse, are you absolutely sure that dados.R can see db.R? I know they are in the same folder, but are you sure that when dados.R is executed that getwd() returns "C:/Users/Maia/Desktop/Relatorios R QT/Escoamento_Capabilidade" ? One way to test this is to change your source statement in dados.R to include the full path, i.e.

source("C:/Users/Maia/Desktop/Relatorios R QT/Escoamento_Capabilidade/db.R")

(please check the spelling in the statement above, I have already edited it twice realizing typos :-))

If this works, your working directory may not be what you think it should be. If it doesn't work I think I am out of ideas...

I'll try it now.

If it does not work, i'll create a single file unifying all files in only one.

Thanks! I'll return later.

It works!
Really thanks man!

I just write the complete path like you said.

Regards.

Glad you got it working with an absolute path! That’s a good start. But I’m afraid you probably still have some work to do if you want to run this app anywhere other than your own computer, since absolute paths won’t work if you move the files anywhere else.

Some tips for making sure your app code is portable:

  • Set the app up as an RStudio Project (you can do this even with an existing folder). This will make sure that any time you open up the Project to work on it, your working directory is always the Project folder.
  • Never use setwd() in your code
  • If you have done both of the above steps, then if db.R and dados.R are in your Project folder, the source() statement in dados.R should just say: source("db.R")
4 Likes

I recently discovered the here package. Very useful! More info: https://github.com/jennybc/here_here

2 Likes

Thanks for the tips.

I created a RStudio Project and the files were in the same folder. I coudn't understand why the funciona "source" was not working.

I transfer the code to a virtual server and I got exactly that problem you told and I'm working to solve this problem now =(.

But, in my own computer, the soluction "source ("path")" saved for a moment.

I'm thinking what might have caused this error.

Seems interesting. :thinking:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.