RStudio Cloud: Different results in Source vs. Console?

Why might getwd() return different values when executed in the Source window versus the Console in RStudio Cloud. For example high-lighting getwd() in the source returns and pressing produces "/cloud/project/CapitalMarketAssumptions". When I copy and paste getwd() into the console I get "/cloud/project". This has messed with my mind when debugging. Mainly I'd like to know how the directory is set when using the Source to reduce errors. Thanks.

Hi @rmacey,

Is it possible that some other bit of your code is resetting the working directory? If I create a new project and run getwd() from an R file in the source window and from the console, I see the same thing, even if it's in a folder: "/cloud/project".

Do you have a minimal example that demonstrates the behavior you're describing? You can make a project in Your Workspace public so everyone can see it by opening the project, clicking on the gear in the project menu, clicking on "Access" and changing the dropdown to "Everyone".


Here is how to reproduce the behavior where pressing ctrl-Enter on getwd() in the Source window produces a different result than running getwd() in the Console window. Interstingly, performing these steps in RStudio on a Windows desktop produces the same result in both windows.

Navigate to a sub-folder in a project. This might be cloud/project/data. It doesn't matter what the sub-folder is called as long as there is one. In the Files panel under the More menu item 'Set the Working Directory'. At that point the getwd() from the Console will return cloud/project/data (or whatever the user sets the working directory to). However, getwd() from the Source window still returns cloud/project.

These same steps in RStudio for Windows would cause the getwd() function to return cloud/project/data from both the Source and the Console.

Personally I think the cloud and desktop versions should function the same as much as possible. Also, I would prefer the console and source to return the same result when the same function is run because it makes debugging easier. Thank you.

Hi @rmacey,

I can see how that would make things very difficult to debug. Unfortunately, I'm still not able to reproduce what you're seeing in Cloud. Here's what I did:

  1. Log in, create a new project
  2. Once the project opens, in the Files pane, click New Folder and call it something (I called mine "test")
  3. Click on the newly created folder
  4. In the Files pane, click More, and then click "Set as Working Directory"
    I see this output in the Console:
    > setwd("/cloud/project/test")
  5. From the main header, click the New File button (the little one with the green +) and then click "R Script" in the menu that appears
  6. In the new (unsaved) file that opens in the Source pane, type getwd(), then run it
    I see this output in the Console:
    > getwd()
    [1] "/cloud/project/test"
  7. In the Console, type getwd() and hit enter
    I again see this output:
    > getwd()
    [1] "/cloud/project/test"

Am I missing a step somewhere along the way? If you follow these steps exactly, do you still see the issue? If I'm missing something and you can create a reproducible example that demonstrates this issue, it'll make it much easier to investigate.

Thanks so much,

First, Happy New Year. I am grateful for RStudio and the entire R community. I reproduced your results. I think I have steps you can follow to reproduce the error.

What I suggest you try:
Create a project. Add a folder called 'test'. Below that add a subfolder called 'data'. Navigate to 'data' and set it as the working directory from the Files pane. Check that data is the current working directory with the getwd() command from the console. Navigate up one level to the test directory. Use the File / New / R Markdown command (I chose HTML). Delete everything from line 12 on (just below blank line following ## R Markdown). Insert an R code block. Inside the block add a getwd() statement. Save the .rmd file in the test folder (being creative I named it test.rmd). Use Ctrl-Enter to execute that one line. Now I get the "test" folder when getwd() is run from the source and the "data" folder is run from the console.

For what it's worth, now that I am aware of the problem I can check that my code and console point to the same folder. The headache for me was I was trying to debug a 'path not found' type of error. The code would trigger the error. I'd run the code successfully in the console. That was my debugging headache. If this is so hard to reproduce I wonder how often people are going to encounter it. What I'm trying to say is that I understand if you want to move onto more pressing topics.

Again thank you for your help.

P.S. You can ignore the text below. I wrote it on the journey to produce the error above. I may need it if you still can't reproduce the error.

I logged out of RStudio Cloud, closed my browser and opened a new browser window. I log in. I click on a Workspace named "Red Tortoise". Then I click on my "Deployed" project. For whatever reason (I suppose the last session settings are saved), The File directory is open to Cloud/project/PortfolioConstruction/data (in the right bottom pane) Via the console getwd() produces that result. In the right bottom File pane I move up one folder by clicking on PortfolioConstruction. I open an .rmd file I happen to have there. I insert a getwd() command into the rmd and execute just that line with ctrl-enter. It returns
Executing getwd() in the console still produces

So the screenshots. Open the project -

Immediately run getwd() from the console -

Move up one level in directory structure, open an .rmd file -

Insert getwd() at end of .rmd file, highlight it and press ctrl-enter -

As shown in the last screenshot the results of getwd() don't match. For good measure, I run getwd() again from the console to confirm -

I can generate this over and over. Pretty sure it's not related to one project or anything. I don't even remember how I made it happen the first time. Also, in playing with this, I also found that when I left the project with the Files pane showing "/cloud/project/PortfolioConstruction", and then reopened it the console still produces "/cloud/project/PortfolioConstruction/data"

Happy new year to you, too! I'm glad you could reproduce what I am seeing, and with the steps you've outlined, I can reproduce what you are seeing too. Interestingly, following these steps on a local IDE works the same way as Cloud does, for me (that is, there is a difference in getwd() between Source and Console when following these steps):

  1. Create a New Project
  2. Create a test/ folder, and navigate to it
  3. Create a data/ folder inside the test/ folder, and navigate to that
  4. In the data/ folder, select "Set As Working Directory" and verify it in the console
  5. Navigate back up to the test/ folder, and create a new R Markdown as described, add the getwd() command, and save it to the test/ folder.
  6. Execute the line in the .Rmd, and:

Now I get the "test" folder when getwd() is run from the source and the "data" folder is run from the console.

Do you see this same behavior locally when following these steps? If you don't, what version of the IDE are you running?

Note: this might also be specific to Rmd -- see this comment:

and also this issue:

1 Like

I get the same results (not matching) and appreciate the references because they help explain what's going on. Thank you for your help on this.

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