When you open or create a project in RStudio, the working directory is automatically changed to that of the project. However, if you are saving files or doing work in other subdirectories, it's often convenient to change the working directory then. It can get to be a pain to manage this in every script.
Is there a variable that always points to the project root (that is also readable by the session forked by the "Knit HTML" button) that can be used to make this easier?
The "Knit HTML" button appears to set the working directory to that of the R Markdown file. For example, if you have a variable called project.root in your ./Rprofile, and you click "Knit HTML" with this script opened,
```{r}
getwd()
source('./Rprofile')
setwd(project.root)
getwd()
```
the first and last result will be the script directory and the rest will throw errors.
To recap, as you inferred, you can set R's working directory at the command line like so:
setwd("~/NateProjects")
You can also use RStudio's Files pane to navigate to a directory and then set it as working directory from the menu: Session --> Set Working Directory --> To Files Pane Location. (You'll see even more options there). Or within the Files pane, choose More and Set As Working Directory.
But, you can do better & set up an environment like a professional programmer. To do this, you can choose to keep all the files associated with a project (a project being loosely defined as I have all my personal code under one project) organized together -- input data, R scripts, analytical results, figures. In fact, RStudio has built-in support for this. There is an excellent tutorial here that you can have a look at which explains how to walk through in step by step detail:
http://www.rstudio.com/ide/docs/using/projects
Essentially, taking from the documents, you need to do the following:
Create a new project use the Create Project command (available on the Projects menu and on the global toolbar). This can be the place that you put all future sub projects and sub-folders in
Then, once you create this new project in RStudio, the following actions happen:
You create a project file (with an .Rproj extension) within the project directory. This file contains various project options (discussed below) and can also be used as a shortcut for opening the project directly from the filesystem.
You create a hidden directory (named .Rproj.user) where project-specific temporary files (e.g. auto-saved source documents, window-state, etc.) are stored. This directory is also automatically added to .Rbuildignore, .gitignore, etc. if required.
You load the project into RStudio and display its name in the Projects toolbar (which is located on the far right side of the main toolbar)
Related
Please consider the following.
I like to structure my R projects with different subdirectories. Using bookdown or quarto seems to make this quite difficult since both like to have their source files (.Rmd or .qmd) in the project directory. I was hoping that this will get easier with quarto but this does not seem the case, although it should be possible according to this post.
Problem description (replicable example)
Start a new quarto book project in RStudio: File > New Project > New Directory > Quarto Book.
This will create several .qmd files in the R project directory. Now create new subdirectories. Starting from the R project directory: scripts/qmd.
If we put all .qmd files from the R project directory into scripts/qmd, we also need adapt their path in the _quarto.yml (e.g., from index.qmd to / scripts/qmd/index.qmd).
After doing so and pressing the Render button, we receive the ERROR: Book contents must include a home page (e.g. index.md).
Putting only the index.qmd file back into the R Project directory (and adapt the _quarto.yml), the book can be rendered.
Question
How can we put all .qmd files into a dedicated subdirectory?
Edit (ignore below)
Originally the below question was also asked but is now answered: quarto does not seem to have a "merge-knit" option like bookdown (see here). This unfortunately makes quarto useless for my use case.
If this is not possible, how can we make sure that object created in the R project directory are accessible for the .qmd files in the subdirectories? (setting execute-dir: project as suggested here did not work for me.
I'm assuming your purpose is just a specific personal preference for file organization.
You can have a large portion of your content in subdirectories (and that's my understanding of what Alison's blog post refers to), but you still need to have your index.qmd in the root. For example, you can have a subdirectory for projects, another one for posts, another one for tutorials, like Alison does.
In any case, one way of achieving what you want is by using pre- and post-render scripts.
You could move all the files from the scripts/qmd directory before rendering and then move them all back afterwards.
pre-render: mv scripts/qmd/*.qmd .
post-render: mv *.qmd scripts/qmd
Incidentally, that doesn't match my own personal preference ;-)
#Lucas A. Meyer, I tested the pre-render and post render options you suggested above, however, it throws an error after moving the file from my subdirectory to the project root:
ERROR: NotFound: The system cannot find the file specified. (os error 2)
Then, it fails to render the file and move it back to the original subdirectory location.
I then set execute-dir: project in my .yml file thinking it was trying to execute in the wrong folder, but it did not change anything.
P.S. I would have added as a comment, but my reputation is not high enough, yet. :(
UPDATE1:
I thinking I found a workaround for the time being. I've include the following project options beneath the project: header:
output-dir: subdir1/subdir2 Moves the html file, only.
post-render: xcopy *_files subdir1/subdir2 /E Copies the supporting files over.
The mv shell command can't be used for directories or you get the Directory Not Empty error.`
I am trying to populate a QTableview with some data. When developing under Qt Creator, data is read from build directory. Running the program each time, the QTableview keeps getting repopulated with previous data.
If I manually change the build directory each time before run, doesn't happen. But how do I solve the problem without manually changing the build directory every time I want a fresh run?
In your project directory there is a file name ProjectName.pro.user.
This file creating when you configure your project. it's XML file.
you can find this line on *.user files:
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/probook/Documents/Qt/testProject/Build/Windows/Debug</value>
This line define build directory address for debug. there is a same line for Release and Profile in that file.
You can also use Qt creator to define(customize) relative directory build:
goto Qt creator, Tools, Options..., Build & Run, General, Default build directory.
More info:
Qt Creator stores user-specific project settings in a .pro.user file. You can share these settings between several projects as a .pro.shared file. It has the same XML structure as a .pro.user file, but only contains the settings to share.
Based on comment, issue seems to be data which gets saved to build directory. And solution would be to remove it before running the application.
You can add Custom Process Step, either under Projects - Build Settings -
Build Steps, or under Projects - Run Settings - Deployment, and just delete the desired data files. Exact command depends on operating system.
An alternative might be to add a command line switch, something like --development-erase-saved-data, to the application itself, and erase the files (or just not read them, or whatever). Then add that command line switch to Projects - Run Settings - Run Steps - Command Line Arguments for desired build configurations.
Adding support for this in the application itself is easier to maintain, and is almost automatically cross-platform. However, it might be a feature you don't want in the application, though in that case you might only enable it for Debug builds (with #ifdef).
As a side note, saving data to executable directory is not a good idea these days. You might want to save it for example to location returned by
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
(See here for more info.)
I opened a project "ProjectOne" in atom and then added another project folder "ProjectTwo". When I cmd + t to look for a folder, it always looks through ProjectOne even though I have that folder tree folded and ProjectTwo tree opened. How do I look through ProjectTwo and switch back and forth between the two projects?
Each Atom window has currently has one project. If the two folders are added to the same project (that is, they are both added in the same Atom window), then the file fuzzy finder will search through files in both of them (because the fuzzy finder searches through the entire project, not just individual folders added to the project).
For now, if you want to search through individual root folders, you'll need to either open them in different Atom windows, or use a plugin that behaves the way you want it to.
I am using ubuntu 14.04 LTS and Atom Editor 1.5.3
If I and click on Open File I want that the dialog window shows the directory of the current file I am working on. However, it always shows the directory Recently Used and I have to click through the folders to get to my project folder. Where can I change this so that the dialog window starts in my project folder?
Also I noticed that the treeview doesnt change if I work on a specific file. So if I have two projects folders called Project 1 and Project 2 and I start with a file from folder Project 1 then I see the treeview of this folder. However, if I open a file from Project 2 and work on this file, the treeview of folder Project 1 remains. How can I make that the treeview always corresponds to the current file I am working on?
As for the first part of your question, this is a bug in atom for the linux version. Someone has suggested a patch but it seems it has not been accepted yet in the main atom version, even if it seems to work (see the discussion). The two commits are here, if you want to try something with them (what would imply something like recompiling atom yourself of course...).
About the second part of the question: I don't think it's possible to let atom switch automatically from a project to another when clicking on a file. But you can use the project manager package, what will allow you to define projects, and to set a root directory for each project and then switch from a project to another manually. The treeview will then fit with the current project.
i'm trying to package a deployment for Azure, but it is not packaging some files that are needed for the app to work properly. Those files are mostly PDFs and DOCXs.
If I go to the Package/Publish settings I have 3 options:
1. Only files needed to run this application. This is the default option which is excluding the PDFs and DOCXs.
2. All files in this project. This is including the missing files, but it is also including the code behind files (even though they are compiled). I do not want to include those files.
3. All files in this project folder. Haven't even tried this one because it will probably be worse than option 2.
My question is how do I set which extensions are actually needed to run this application?
Right after posting this question, I figured it out. For any out there with the same or similar problem here it goes...
In order to include certain files in the deployment package you have to set build action of those files to Content. Just right click on the file in solution explorer and click Properties. There change the Build Action to Content:
Also, you have to set the the setting of the Package/Publish to "Only files needed to run this application". The default option.