here::here Pulls from Incorrect Directory and Ignores .here File in R - r

I am having trouble loading my .csv files with here::here.
The here package is setting its default directory as:
'Z:/PSE/Analysis/One'
My working directory is:
Z:/PSE and I have put a .here file in that directory. Here is the message I get after using dr_here():
Here() starts at Z:/PSE/Analysis/One, because it contains a file matching [.]Rproj$ with contents matching ^Version: in the first line
I thought the here function started at the current working directory, which is Z:/PSE, in which it should recognize the .here file and follow the path that I specify in the remainder of the here function?

Related

The system cannot find the file specified in Rmarkdown

I am doing my current project in RStudio Desktop. I am doing RMarkdown to later transfer. I am having some trouble getting an error of the system cannot find the file specified RMarkdown. At first, it says that the combined_databike was not found, but I literally did it in the same file already also you can see in the upper right all of the data frames which mention "combined_databike." This being said when I am trying to hit knit it gives me the error. Now the error says is at the tripdata_202006, which I cannot understand because I imported every file from tripdata_202006 to tripdata_202105 using the "import dataset."
I want to understand why is not working and how I bring a solution.
That's because the file you want to read is not in the folder where your project or in this case your rmarkdown file is.
As you can see in the file list of your console, in your directory you have 4 files, and you don't have the folder "Bike data" where the file 202006-divvy-tripdata.csv is located, according to the path that is in line 83 of your code.
Maybe to solve that, I think you have two options, the first one is to write the whole path to the folder location and then to the file. And the second option is to move the folder "Bike data" with the files you have in it, where your rmarkdown file is located.

Find the right path of child document in r markdown

I got one code from a colleague, and I want to modify it for my purpose. It's a bookdown project. In the index.Rmd file, there is a code
r child = here::here("prelims", "00--prelim.Rmd")
This means, in the working directory, there is a folder called "prelims" and the child document "00-prelims.Rmd" exists in that "prelims" folder. I can knit the index.Rmd document with this code, when this index.Rmd is part of a folder. But if I have created an RStudio project, then the location of "00-prelims.Rmd" changes.
If projectname.Rproj file exists, then the location of the "00-prelims.Rmd becomes here::here("projectname","prelims","00-prelims.Rmd").
So, I have tried the following
r child = ifelse(file.exists(Sys.glob("*.Rproj")),here::here(sub("\\..*", "", list.files(pattern = "\\.Rproj$")),"prelims", "00-prelims.Rmd"),here::here("prelims", "00-prelims.Rmd"))
But it gives me logicl(0) error. How can I get the right path of the "00-prelims" file every time?

Read a file in R without changing the working directory

How can others who run my R program read a file(eg: csv) used in my R code without having to change the working directory in setwd()?
I will suggest you use the here() function in the here package in your code like this:
library(here)
Data1 <- read_csv(here("test_data.csv"))
read.csv has a file argument and if I were to quote from the inbuilt R help about file:
If it does not contain an absolute path, the file name is relative to
the current working directory, getwd().
So, providing the absolute path of the file inside the file argument solves this problem.
In Windows
Suppose your file name is test.csv and it's located in D:\files\test_folder (you can get the location of any file from its properties in Windows)
For reading this file you run:
df<-read.csv('D:\\files\\test_folder\\test.csv')
or
df<-read.csv('D:/files/test_folder/test.csv')
Suggested reading: Why \\ instead of \ and Paths in programming languages
Haven't used R in Linux but maybe Getting a file path in Linux might help
Read from web
Just type in the web address of the dataset in the file attribute. Try:
df<-read.csv('https://raw.githubusercontent.com/AdiPersonalWorks/Random/master/student_scores%20-%20student_scores.csv')
Note: This link contains a list of 25 students with their study hours and marks. I myself used this dataset for one of my earlier tasks and its perfectly safe

How to import an external dataset into in a Moodle question?

I would like to import an external dataset using read.table() (or any other function for reading files) and then randomize or sample over it. The file is stored in a subfolder within the parent folder that contains the exercises *.rmd. I am working within a RStudio project. I tried placing the dataset in different levels of the folder structure. Using relative path did not work, but absolute paths did.
My folder structure is:
$home/project_name/exercises # It contains the RMD files
$home/project_name/exercises/data # It contains data files that I want to process
$home/project_name/datasets # this folder could eventually contain the dataset I want to process
To make this code more portable, I would like to know o the manage relative paths within *.Rmd for the knitting process.
The exercises are copied to a temporary directory and processed there. Hence, the easiest option is to copy these files to the temporary directory using include_supplement("file.csv"). By default this assumes that the file.csv is in the same directory that the exercise itself resides in. If it is in a subdirectory you can use include_supplement("file.csv", recursive = TRUE) and then subdirectories are searched recursively for file.csv.
After using include_supplement(), the copied file is available locally and can be processed with read.table() or also included in the exercise as a supplementary file. See http://www.R-exams.org/templates/Rlogo/ for a worked example. However, note that the Rlogo template explicitly specifies the directory from which the file should be copied. This is not necessary if that directory is the same as the exercise or a subdirectory.

How can I set different directory to the current directory in R?

I'm trying to open a file that does not exist in my current directory. This file is named testFile.r existing in the data folder in my current directory. I tried file.path("./data") and then wanted to show the file with this command file.show("testFile.r") but it gives this error:
Error: File testFile.r does not exist.
And the command getwd() gives the previous current directory. So any thoughts on this?
You change your current directory using the command setwd(new_directory)
(replacing new_directory with the path to the directory you want).
If you'd rather stay in your current directory, just do
file.show("./data/testFile.r")
To keep track of multiple paths, you can save each as a variable and use the paste function to reference the file:
path1 <- "./data/"
path2 <- "./second_folder_name/"
file.show(paste0(path1, "testFile.R"))
file.show(paste0(path2, "testFile.R"))

Resources