I use Windows and try to read .Rdata file to RMarkdown using rio's import function. It keeps giving me errors. This works just fine when I'm using R code in the same folder.
So here is the code in the first RMarkdown code chunk
{r setup, include = FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(rio)
df_clean <- import("data/df_clean.rdata")
Error in import("data/df_clean.rdata") : No such file
Is there a different between using R Code or RMarkdown? This also works fine when I type it in the R console, but doesn't work in the R Code chunk in the RMarkdown.
When I check in the working directory, the file is there
> getwd()
[1] "C:/Users/Project/Project A"
> list.files()
[1] "code" "data"
[3] "documentation" "output"
> list.files("data")
[1] "archive" "df_clean.rdata" "df_unique.rdata"
I'm new to R and just start coding this year. I hope I can do my EDA in RMarkdown to become more organized. Kindly help me with the question format if I did not posted it correctly.
if you unsure of the file path of the RData to import, use this to make Selection manually first..
df_clean <- import(file.choose())
or you can also get the full path of your RData stored in variable by doing:
RData_path <- file.choose()
df_clean <- import(RData_path)
Related
I am typing up some results comparing various methods for importing unicode files in R. I read in a CSV file using dplyr::read_csv() and utils::read.csv(), then compare them using dplyr::setdiff(). In fact, the two data files are different because the column names are read differently by the default options of the two methods. This causes an error to be written to the console. I know how to fix the error itself; that is not the issue. The three commands in the markdown file are
```{r read.csv message=FALSE, warning=FALSE, error=FALSE}
dplyr_read_csv <- read_csv("World Class.csv")
base_read_csv_T <- read.csv("World Class.csv")
setdiff(dplyr_read_csv, base_read_csv_T)
```
..which, when run as an R script result in:
> setdiff(dplyr_read_csv, base_read_csv_T)
Error in `setdiff()`:
! `x` and `y` are not compatible.
✖ Cols in `y` but not `x`: `height..in..`, `weight..lb..`, `height..cm..`,
`weight..kg..`, `中文..Simplified.`, `中文..Traditional.`, `English..GB.`,
`English..US.`, `Русский.язык`, `Tiê.ng.Viê.t`.
✖ Cols in `x` but not `y`: `height (in.)`, `weight (lb.)`, `height (cm.)`, `weight
(kg.)`, `中文 (Simplified)`, `中文 (Traditional)`, `English (GB)`, `English
(US)`, `Русский язык`, `Tiếng Việt`.
I would like for this error to show in the generated output.
When I put the code in the R markdown file, the file will not knit because of this thrown error. I have done the obvious things like putting error = FALSE in the R chunk, but none of my efforts have worked.
You can find the data file (named World Class.csv) and the R markdown file (read_world_classes.Rmd) in my github repository. What should I do to include this chunk and its error in the markdown file?
(There is a regular R script read_world_classes.R that I initially made to compare a bunch of other file types aside from CSV. That's what all the other files in the repository are).
As #bs93 said above, the correct output is obtained by adding error=TRUE to the r chunk:
```{r error=true}
<code goes here>
```
Please forgive me if this is not perfect but this is my first post.
I am currently working on trying to transform a large number of .docx documents into .pdf
I have found the RDCOMClient package which has done wonders. However I now need to add alt text into my charts. The code I am using is below:
library(RDCOMClient)
library(plyr)
library(tidyverse)
# this will destroy all objects in your workspace so be careful
# rm(list = ls()) # deletes all data frames
file <- "directory"
wordApp <- COMCreate("Word.Application") # create COM object
wordApp[["Visible"]] <- FALSE #opens a Word application instance visibly if true
wordApp[["Documents"]]$Add() #adds new blank docx in your application
wordApp[["Documents"]]$Open(Filename=file) #opens your docx in wordApp
#THIS IS THE MAGIC
wordApp[["ActiveDocument"]]$SaveAs("Directory",
FileFormat=17) #FileFormat=17 saves as .PDF
wordApp[["ActiveDocument"]]$Close(SaveChanges = 1) # says there are no changes that need saving
Where there is function in double [] like Documents is there something for chart.
I have found a full list of them for excel at the link here: http://www.omegahat.net/RDCOMClient/Docs/introduction.html
However I tried to install the SWinTypeLibs package to get the same thing for word using the following code:
install.packages("remotes")
remotes::install_github("omegahat/SWinTypeLibs")
and keep getting an error
if anyone has a list for word like the excel above would really need it.
Thanks for all the help in advance.
James
I've applied a quick fix to the .Rd docs in the original package from omegahat. You can find it here--should compile now.
karnner2/SWinTypeLibs
devtools::install_github("Karnner2/SWinTypeLibs")
Here is the question:
In file.r I ran an extensive analysis based on a huge dataset.
Every time I open the file I just need to load the libraries and everything is ready.
I don't need to download anymore any of the dataset inputs I need.
Now I have created a RMD file.rmd with the same code of file.r to present its findings.
I'm trying to get a preview of how the pdf will look like.
The problem is that when I click "Knit to pdf", it starts to download all the packages and datasets again. I have to wait hours to see the effects of small changes in code.
And there is more:
Some objects created in R file simply are not working in the rmd file.
Ex: in R file I coded:
edx2 <- edx2 %>% mutate(timeRr = yearRating - release)
When I try to run the same code in the rmd file I get the message:
Error in Func(x[[i]],...) : object 'timeRr' not found calls: f -> scales_add_defaults - > lapply - > fun
The same libraries loaded in both files (r and rmd)
What am I doing wrong?
1) At the end of the data analysis (file.R), save the data you need for the Notebook in a .RDS file.
For example, if you generated 3 results : res1, res2 and res3
results <- list(res1 = res1, res2 = res2, res3 = res3)
saveRDS(file = 'results.RDS', results)
2) instead of sourcing the analysis script, just read the results in the Notebook (.Rmd)
data <- readRDS('results.RDS')
# Results available for further use in the Notebook
data$res1
data$res2
data$res3
The error you get with edx2 is probably due to the fact that a new session is opened during generation of a notebook : are you sure that file.R really generates edx2, or is it only available in your current session?
I am attempting to pull R code chunks over HTTPS from an R script into a LaTeX document.
The .R file is in rstudio server and shared via webdav.
The LaTeX document resides on a server that cannot store files locally (ShareLaTeX).
Therefore, to get around the problem, I thought I'd use URL calls,
the following works for pulling in data:
<<load_data, echo=FALSE, cache=FALSE>>=
library(RCurl)
x <- getURL("https://user:pass#my.webdav.server.net/webdav/data/data.csv")
y <- read.csv(text = x,stringsAsFactors=FALSE,na.strings = "NA")
y
#
However, I would also like to pull in code chunks.
I have tried the following:
<<external-code, cache=FALSE>>=
z<-getURL("https://user:pass#my.webdav.server.net/webdav/model.R")
read_chunk(z, lines = code, labels = "foo")
#
However, this returns the error:
error in read_chunk(z, lines = code, labels = "foo"): object `code` not found
Is there some way to make knitr parse this variable as a file, or read the external URL?
I'm new to using Markdown, and have looked for a similar problem to this on SO without success. I'm using Rmarkdown (with Rstudio and knitr) to write a vignette that describes reading in a datafile which is imported as part of the package. I can correctly access the datafile using
> system.file("extdata", "Marlin-tag38606.txt", package = "xtractomatic")
I want to show the first few lines of this file in the vignette, so my code reads
```{r, results=as.is}
datafile <- system.file("extdata", "Marlin-tag38606.txt", package = "xtractomatic")
system(paste("head -n5 ",datafile))
```
The problem is that the results of this call are output to the Rmarkdown console and NOT to the vignette html file.
The output in the Rmarkdown window of RStudio is (but formatted nicer):
|................... | 29%
label: unnamed-chunk-8
date lon lat lowLon higLon lowLat higLat
4/23/2003 203.899 19.664 203.899 203.899 19.664 19.664
4/24/2003 204.151 19.821 203.912597 204.389403 18.78051934 20.86148066
4/30/2003 203.919 20.351 203.6793669 204.1586331 18.79728188 21.90471812
5/1/2003 204.229 20.305 203.9943343 204.4636657 18.90440013 21.70559987
|.................... | 31%
Which is what I wanted outputted to the vignette text, but it is not there. Within the resulting vignette all I have is the two lines of R code, but not the output from the system call.
Any advice would be appreciated. Thanks.
Cara Wilson
Use intern = TRUE for system(), then cat() the output:
cat(system(paste("head -n5", datafile), intern = TRUE), sep = '\n')
Using a bash chunk in the Rmarkdown document does the job for me.
For example with the package testdat which has .csv file in its extdata directory:
```{bash}
head -n5 ~/R/x86_64-pc-linux-gnu-library/3.3/testdat/extdata/2012.csv
```
will give in your html file:
## 14,,2012,Censo,1775351,,
## 14,,2012,Votantes,1135568,64.0,
## 14,,2012,Nulos,9168,0.8,
## 14,,2012,Válidos,1126400,99.2,
## 14,,2012,Blancos,14640,1.3,
I am not sure though this option existed in 2014 when you've asked the question.