Line breaks in directory using knitr/R Markdown - r

I'm currently working with R Markdown and trying to get the code, results and documentation to one pdf-file with the knit function in the end.
Within in the document, I want to set my directory with the setwd command.
setwd("C:/Users/Testuser/Documents/Testfolder/Testsubfolder/Testsubfolder2/Testsubfolder3/Testsubfolder4/Testsubfolder5")
However, the file path exceeds the width of the pdf and there is no line break so part of it is cut off at the right side. When I try to insert a page break manually, for example by pressing Enter after "Testdubfolder2", I get the following Error message:
"Fehler in setwd("C:/Users/Testuser/Documents/Testfolder/Testsubfolder/Testsubfolder2/\n
Testsubfolder3/Testsubfolder4/Testsubfolder5") : kann Arbeitsverzeichnis nicht wechseln."
, which in English simply means, that the directory can't be changed. Is there any way to display the file path over two lines using the knit function? I'd be grateful for any solution or hint.

You can use file.path to construct paths from multiple subpaths, for example:
setwd(file.path(
"C:/Users/Testuser/Documents",
"Testfolder/Testsubfolder/Testsubfolder2",
"Testsubfolder3/Testsubfolder4/Testsubfolder5"
))

Related

file.remove() with dynamic input in R

I create dynamic LaTex reports in R markdown and want to delete the .tex file after the .pdf has been created as part of the overall function get_report. The name of the .pdf file in dynamic. I tried
file.remove(paste0("/sang/pect/apps/office/admin/", DATA$report_filename, ".tex"))
where DATA$report_filename is the name of the .pdf that is being created. So if i want to remove the file of a report called "hello", the path would be /sang/pect/apps/office/admin/hello.tex
When I call the function mentioned above, I however get an error: 'No such file or directory'
The reason is that R tries to remove the file /sang/pect/apps/office/admin/.tex
It just leaves out the dynamic part
But when i run
file.remove(paste0("/sang/pect/apps/office/admin/", DATA$report_filename, ".tex"))
in the console, all works well.
So what is my mistake?
If paste0("/sang/pect/apps/office/admin/", DATA$report_filename, ".tex") returns "/sang/pect/apps/office/admin/.tex", I would inspect the DATA$report_filename vector by e.g. using print() function.

What is the right way to show up images at R Markdown?

I am doing an html document from R Markdown (RStudio) and I think I am following the right instructions to show it up. I read the R's Bookdown (https://bookdown.org/yihui/rmarkdown) and there says this is the way, or at least this is what I get from it. I am using this only script line to make the image calling:
![Hadley Wickham](Macintosh HD/Usuarios/ivanmendivelso/Dropbox/Escuela/IntroR Vids/HW.jpeg).
When knitting I get this error message: "File Macintosh HD/Usuarios/ivanmendivelso/Dropbox/Escuela/IntroR Vids/HW.jpeg not found in resource path
Error: pandoc document conversion failed with error 99
Execution halted".
Does anybody know what am I doing wrong? Why I can not see images in my documents?
Thanks in advance.
It looks like your path is not correct.
If the image can be moved, put it in the same directory as your .Rmd file. If you can't move it to the file directory and if you don't speak UNIX, you can get the path by:
Finding the file with the finder
Right click on it
hold the option key on your keyboard and you will see the menu change from Copy "HW.jpg" to say Copy "HW.jpeg" as pathname
Paste that path into your code.
When you have file spaces in your file path you need to put single quotes around to make them strings
![Hadley Wickham](‘Macintosh HD’/Usuarios/ivanmendivelso/Dropbox/Escuela/‘IntroR Vids’/HW.jpeg)
What about this below
![Hadley Wickham](/Macintosh HD/Usuarios/ivanmendivelso/Dropbox/Escuela/IntroR Vids/HW.jpeg).
notice the / at the begining of the file path. on Mac the very first / is meaning from the root of your hard drive

R Shiny: 'error cannot open the connection'

so I have a Shiny app where I'm trying to read in a user-identified input file.
Towards that end, my ui.R has the line fileInput("predictor2", label = "Predictor Values") and I try to read the file using the line predictor <- read.delim("input$predictor2") in my server.R file.
However, I get a message saying Error: Cannot open the connection. If I don't try to read in the file and use another matrix of values, the code works fine. Any advice for how to fix this problem or more detail that would be useful?
You code is looking for a file with the literal name input$predictor2 which presumably does not exist. You first need to remove the quotes from around it, then add which column of the return actually has the path to the data, e.g.:
read.delim(input$predictor2$datapath)
See the help for fileInput for an example that checks to make sure something has been uploaded first.

Calling Skim from inside R

I'm making a simple line in r to automatically open my generated plots.
I output the plots to a file called "plots.pdf" in the same directory as my r file, and at the end i use this two lines to try to open it:
dir <- paste("/Applications/Skim.app/Contents/MacOS/Skim ",getwd(),"/plots.pdf",sep="")
system(dir)
Basically, dir concatenates the full path of the skim app and the full path of the generated plot.
If i run the string stored at dir in a shell it works perfect, it opens the pdf file in Skim, but when i run it with system() from inside R it doesn't work (Skim says 'The document “plots.pdf” could not be opened.').
I believe this is a very little mistake somewhere in the syntax regarding the absolute/relative paths, but haven't managed to find it... Any advice is welcome! (Or a better way to achieve the same)
I found a way to bypass that problem, i just changed the path to Skim for the 'open' command and i let the system to assign the default app for pdf viewing. So:
dir <- paste("open ",getwd(),"/plots.pdf",sep="")
And it works.

add local image file in R presentation

I'm trying to include one imaging file (.png) using R markdown for R presentation.
I followed the suggestion from: How to import local image using knitr for markdown
but by using ![title](my.png), I get this error:
Error: unexpected '[' in "!["
The my.png file is in current path. I also tried using absolute path but got the same error message.
Putting above inside the r chuck failed too.
I also tried
```{r,fig.width=350, fig.height=250,echo=FALSE}
library(png)
library(grid)
appimg <- readPNG('my.png')
grid.raster(appimg)
```
but failed too!
I am working on windows 7 R studio 0.98.1102 and R 3.2.1 .
After a huge mount of Google search. I finanlly figure out what is the problem.
The key is that HTML cannot refer local file for security reason. Except that it is a local HTML file, then it can refer local file in the same file directory.
And R presentation is actually a HTML file like a webpage.
So, just put your image file the same directory with the HTML file, things will work. At least it worked for me.
Just use
![some caption](img_file_name.png)
Removing the quotes actually worked for me and note that one better doesn't keep any spaces while naming the image as in use,
![title](my_image.png)
Using quotes or having spaces(when renamed) hasn't worked for me in this case.
The 'imager' package makes this pretty easy:
library(imager)
myimg <- load.image("<pathto>/file.png")
plot(myimg)
If your output format is : slidy_presentation
You can use html code:
<img src="/Users/name/folder/xyz.png";>
Further, if you wish to align and adjust the image to a specific side (say right of your final output and specify a size) you can use something like -
<img src="/Users/name/folder/xyz.png"; style="max-width:280px;float:right;">

Resources