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.
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'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.
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...
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")