R Sweave output error - r

I'm using RStudio v0.96.331 with pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
I have a R project in the '/home/operacao/Myprojs/projName', which is my working directory.
Now, if i create a folder called 'reports' in '/home/operacao/Myprojs/projName/reports', and inside the sweave file (which is in the reports folder) use the code
setwd('/home/operacao/Myprojs/projName')
After loading some packages, i receive the error
Error in driver$finish(drobj) :
the output file 'my_report.tex' has disappeared
Calls: <Anonymous> -> <Anonymous>
Execution halted
But the file is in the folder, and the plots i made appear in the .pdf. The text output
does not appear.
Anyone knows why that happens? If i save the Sweave files in my directly in my working directory, everything works fine.
Thanks!

Probably RStudio requires you to set the working directory to the location which contains the Sweave file. Why do you need to set your working directory to another directory? You could use source to load any R code files which are in projName.

Related

R studio will not remember my file path for a code chunk

When I run the following code I get an error.
library(tidyverse)
day <- read.csv("day.csv")[, -1]
glimpse(day)
Result:
Show in New Window
Error in file(file, "rt") : cannot open the connection
However when I specify the file path it works:
library(tidyverse)
setwd("~/UofUProjects/IS6489")
day <- read.csv("day.csv")[, -1]
glimpse(day)
I don't want to have to specify the file path for every single code chunk I run. Is this a setting I may have accidentally disabled? I tried deleting an re adding the file. I also tried clicking more and choosing set working directory and I still get the same error. I also tried updating Rstudio and R to the newest versions.
here is my working directory
The R markdown file I was using was saved in my download folder instead of my project folder. I was trying to specify the working directory folder with the R studio settings, and thought it would recognize however it did not work unless I actually saved the R markdown file in the project folder or used setwd(). I ran the code without the setwd() after I saved in the correct location and it worked just the way I wanted it to.

My R scripts perfectly work when running , but when I use the same scripts in R markdown get an error "does not exist in current working directory"

My R scripts perfectly work when running when in scripts file in R studio, but when I use the same scripts in R markdown get an error; file "does not exist in current working directory"
Both are in the same wd.
What may be the reason?.
Note: All my work do in google drive offline.
This is probably because R looks for files relative to the current working directory - by default Sys.getenv("HOME"), whereas knitr looks in the same directory as the Rmarkdown file.
The solution is to specify the correct full or relative path to files in the RMarkdown code.
This is what the new here R package was designed for. It always looks at the root of the R project directory (which is what "here" refers to). It doesn't matter if your Rmarkdown file is in a subdirectory.
library(here)
here("file_i_want.csv")
This will work the same regardless of if you use R scripts or Rmarkdown
More details here (pun intended):
https://github.com/jennybc/here_here

Ignore specific files in packrat search

I am creating reports using R, RStudio, knitr, and packrat. I have a project folder structure similar to below:
project_folder/
- packrat/
- .Rprofile
- analaysis_folder/
- library.R
- child.rnw
- data_folder/
- knitr_rnw_location/
- file.rnw
- .Rprofile
And have set up the .Rprofile with the appropriate lines in the main project_folder and the subdirectory of the .rnw file, according to the recommendations given in RStudio's Limitations and Caveats page.
When I run packrat::init() at the project_folder level, the packrat folder is set up. Then when I open the file.rnw the packrat library is all set up.
However, when I execute packrat::snapshot() it gives errors
Unable to tangle file knitr_rnw_location/file.rnw; cannot parse depndencies
and fails. Is there a way to tell packrat to ignore my .rnw files? All library() are called from separate .R scripts and are source() through the .rnw files. It also searches any variables declared in the knitr chunks and gives the error
Error in eval(x, envir = envir): object 'my_variable_name' not found
In the end, it does state
Snapshot written to "~/project_folder/packrat/packrat.lock"
So I can only assume that packrat::snapshot() was successful. Has anyone else run into the same issue when working with knitr and packrat?
Much appreciated,

How to run an Rmd shiny markdown doc from online repo?

I have an interactive knitr/rmarkdown doc that uses shiny apps.
I want to place the .Rmd file on either my personal site or GitHub.
How would I be able to remotely run this file directly from online?
For example, when I run the Rmd file from my local machine, I use
rmarkdown::run("./IntroToShinyWorkshop.Rmd")
But when I try to run a file form online I get an error that the path doesn't exist (w/ one of the issues being that it merges the url with my wd).
run("IntroToShinyWorkshop.Rmd",dir = "https://raw.githubusercontent.com/theforestecologist/ShinyWorkshop/master/")
Error in run("https://raw.githubusercontent.com/theforestecologist/ShinyWorkshop/master/IntroToShinyWorkshop.Rmd") :
The directory 'C:/Users/theforestecologist/Documents/https:/raw.githubusercontent.com/theforestecologist/ShinyWorkshop/master' does not exist
In addition: Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="https://raw.githubusercontent.com/theforestecologist/ShinyWorkshop/master": The filename, directory name, or volume label syntax is incorrect
Related: Once I do figure out how to run a file from an online repo, I'm also interested in how to change the working directory to that url so that I can utilize other files there.
For example, I have a wrapper function (.R file) I can source that will call my .Rmd file (instead of running the Rmd file directly) -- however, I get the error that I cannot change the wd in my wrapper function to access the .Rmd file online.

R-Studio cannot read .txt file

I have a problem that I cannot solve, and it is the following: I am writing a document with R-Studio, but every time I try to run it, the program says to me that there is an error. The error apparently looks like to be linked to the fact that R studio is not able to load a table from a .txt file.
This is what I have done:
I opened R-Studio and I set the working directory (with the setwd() command) in the folder where the .txt file from which I want to source the table is located;
I opened the R-Markdown script file (with .Rmd extension) and I run it ('knit HTML' button);
R-Studio gave me the following error:
Errore in eval(expr, envir, enclos) : oggetto "mydata" non trovato; Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval
How can I solve this? I cannot figure out why R-Studio is not able to read the table and load the data.frame, while the Console inside R-Studio is.
Thank you for any help you can provide.
When you knit a document in R-Studio, this happens in a separate environment. This means that variables that are available while you type in the console are not available when you knit a document. Similarly, when you set the working directory in the console, this has no effect on knitting a document.
So the solution to your problem could be to simply add the setwd() command to your .Rmd file.
In your R Markdown file, you have to specify the path to the txt file relative to the location of the Rmd file.

Resources