Finding absolute path for file on network drive - r

I am having some trouble knitting a markdown file which is sitting on a network drive. I am using rmarkdown::render() to knit the file. I have tracked the problem down to the file_path_as_absolute() function.
I have a markdown file, foo.Rmd, in my working directory.
> list.files()
[1] "$RECYCLE.BIN" "2014-11-04-popular-wagers-distribution.pdf"
[3] "db-connection.R" "desktop.ini"
[5] "figure" "foo.md"
[7] "foo.Rmd" "game-popularity.R"
[9] "My Data Sources" "player-data-game-monitoring.tsv"
[11] "player-linking.R" "player-pca.R"
[13] "query-1.sql" "query-2.sql"
[15] "R" "report-test-2.Rmd"
[17] "report-test.md" "report-test.Rmd"
[19] "report-test.xxx" "SQL Server Management Studio"
[21] "test.md" "test.R"
[23] "test.Rmd" "Thumbs.db"
I can find the absolute path:
> normalizePath("foo.Rmd")
[1] "\\\\dernetapp02\\h$\\users\\AndrewCo\\myCitrixFiles\\Documents\\foo.Rmd"
Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="foo.Rmd": Access is denied
This works although it generates a warning. I am not sure why I get this warning because I certainly do have both read and write access to the file.
But calling file_path_as_absolute(), which is just a wrapper for normalizePath(), turns that warning into an error.
> tools::file_path_as_absolute(input)
Error in normalizePath(path.expand(path), winslash, mustWork) :
path[1]="foo.Rmd": Access is denied
I am pretty sure that the problem relates to the fact that the file is on a network drive because I do not encounter this issue when I do the same thing with a file on my local drive. However, as mentioned above, I do have read/write access to this file.
Does anybody have any ideas for the source of this problem? And, more importantly, a possible solution?
Based on a suggestion (below) I tried mapping to a network drive:
> system("net use s: \\\\dernetapp02\\h$")
This seemed promising but ended up with the same results:
> normalizePath("s:\\users\\AndrewCo\\myCitrixFiles\\Documents\\foo.Rmd")
[1] "s:\\users\\AndrewCo\\myCitrixFiles\\Documents\\foo.Rmd"
Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="s:\users\AndrewCo\myCitrixFiles\Documents\foo.Rmd": Access is denied
Thanks!
Best regards,
Andrew.

Same problem here.
I ended moving the project in a local folder and copying the produced file with the command file.copy.
It is a generic solution and if someone find one better, I am interested in.

Realise this is an old question, but I just had the same issue. For me, mapping the network drive via windows explorer worked.
It is important to close the current R Session and re-open the Rproj file from the mapped drive location.
It all worked normally from there.

Related

R error-normalizePath(path.expand(path), winslash, mustWork)

I was provided a new work computer and I was granted admin rights for the weekend. I installed R version 4.2 but get this error when it starts up:
normalizePath(path.expand(path), winslash, mustWork) :
path[1]="P:": The network path was not found
Because it is trying to connect to my shared Drive "P" first, it is a slow process each time I start R or R Studio.
When I am not connected to the network it is faster but only after it searches for the P drive and goes through the error.
This is a slow process. If I could have it normalizePath(path.expand(path)) the "C" drive instead of "P", it would make things much faster.
Is there a way to have it go directly to the C Drive? I have tried to change the .Rprofile but it doesn't help because it still searches the shared P Drive on the R startup.
Any help on preventing it from going to the shared drive and getting stuck would be appreciated.
Thank you.

How to import an excel file into r-studio [duplicate]

I am trying to use readxl::read_excel to import tabular data from an excel spreadsheet (.xlsx and .xls) into R. I have had success with this function when reading spreadsheets from Desktop, but I am having trouble reading from my Documents folder now.
I tried reading both .xlsx and .xls formats of the same file and got 2 different error messages.
# Check the working directory
[1] "\\\\isdowfsv04/D100Home/e525902"
# Read .xlsx
specs <- read_excel("Class Specs Sept 2016.xlsx")
Error: 'exdir' does not exist
# Read .xls
specs <- read_excel("Class Specs Sept 2016.xls")
Error: path[1]="\\isdowfsv04/D100Home/e525902/Class Specs Sept 2016.xls": Access is denied
The second message made me think I don't have permission to read/write data here, but I create folders/files here all the time. I have even used another R library, readr package, to import .csv files from this same directory.
# Using readr to import .csv version of the file works fine.
specs <- read_csv("\\\\isdowfsv04/D100Home/e525902/Class Specs Sept 2016.csv")
Based on other posts I've seen, it seems like a permission issue, but it's unclear to me how to fix it.
I tried using Sys.setenv() to change the temporary folders location to somewhere I know I have permission to write (i.e., Desktop) because I thought readxl used these paths when reading the data. But that didn't work because I got the exact same error messages.
Sys.getenv(c("TEMP", "TMP"))
TEMP TMP
"C:\\Users\\e525902\\AppData\\Local\\Temp" "C:\\Users\\e525902\\AppData\\Local\\Temp"
# Change temp file paths to the Desktop
Sys.setenv(TEMP = "C:\\Users\\e525902\\Desktop")
Sys.setenv(TMP = "C:\\Users\\e525902\\Desktop")
I am using a PC at work and the Documents folder is actually a network drive. I have used read_excel to import .xlsx files from other network folders, but this one is giving me trouble. Below is more information on the system I'm using.
Sys.info()
sysname release version
"Windows" "7 x64" "build 7601, Service Pack 1"
nodename machine login
"10004053WDT" "x86-64" "e525902"
user effective_user
"e525902" "e525902"
search()
[1] ".GlobalEnv" "package:readr" "package:readxl" "tools:rstudio" "package:stats"
[6] "package:graphics" "package:grDevices" "package:utils" "package:datasets" "package:methods"
[11] "Autoloads" "package:base"
# R version
version$version.string
version.string R version 3.2.5 (2016-04-14)
I ran into the same issue. It was occurring to me when I set my working directory to the base location of the excel files, and then in the read excel, using a relative path to get to the directory.
Once I rebooted R, and instead kept my working directory as the default, and then used the full file path in the read_excel function, my exdir error went away.
Let me know if this works for you. I was also on a network drive at work with a windows 7 64-bit system.
The current working directory is displayed by the RStudio IDE within the title region of the Console pane. You can also check your current working directory by running the command getwd() in the console.

RStudio: unexpected call to `dir.create()` with the first instruction within a project stored on a network drive

First, apologies for a lack of a reproducible example, but I cannot really provide one as I believe the problem lies within my network settings. Please treat this question as a call for help in debugging the issue...
After opening in RStudio a project stored on a network drive and running the very first instruction (being it a package load or even a <- 1) I am seeing a really weird output in the console:
> a <- 1
Warning message:
In dir.create(tempPath, recursive = TRUE) :
cannot create dir 'F:\Marketing', reason 'Permission denied'
I have all possible temp dirs set up in user environment variables (TEMP, TMP, TMPDIR) and Sys.getenv() is printing them correctly.
"F:\Marketing" is a valid path on my network drive and it is a root directory of the project.
I have tried to debugonce(dir.create) in .RProfile to see what the tempPath is, but unfortunately this resulted in an "invalid 'envir' argument" error.
After copying the project to a local drive the problem disappears, so this is clearly a network drive/network setup problem, but I do not know where to dig more and my IT dept. is not really useful here...
Any ideas how to debug this warning?

Error when using readxl: 'exdir' does not exist

I am trying to use readxl::read_excel to import tabular data from an excel spreadsheet (.xlsx and .xls) into R. I have had success with this function when reading spreadsheets from Desktop, but I am having trouble reading from my Documents folder now.
I tried reading both .xlsx and .xls formats of the same file and got 2 different error messages.
# Check the working directory
[1] "\\\\isdowfsv04/D100Home/e525902"
# Read .xlsx
specs <- read_excel("Class Specs Sept 2016.xlsx")
Error: 'exdir' does not exist
# Read .xls
specs <- read_excel("Class Specs Sept 2016.xls")
Error: path[1]="\\isdowfsv04/D100Home/e525902/Class Specs Sept 2016.xls": Access is denied
The second message made me think I don't have permission to read/write data here, but I create folders/files here all the time. I have even used another R library, readr package, to import .csv files from this same directory.
# Using readr to import .csv version of the file works fine.
specs <- read_csv("\\\\isdowfsv04/D100Home/e525902/Class Specs Sept 2016.csv")
Based on other posts I've seen, it seems like a permission issue, but it's unclear to me how to fix it.
I tried using Sys.setenv() to change the temporary folders location to somewhere I know I have permission to write (i.e., Desktop) because I thought readxl used these paths when reading the data. But that didn't work because I got the exact same error messages.
Sys.getenv(c("TEMP", "TMP"))
TEMP TMP
"C:\\Users\\e525902\\AppData\\Local\\Temp" "C:\\Users\\e525902\\AppData\\Local\\Temp"
# Change temp file paths to the Desktop
Sys.setenv(TEMP = "C:\\Users\\e525902\\Desktop")
Sys.setenv(TMP = "C:\\Users\\e525902\\Desktop")
I am using a PC at work and the Documents folder is actually a network drive. I have used read_excel to import .xlsx files from other network folders, but this one is giving me trouble. Below is more information on the system I'm using.
Sys.info()
sysname release version
"Windows" "7 x64" "build 7601, Service Pack 1"
nodename machine login
"10004053WDT" "x86-64" "e525902"
user effective_user
"e525902" "e525902"
search()
[1] ".GlobalEnv" "package:readr" "package:readxl" "tools:rstudio" "package:stats"
[6] "package:graphics" "package:grDevices" "package:utils" "package:datasets" "package:methods"
[11] "Autoloads" "package:base"
# R version
version$version.string
version.string R version 3.2.5 (2016-04-14)
I ran into the same issue. It was occurring to me when I set my working directory to the base location of the excel files, and then in the read excel, using a relative path to get to the directory.
Once I rebooted R, and instead kept my working directory as the default, and then used the full file path in the read_excel function, my exdir error went away.
Let me know if this works for you. I was also on a network drive at work with a windows 7 64-bit system.
The current working directory is displayed by the RStudio IDE within the title region of the Console pane. You can also check your current working directory by running the command getwd() in the console.

r - How to specify the path in normalizePath, or get around this error associated with it?

I'm learning R and just have it installed on my office computer. I don't have the administrator right on the computer (as I even have to call IT for installation).
Then I install a package. At first it doesn't work when typing, for example:
install.packages("thepackage")
The error message is this:
Error in normalizePath(path.expand(path), winslash, mustWork) :
path[1]="\\company\5050\Users\myusername\Documents\R\win-library\3.3": Access is denied
In addition: Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="\\company/5050/Users/myusername/Documents/R/win-library/3.3": Access is denied
I do some homework and find that a potential solution is to "Map a network drive to your network folder". I'm not sure what it means, but I try this:
install.packages("thepackage",lib="H:/Documents/R/win-library/3.3")
because it looks like I have more "control" of H drive (it has my username on it). And it works:
package ‘thepackage’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\myusername\AppData\Local\Temp\Rtmp4MNURu\downloaded_packages
I then fail to load the package,
library(thepackage)
saying:
Error in normalizePath(path.expand(path), winslash, mustWork) :
path[1]="\\company/5050/Users/myusername/Documents/R/win-library/3.3": Access is denied
But again this works:
library(thepackage,lib="H:/Documents/R/win-library/3.3")
So how can I set the normalizePath to the one that works to avoid additional and rather unnecessary specification of library directory?
You can put in your home directory's .Rprofile file (just create it if it's not there yet) the following line:
.libPaths("H:/Documents/R/win-library/3.3")
That way this location will be used by default. The .Rprofile is run every time you're opening any new R session. You can copy the existing content of the folder from which you don't have write access to this folder to include all pre-installed packages.
I think you are looking for:
system("net use D: \\\\company\\path\\")
to map to the virtual D drive. I would then use file.path when accessing the stuff on D:. It looks that you may benefit from changing R defalut library path in Rprofile.site, by adding the line:
.libPaths("Path to your libs")
When you type .libPaths() can you read and write to that directory with no problems?
I was with the same problem them I found this solution by the user Mike M.
In Windows 10/11 you can also set the R Home Directory just for R
without changing your system HOME with a special environment variable
R_USER. Adding this to your Environment Variables with the path you
want for your R Home will set the R Home path without changing your
system HOME. RStudio looks for R_USER first (and then moves on to
HOME).
It worked for me.

Resources