Disclaimer: I am very new here.
I am trying to learn R via RStudio through a tutorial and very early have encountered an extremely frustrating issue: when I am trying to use the read.table function, the program consistently reads my files (written as "~/Desktop/R/FILENAME") as going through the path "C:/Users/Chris/Documents/Desktop/R/FILENAME". Note that the program is considering my Desktop folder to be through my documents folder, which is preventing me from reading any files. I have already set and re-set my working directory multiple times and even re-downloaded R and RStudio and I still encounter this error.
When I enter the entire file path instead of using the "~" shortcut, the program is successfully able to access the files, but I don't want to have to type out the full file path every single time I need to access a file.
Does anyone know how to fix this issue? Is there any further internal issue with how my computer is viewing the desktop in relation to my other files?
I've attached a pic.
Best,
Chris L.
The ~ will tell R to look in your default directory, which in Windows is your Documents folder, this is why you are getting this error. You can change the default directory in the RStudio settings or your R profile. It just depends on how you want to set up your project. For example:
Put all the files in the working directory (getwd() will tell you the working directory for the project). Then you can just call the files with the filename, and you will get tab completion (awesome!). You can change the working directory with setwd(), but remember to use the full path not just ~/XX. This might be the easiest for you if you want to minimise typing.
If you use a lot of scripts, or work on multiple computers or cross-platform, the above solution isn't quite as good. In this situation, you can keep all your files in a base directory, and then in your script use the file.path function to construct the paths:
base_dir <- 'C:/Desktop/R/'
read.table(file.path(base_dir, "FILENAME"))
I actually keep the base_dir assignemnt as a code snippet in RStudio, so I can easily insert it into scripts and know explicitly what is going on, as opposed to configuring it in RStudio or R profile. There is a conditional in the code snippet which detects the platform and assigns the directory correctly.
When R reports "cannot open the connection" it means either of two things:
The file does not exist at that location - you can verify whether the file is there by pasting the full path echoed back in the error message into windows file manager. Sometimes the error is as simple as an extra subdirectory. (This seems to be the problem with your current code - Windows Desktop is never nested in Documents).
If the file exists at the location, then R does not have permission to access the folder. This requires changing Windows folder permissions to grant R read and write permission to the folder.
In windows, if you launch RStudio from the folder you consider the "project workspace home", then all path references can use the dot as "relative to workspace home", e.g. "./data/inputfile.csv"
Related
Windows seems to put R libraries in a onedrive directory by default if onedrive is in use. This is undesirable especially if you're using both R and onedrive on multiple computers with the same onedrive account.
How would I set my library to be put inside of C:\users<username>\documents instead of in C:\users<username>\onedrive\documents? There are good solutions here (How do I change the default library path for R packages), but they're mostly focused on solving this for a single windows account. Is there a general way to solve it for all accounts?
Every R installation has an etc/ directory with configuration, in it you can set Rprofile.site or, easier still, Renviron.site.
Files ending in .site should not get overwritten on the next install. Make sure you don't bulk delete though.
You can query where it is via R.home("etc"). On my (Linux) system:
> R.home("etc")
[1] "/usr/lib/R/etc"
>
Really excellent solution from here (https://github.com/r-windows/docs/issues/3):
just create an Renviron.site file in the /etc folder of your Rinstallation, then copy the following line to it:
R_USER=C:/Users/${USERNAME}/Documents
This sets R_USER which in turn sets R_LIBS_USER according to the user directory of each account under windows 10.
When I setwd using Rstudio IDE GUI, then I go to create a new file... the default working directory is not the current working directory. What is a better Workflow? Any .Rprofile commands to always save in the working directory?
pic 1
pic 2
As mentioned in the comments, using a project would certainly help. Projects will always set the working directory to the project root folder on opening. Additionally, previous commands and working environment is saved (if you want it to be), so you can get right back to where you were. This is especially useful if you are working on different assignments.
Additionally, you can use getwd() to get your current working directory. Remember that the RStudio file-browser doesn't update when you set your working directory in R.
am trying to write an R script that will access an Excel file that is stored on my company's Sharepoint page so that I can make a few calculations and plot the results. I've tried various ways to do this (download.file, RCurl getURL(), gdata), but I can't seem to figure out how to do this. The url is HTTPS and there should be a username and password required. I've gotten the closest with this code:
require(RCurl)
URL<-"https://companyname.sharepoint.com/sites/folder/_layouts/15/WopiFrame.aspx?sourcedoc={2DCC2ED7-1C13-4910-AFAD-4A9ACFF1C797}&file=myfile.xlsx&action=default'
f<-getURL(URL,verbose=T,ssl.verifyhost=F,ssl.verifypeer=F,userpwd="mylogin:mypw")
This seems to connect (although the username and password don't seem to matter) and returns
> f
[1] "<html><head><title>Object moved</title></head><body>\r\n<h2>Object moved to here.</h2>\r\n</body></html>\r\n"`
However, I'm not sure what to do at this point, or even if I'm on the right track. Any help will be greatly appreciated.
I use
library(readxl)
read_excel('//companySharepointSite/project/.../ExcelFilename.xlsx', 'Sheet1', skip=1)
Note, no https:, and sometimes I have to open the file first (i.e., cut and paste //companySharepointSite/project/.../ExcelFilename.xlsx into my browser's address bar)
I found that other answers did not work for me, perhaps because I am on a Mac, which obviously does not play as well with Microsoft products such as Sharepoint.
Ended up having to split it into two pieces: first download the Excel file to disk and then separately read that Excel file.
library(httr)
library(readxl)
# the URL of your sharepoint file
file_url <- "https://yoursharepointsite/Documents/yourfile.xlsx"
# save the excel file to disk
GET(file_url,
authenticate(active_directory_username, active_directory_password, "ntlm"),
write_disk("tempfile.xlsx", overwrite = TRUE))
# save to dataframe
df <- read_excel("tempfile.xlsx")
df
# remove excel file from disk
file.remove("tempfile.xlsx")
This gets the job done, though would be interested if anyone knows how to avoid the interim step of writing to disk.
N.B. Depending on your specific machine/network/Sharepoint configuration, you may also be able to just use authenticate(":",":","ntlm") per this answer.
I was unable to accomplish this using hints from answers above in R (I tried many approaches found on this site). However, just to highlight the response by #RyanBradley above and especially the response by #ZS27:
I instead had to use the OneDrive Desktop client (Windows) to allow me to sync the folder to my computer. Newer versions of SharePoint (like that found in MS Teams) have a sync button or feature in the document libraries / folders that interfaces with OneDrive.
This is the functional equivalent of mounting the folder as a network drive, so R interfaces with the file as if it was a part of your file system. Works for me.
You may need to map a network drive to the SharePoint library so that you can connect to it directly. Or if you don't want to map a network drive you could also place a shortcut to the folder in your startup folder.
Example file path:
\company_sharepoint_site\ssp\site_name\sub_site_name\library_name
Example start up folder location (Windows 10):
C:\Users\USER_NAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Note direction of the slashes ("\" rather than "/") is important so that your file path is interpreted as a file location, not an internet browser location. By placing such a path in a network drive or as a shortcut in your startup folder your PC should connect to it when it boots.
# Load or install readxl
if(require(readxl) == FALSE){
install.packages("readxl")
if(require(readxl)== FALSE){stop("Unable to install and load readxl")}
}
# Define path to data
data_path <- "\\\\company_sharepoint_site\\ssp\\site_name\\sub_site_name\\library_name\\Example.xlsx"
# Pull data
df_employees <- read_xlsx(data_path)
I had a situation exactly like you. I want to access an excel file, available on an sharepoint site using R programming language.
I have also surfed many stuff in Internet and I didn't find anything relevant to my requirement.
Then, I have attempted the following thing:
I have made the sharepoint folder as a network drive folder, in my local system.
Then, I have accessed that excel file(in sharepoint site) from my machine without accessing web browser.
Hence, I have copied the network path, present in my system (it will be same as your sharepoint site, however it will not have https/http.
The site will start with "\" like the following: "\sharepoint.test.com\folder\path").
Launch RStudio and select Import Dataset option, under Environment section.
Choose 'From Excel'. 'Import Excel Data' form will be opened.
Under File/URL field: Paste the network path of sharepoint (copied from your machine).
Click Import, the excel file in Sharepoint will be imported in R successfully.
Ensure that the file should not have html language as input (lie %20 and all) and Backslash should be used as separator in the URL.
While importing the file, provide the input of the folder name exactly, as you see.
For example:
Sharepoint.microsoft.com - Sharepoint's Domain
Department name - name of the Folder
Project name - name of the folder
Sample.xlsx - name of the file
So, your URL to import dataset should be:
"\Sharepoint.microsoft.com\Department name\Project name\Sample.xlsx".
Thank you!
Try using the link in this format:
http://site/_layouts/download.aspx?SourceUrl=url-of-document-in-library
If above doesn't work try this syntax [note slash directions]:
"\\gov.sharepoint.com#SSL/DavWWWRoot/sites/SomePath/SomePath/SomePath/SomeFile"
See this for more info about syntax and what's going on:
Connect to a site via SSL/DavWWWRoot not usual URL? Why does this make a difference?
I use R under Windows on several machines.
I know you can set the working directory from within an R script, like this
setwd("C:/Documents and Settings/username/My Documents/x/y/z")
... but then this breaks the portability of the script. It's also annoying to have to reverse all the slashes (since Windows gives you backslashes)
Is there a way to start R in a particular working directory so that you don't need to do this at the script level?
You should copy shortcut to R (R.lnk file) to desire folder. Then in "Properties" (right mouse button -> last option) delete anything in field "Start in..." in second tab ("Shortcut"?). If you start R with this shortcut working directory will be that one where the shortcut is.
I don't have english version of Windows so I'm not sure about field names, but they should be easy to find.
Similar questions were in R-windows-faq:
2.5 How do I run it?
2.10 How can I keep workspaces for different projects in different directories?
2.14 What are HOME and working directories?
In 2.14 is mentioned that
The working directory is the directory from which Rgui or Rterm was launched, unless a shortcut was used when it is given by the `Start in' field of the shortcut's properties.
You could use an environmental variable. This can work with Sys.getenv() and Sys.setenv(). For instance:
> Sys.setenv(R_TEST="testit")
> Sys.getenv("R_TEST")
R_TEST
"testit"
If you sent the variable in your script, you should be able to access it from within, and then call setwd() on that output.
Save your workspace to the desired directory and thereafter you just open the workspace from Windows explorer.
I put the following line in front of my scripts and it allows me to work across my computers.
setwd(path.expand("~/path/to/working/directory/") )
where ~ is = to your home directory.
Sys.setenv(HOME = "path") or Sys.setenv(R_USER = "path") can both set the home directory.
In my case, I work on several windows boxes, each have fairly different directory structures, but by setting the home directory properly I can sync code between computers and have them run properly on each one since where I run my R projects have similar directory structures.
If you're using Emacs/ESS, this isn't a problem. I navigate to the directory where my R script is located, open it, then start an R ESS process. An R console pops up with the current directory as R's working directory.
If you haven't converted to Emacs/ESS, I recommend it. (Though to prevent a flame war, I also note there are similar options for Vi users.)
Hope that helps.
Just a detail: instead of reversing the slashes as you say, just add another backslash. Two of these \\ works the same way as one of these /. That makes it at least a little easier.
For Ubuntu:
Insert the following command into your .Rprofile file (usually in your home directory):
setwd(Sys.getenv("PWD"))
Now your default working directory will be whatever directory you launched R from. Keep in mind you can also set up default workspaces in different directories by saving your workspace image as .RData wherever you plan to launch R (startup sources .Rprofile before searching for .Rdata in the cwd).
To set the R work directory like the current directory of the R script that I'm working, I always use a combination of the commands getwd() and setwd(), like this:
path <- getwd()
setwd(path)
or
setwd(getwd())
If you want learn more about it, see this article.
Cheers,
[]'s
To set working directory in R Studio:
Refer detailed slide deck with screen-shots here.
Using setwd(): windows users would need to replace backward slashes '' with forward slashes '/' or double backward slashes '\'
You can do the former using find & replace (Short-cut: Ctrl+F)
Another option: Go to Session --> set working directory --> choose working directory & browse the folder which you want to set as the working directory, click on open
Quickest method (my favorite) use the shortcut 'Ctr+Shift+H' (on windows system), browse the folder which you want to set as the working directory, click on open
To set a permanent working directory (when not in a project) in R Studio:
Refer my quick video on the same: https://youtu.be/hMjzO4bAi70
Go to Tools --> Global Options --> R General [Basic] --> Default Working Directory (when not in a project)
browse the folder which you want to set as the working directory, click on 'Apply' and 'OK'
However, the efficient & better way to organize your work is to create projects & use version control.
Put a shortcut for the R gui into your desired directory. Right-click and look at the shortcut properties. Delete the entry for "Start In" and click OK. When you launch the R gui from this shortcut the default directory will be the folder from which you have launched. Copy/paste this shortcut wherever you desire.
I asked this on Super User, but someone suggested that I take it here because there are many more R experts.
The question:
I have to keep navigating to my directory when I go to File > Change dir..., which is particularly annoying.
Does anyone know how to make R remember the previously used directory?
I keep all the code associated with a particular project in a file (or more often a series of files). The first line is usually
setwd(...)
which sets the directory.
Once a workspace has been saved in the desired directory, just start R by opening that workspace (rather than from the desktop or start menu). Then the directory is already set to where you want it.
I may not be answering your question, because it's a bit vague, but some thoughts:
You can store the location of 'my directory' in R's .GlobalEnv so that it starts there when you start R.
This article discuses how to have different working directories with corresponding different ".RData" files.
You could write a custom function that remembers the current directory before you set the new directory
cd <- function(x = "") {
logical (length = 0)
if (!is.logical(x)) {
cwd <- getwd()
Sys.setenv("R_OLDWD"=cwd)
setwd(x)
} else {
setwd(print(paste(Sys.getenv("R_OLDWD"))))
}
}
From the R for Windows FAQ:
The working directory is the directory from which Rgui or Rterm was launched, unless a shortcut was used when it is given by the `Start in' field of the shortcut's properties. You can find this from R code by the call getwd().
The home directory is set as follows: If environment variable R_USER is set, its value is used. Otherwise if environment variable HOME is set, its value is used. After those two user-controllable settings, R tries to find system-defined home directories. It first tries to use the Windows "personal" directory (typically C:\Documents and Settings\username\My Documents on Windows XP and C:\Users\username\Documents on Vista). If that fails, if both environment variables HOMEDRIVE and HOMEPATH are set (and they normally are), the value is ${HOMEDRIVE}${HOMEPATH}. If all of these fail, the current working directory is used.
You can find this from R code by Sys.getenv("R_USER").
This depends on the system that your using. There are a few tricks to use but if your looking to run R from a system menu and have it remember the directory the quick answer is no that won't happen. Linux is pretty easy just navigate to the directory in the terminal first and that will solve the problem. I have no idea about macs, But I can talk about windows extensively. First if you navigate to the directory and save your workspace once then you can use the saved .RData file to double click and restore your workspace including current directory. My personal, and biased opinion is to use an editor like Notepad++ with NppToR that way when you spawn a Rgui window you inherit the active directory from the current script. It also provides a menu command to adjust the working directory to the current script's directory.
Another point is that you can always set the working directory with the setwd("path/to/dir/") command inside any R session on any platform.
I use StatET and Eclipse as my R user interface. It automatically sets the working directory to the location of my project folder. workspace = ${project_loc}.
It also automatically loads any saved workspace, when starting R from a particular project.
On Windows, I put a file Rgui.bat from code.google.com/p/batchfiles in my project directory and use this to start R.