Why does R Markdown not want to Knit with spss file? - r

I performed a multilevel analysis in R (following Kay Chansiri's tutorial but using my df). It worked as it should, but then I wanted to do it again in R Markdown so as to make it into a report. The code works fine in R Markdown, but it will not knit it. Everything seems to revolve around the spss file I am using.
Here is the code from the first chunk:
```{r MLM_deJong}
MLM_deJong<-read_sav("dataset_MA2.sav")
View(MLM_deJong)
```
And the next chunk:
```{r}
MLM_deJong = MLM_deJong %>% replace_with_na(replace = list(business = 999))
mean(MLM_deJong$business, na.rm = T)
MLM_deJong$business[is.na(MLM_deJong$business)] <- mean(MLM_deJong$business, na.rm = T)
```
and so on...
R imports the df and performs all the operations. But when I want to knit it, here is what the R Markdown tab in the console writes:
Line 46Error in read_sav("dataset_MA2.sav) : could not find function "read_sav" calls: ... handle -> withCallHandlers -> withVisible -> eval -> eval.
Needless to mention, no file is knit.
I have of course installed the 'haven' package. The same occurs when I tried read.spss through the 'foreign' package.

Even though you have installed the haven package, it is not automatically loaded when rmarkdown knits.
Option 1: Load the haven package before you use it.
library(haven)
MLM_deJong<-read_sav("dataset_MA2.sav")
Option 2: Prepend haven:: to read_sav
MLM_deJong<-haven::read_sav("dataset_MA2.sav")

Related

Using base load in an active renvironment

I am working on an R markdown notebook in an active renv in my R project directory. Before I had activated the environment, I could use the load function from base R without a problem. But since activating the environment, I get errors when I use load(file=abc.RData).
I have a code chunk as follows:
rm(list=ls())
library(haven)
library(dplyr)
library(data.table)
# Load complete (individual+network) data ---------------------------
load(file="/Volumes/caas/CADRE CLC Data Project5/Clean Data/AK-SU-NETWORKS-ROUT/eda.RData")
If I try to run the chunk, I get the following error:
Error in load(file = "/Volumes/caas/CADRE CLC Data Project5/Clean Data/AK-SU-NETWORKS-ROUT/eda.RData") :
unused argument (file = "/Volumes/caas/CADRE CLC Data Project5/Clean Data/AK-SU-NETWORKS-ROUT/eda.RData")
But if I knit the document, it compiles without a problem.
What might I be doing wrong?
It looks like all I had to do was specify that I wanted to use load from base, as:
base::load(file="/Volumes/caas/CADRE CLC Data Project5/Clean Data/AK-SU-NETWORKS-ROUT/eda.RData")
That seems to have fixed it.

RMarkdown: "Error: path for html_dependency not found:"

I am a Chemist who uses R and R Studio to analyze mass spectrometry data. I have automated scripts that process my data, because there are a lot of files. The scripts call rmarkdown using render() to import data, manipulate the data frame, save the processed data frame as a .csv and generate an html of plots. I have a problem rendering rmarkdown files since I recently updated to R v3.5.1 and R Studio v1.1.463. The error I receive is: Error: path for html_dependency not found: which I get if I use the knit button in R Studio, or if I use render(). The script inside the rmarkdown runs to completion, as the resulting objects are present in the environment window in R Studio, but the html file doesn’t render, and I get the error which stops the loop of render() I use to process all the files in a specific folder. This is a process I have done many times before updating.
I ran traceback() and got the following results:
8: stop("path for html_dependency not found: ", file, call. = FALSE)
7: FUN(X[[i]], ...)
6: lapply(dependencies, validate_html_dependency)
5: dependency_resolver(all_dependencies)
4: html_extras_for_document(knit_meta, runtime, dependency_resolver,
format_deps)
3: base(...)
2: output_format$pre_processor(yaml_front_matter, utf8_input, runtime,
knit_meta, files_dir, output_dir)
1: render("Processing and QA Template_INT_FINAL_MFAssignR.rmd",
output_file = paste0(substr(file_list[i], 1, nchar(file_list[i]) -
4), ".html"), output_dir = folder, params = list(data = file_list[i], path = folder))
I have uninstalled and reinstalled packages several times. I even removed my library folder entirely once, then reinstalled. My packages are up to date, but I had problems installing packages since the update, including rmarkdown. I had to use install.packages(“package”, type=”binary”) to install the dependencies for rmarkdown in order to get it to install. Normally I can just use the Install button in R Studio.
This is a work PC (Windows 10, 64 bit) I do not have administrator access to. I have to uninstall/install through IT at my university, which is a hassle, so I want to come to them with a plan. My package library defaults to a network drive that I can read/write to, and I have limited access to the hard disks; either way I can’t seem to change where packages are installed in R Studio. I don’t know if I can re-install an older version of R and R Studio or if it would help. A lot of the packages I use are developed for the current version of R and I was having some other issues, which was why I updated in the first place. The exact same scripts and data files run properly on another PC (my personal laptop, which is also up to date on R, R Studio and packages); the only changes I made are to the working directory so the data loads properly. I also had no trouble installing packages.
Here is an example of my code; since my scripts are very long, complicated and data specific, I have prepared a more simplified version of what they do as an example. I really don’t think there is anything wrong with the scripts themselves, as I mentioned before I have run them many times before the update. I suspect something is wrong with either the install of R, R studio or rmarkdown.
The main script, which calls render() from rmarkdown:
setwd("D:/Working Directory")
library("rmarkdown")
folder=paste0(getwd(),"/")
file_list=list.files(path=folder, pattern="*_MF.csv")
for (i in 1:length(file_list)){
render("Processing and QA Template.rmd",
output_file = paste0(substr(file_list[i],1, nchar(file_list[i])-4),".html"), output_dir = folder,
params=list(data=file_list[i], path=folder))
}
The rmarkdown, named “Processing and QA Template.rmd” which is in "D:/Working Directory":
---
title: "Example Processing and QA"
author: "Matt Brege "
date: "2018-12-12"
output: html_document
params:
data: x
path: x
---
```{r, echo=FALSE, message=FALSE}
library(dplyr)
library(ggplot2)
library(tidyr)
file_name <- substr(params$data, 1, nchar(params$data)-7)
folder <- params$path
input <- tbl_df(read.csv(paste0(file_name, "_MF.csv"), stringsAsFactors = FALSE))
```
```{r, echo=FALSE}
#...a series of long and complicated data manipulations later…
write.csv(input7, paste0("Output/", file_name, "_QAd.csv"), row.names=FALSE, na="")
```
```{r,r, echo=FALSE, warning=FALSE, message=FALSE}
#...plotting section…
# these are just examples
p1 <-ggplot(diamonds, aes(x=carat, y=price)) + geom_point()
print(p1)
p2<- ggplot(diamonds, aes(x=carat, y=price, color=clarity)) + geom_point()
print(p2)
p3<- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point()
print(p3)
```
Finally, I have found the following similar posts about what appears to be the same issue, but they are old and from different versions of R, and it doesn’t seem like it was resolved in those instances anyway:
RMarkdown cannot knit: html_dependency not found
RNotebook cannot output due to html_dependency not found
R looks in the wrong place for html dependency
One suggestion from the last link was to clear the cache: “If you cached the chunk containing HTML widgets, you may need to invalidate the cache after you update R packages. – Yihui Xie Dec 6 '17 at 19:00” but I am not exactly sure what that means or how to do it. I generally run cat("\014") and rm(list=ls()) at the beginning of each script, but I don’t know if that’s what the suggestion means, and it has not helped.
I got it to work by:
Clear Knitr Cashe... (see the little arrow next to the Knit-icon in RStudio).
I also had to restart R (.rs.restartR() ).

Error there is no package called RevoUtilsMath with R Markdown

I am running R 3.4.3 and 3.5.1 (non-Microsoft version) and RStudio version 1.1.456. I am trying to knit some code into RMarkdown. However, I get the following error:
Error in library(p, character.only = TRUE) : there is no package called 'RevoUtilsMath'Calls: <Anonymous> ... suppressPackageStartupMessages -> withCallingHandlers -> library
Execution halted
The package RevoUtilsMath is part of the MKL install with Microsoft R. I cannot install it as a supplemental package with 'regular R'. The script itself runs fine, it just does not work in R Markdown.
The following libraries are loaded:
```{r loadLibraries, echo=FALSE, warning=FALSE}
library(RODBC)
library(dplyr)
library(markovchain)
library(DT)
library(reshape2)
library(knitr)
library(ggplot2)
library(scales)
library(PerformanceAnalytics)
library(plotly)```
The missing package error happens when executing the code below in Markdown. It is called using this code.
```{r histogram1, echo=FALSE, cache=TRUE}```
The histogram1 code is below (very standard ggplot).
g <- ggplot(dataClean, aes(x = IncSnapshotDay, fill = Represent)) +
geom_histogram(bins=70, alpha = .8) +
scale_fill_manual(values = colors) +
scale_x_continuous(labels = comma, limits = c(0,40000)) +
facet_wrap(~SnapshotDay) +
ylim(0,4000) +
theme_bryan()
g
I thought that maybe one of these packages has a dependency, so I ran the following to find out.
library(tools)
> dependsOnPkgs('RevoUtilsMath')
It returns character(0) which indicates that none of the packages depend on it. I did a test of the function on ggplot2, and it works
dependsOnPkgs('ggplot2')
[1] "dendextend" "GGally" "ggthemes" "plotly" "viridis" "caret" "crosstalk"
[8] "DT"
So why does R Markdown/knitr generate this error since the code itself runs fine outside of Markdown, and how do I fix this?
I think you are experiencing a problem similar to one I just had though, without more detail, it is hard for me to know.
In my case, the problem was caused by knitr caching the list of packages used by a previous author (using Microsoft R). The immediate solution was to simply clear the knitr cache (via the "Knit" drop down menu in RStudio) before attempting to knit the code.
I still don't really understand why this is happening or how to avoid it in future situations, but this at least provides a way to create the document even in the face of this behavior.

R Markdown error in code when knit to HTML

I am trying to run code chunks in my markdown document. I have an R script that runs all the code that I need without any issues. Then, when I copy and paste the code into the markdown document, the code will run within the chunk, but will fail when trying to knit into an output document (html/pdf).
I had to create a safe.ifelse function to prevent r from converting my dates to a numeric format as discussed here.
The error appears to be with the code:
safe.ifelse = function(cond, yes, no){structure(ifelse(cond, yes, no), class = class(yes))
}
The error message I get is:
Line 121 Error in structure(ifelse(cond,yes,no), class = class(yes)) : could not find function "days" Calls: ... transform.data.frame ->eval->eval-> safe.ifelse-> structure Execution halted
The line of code following my safe.ifelse function is
seminoma1 = transform(seminoma1, recur.date = safe.ifelse(salvage.tx=="Yes",
date.diagnosis + days(pmax(time.rad, time.chemo, na.rm=TRUE)), NA))
Any help would be appreciated. Thanks.
I'm still too new to comment, but the only time I get an error like that is when I forget to define a function/variable or forget to source a package.
Since days() isn't part of R's base package, I think you need to add:
```{r echo = FALSE}
library("lubridate")
```

Error in R. Error in gsub("(?<=\n)(?=.|\n)", continue, x, perl = TRUE) :

I am encountering an error in R that I cannot seem to figure out. I am creating an R markdown document where I read in an a csv table using this code.
iati <- read.csv(file="/filepath/IATI_NGOS.csv",head=TRUE,sep=",")
and then using ggplot2 I create a plot using the following code.
figure_one <- ggplot(iati, aes(iati$reporting.org))+
geom_bar(fill="blue")+
ylab("Total Activities")+
xlab("NGO Reporting Organizations in IATI")+
ggtitle("Total Number of Activities compared to each NGO Reporting Organization in IATI")+
coord_flip()
When I try to call figure_one in the R markdown I get the following error:
Quitting from lines 44-55 (NGO_IATI.Rmd)
Error in gsub("(?<=\n)(?=.|\n)", continue, x, perl = TRUE) :
input string 1 is invalid UTF-8
Calls: <Anonymous> ... paste -> comment_out -> line_prompt -> paste -> gsub
In addition: Warning message:
In grep("\n", message) : input string 1 is invalid in this locale
Execution halted
When I run this code in a regular R script I have absolutely no issues. I have search for some answers but can't figure it out.
Thanks!
I ended solving my issue by just doing a fresh install of R and Rstudio on my local machine. I think the recent update to Yosemite on my local environment created a lot of issues with the TeX plugin I had installed for R markdown.
I get the same question when I knit my rmarkdown document and find **encoding is the cause.
When you use functions like read.csv, fread or read_csv, you will read the column name.
If column names are in other languages, like Chinese, the problem will easily happen.
Or you rmarkdown works on Windows, but the encoding bug happens on Mac, a different environment.
The temporal solution is to rename the column name in English and resave the data files.
Here is the pseudocode in R to show my idea.
library(data.table)
library(tidyverse)
fread('yourfile.csv',encoding = 'UTF-8') %>%
purrr::set_names(c('x1','x2','x3')) %>%
write_excel_csv('yourfile_2.csv')
Here the new file yourfile_2.csv is fine to rmarkdown knit without encoding problems happening.

Resources