Missing index.html and folders structure? - r

After using bookdown::render_book("index.Rmd", "bookdown::gitbook") in RStudio/Windows the index file is not being generated. As the first chapter is #preface (in "index.rmd"), the file being generated is preface.html. The consequence is that it is not working in GitHub. Am I missing something?
How should be the settings in yaml files in order to keep the .rmd (source files of the book) in a subfolder of my R projects and generate the book in another folder? I am trying to have the bookdowm integrated in my research folder structure.
Thanks for this amazing package!!!

For the first question, my problem is that I had written two chapters in the index file. After removing the "# About the author" part, the index.html file came back.

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.

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 to render documents in sub-directories with rmarkdown::render_site()?

For example I have a markdown file called index.md in the root folder and another markdown file in a subfolder called docs/tools/git.md. When I run render_site(), index.html is generated from index.md, but no document is generated from docs/tools/git.md.
The help page of rmardown::render_site says:
R Markdown (.Rmd) and plain markdown (.md) files in the root directory are rendered.
I have tried to add include: ["docs/tools/git.md"] to the _site.yml but this doesn't help.
This question is related, but I already know how to work with sub directories in knitr.
What I want to know is how to render documents in sub-directories with rmarkdown::render_site()?
It is not possible to have sub-directories with rmardown::render_site() according to the author Yihui Xie who referred to the Rmarkdown website page of the bookdown documentation in a comment above:
"Before blogdown was invented, there was actually a relatively simple way to render websites using rmarkdown. The structure of the website has to be a flat directory of Rmd files (no subdirectories for Rmd files) and a configuration file in which you can specify a navigation bar for all your pages and output format options."
It goes on to specify that these simple sites are still useful in some cases. The first two points:
"You are familiar with generating single-page HTML output from R Markdown, and all you want is to extend this to generating multiple pages from multiple Rmd files."
"It suffices to use a flat directory of Rmd files. You do not write a blog or need RSS feeds."

Reading data files in blogdown

I am a new user of blogdown using Hugo. I would like to create a new post that includes R code to read a data file.
The data file is in my static folder, local path C:\mydir\myblogdown\static\data\savedrecs.txt. Since I was successful in referring to an image using a relative path like this, ![](/images\myimage.jpg), I tried reading the data using something similar for the data file, read.csv("/data\savedrecs.txt"), but that didn't work.
I started playing around with the list.files() function to see if I could find a relative path that worked in my local version of the post, list.files("../../static/data") worked, showing me ## [1] "savedrecs.txt".
I tried searching around other folks' blogdown repos on Github to see how they might have referred to a data file, but the only example I found referred to a data file using a URL.
I suspect that this may be due to the location of your data file. In my working example, the Rmd form of my blog post is in the directory, called p0bs/content/post. I also add my data file (a CSV in my case) to this directory.
I then treat the rest of the post as I would in a standard Rmarkdown website, with Rmd chunks (that are named without spaces). In your case, that code would include:
read.csv("savedrecs.txt")
I hope that helps you.

How can I avoid hardcoding a file path?

I am using RStudio to knit an .Rnw file to .pdf. This .Rnw file is stored in directory that is under git version control. This directory also contains a .RProj file for the project.
I collaborate with colleagues who don't know the first thing about .Rnw files and git. These colleagues want to open a Word file and track change their hearts out. So I give the people what they want.
Everyone needs access, so storing the Word file on a cloud service like Box makes sense. In the past I created a subfolder in my repo that I shared—keeping everything within the root directory—but this time around I needed to store the file in a shared folder that someone else created. So my solution was to copy the Word file from this shared directory to my repository.
Technical Approach
I don't know how to make this a reproducible problem, but hopefully you will give me some latitude since I'm trying to make my work fully reproducible ;)
Let's say that my .Rnw file is stored in repoRoot/subfolder. Since knitr changes the working directory to subfolder where this .Rnw file is located, the first chunk sets the root.dir one level up at the project root.
<<knitr, include=FALSE>>=
library(knitr)
opts_knit$set(root.dir=normalizePath('../')) # go up 1 level
#
The next chunk copies the Word file from the shared folder to my git repo and runs the analysis file. The shared directory path is hard coded to my machine, which is the problem I'm writing for your help solving.
file.copy(from='/Users/ericpgreen/Box Sync/Project/Paper/draft.docx',
to='subfolder/draft.docx', # my repo
overwrite=TRUE)
source(scripts/analysis.R) # generates objects we reference in the .docx file
After adding \begin{document}, I include a chunk where I convert the .docx file to .txt and then rename it to .Rnw.
# convert docx to txt
system("textutil -convert txt 'subfolder/draft.docx'")
# rename txt to .Rnw
file.rename('subfolder/draft.txt',
'subfolder/draft.Rnw')
The next child chunk calls this .Rnw file that contains the text of the Word file with references to R objects included through \Sexpr{}:
<<include-draft, child='draft.Rnw', include=FALSE>>=
#
This works just fine for me. Whenever I knit the .Rnw file it grabs the latest version of the .docx file that my colleagues have edited (complete with track changes and comments) and, in a later step not shown here, returns the .pdf file to the shared folder.
Problem to Solve
This setup meets almost every need for me, except that the initial file.copy() command is hard coded to my machine. So if someone in my group clones my repo (e.g., research assistants who DO use version control), it won't run out of the box. Is there a workaround to hard coding in this type of case?
Ultimately you won’t get around hard-coding paths that are outside your control, such as paths to network shares. What you can and should avoid is hard-coding these paths in your documents.
Instead, relegate them to configuration files and/or environment variables (which, again, will be controlle by configuration files, to with .bashrc and similar). The simplest approach is then to use
network_share_path = Sys.getenv('NETWORK_SHARE_PATH',
stop('no network share path configured'))
file.copy(from = network_share_path, to = 'subfolder/draft.docx', overwrite = TRUE)

Resources