Saving text file in the data folder of an R package - r

I am trying to save a text file in the data folder of a private package I am developing.
I tried the following:
my_text <- “Some text string”
save.RDS(my_text, file = “C/…./package_name/data/mytext.rda”)
When I try to build the document, I get the error:
Error in FUN(X[[i]], ...) :
bad restore file magic number (file may be corrupted) -- no data loaded
Calls: suppressPackageStartupMessages ... <Anonymous> -> load_all -> load_data -> unlist -> lapply -> FUN
In addition: Warning message:
file mytext.rda' has magic number 'X'
Use of save versions prior to 2 is deprecated
Execution halted
Exited with status 1.
What could I do to save the text?

Library(readr)
write_rds(x = my_text, path = "C/…./package_name/data/mytext.rda")
try this.

Best way is using devtools::use_data(my_text, internal = TRUE) as mentioned by #PoGibas.

Related

Use save() within sfClusterCall()

I want to run a parallelized simulation using snowfall. Trying to aquire data using return() causes the cluster to exceed memory limitations. Data is at some point not recorded anymore.
So I want to use save() to write the data to a file after each replicate.
sfInit()
try<-function(x){
save(list=ls(),file=paste("myfilename_",x,".RData",sep=""))
}
sfClusterSetupRNG()
sfClusterCall(try,1:100)
sfStop()
The error I get is
Error in gzfile(file, "wb") : invalid 'description' argument
Calls: sfClusterCall -> do.call -> -> save -> gzfile
In addition: Warning message:
In if (!nzchar(file)) stop("'file' must be non-empty string") :
the condition has length > 1 and only the first element will be used

Trying to Knit but can't because read excel function is not being recognized in R

My whole program is working but when I try to knit to a document the same error is being thrown.
Quitting from lines 3-78 (Untitled.spin.Rmd)
Error in eval(expr, envir, enclos) : could not find function "read_excel"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
I don't under stand why. Where are the first 5 lines of my code.
Dataset_Stats1Project <- read_excel(Users/jamiestokes16/Desktop/Dataset_WomeninGov5.xlsx)
#Descriptive stats for important variables
summary(Dataset_WomeninGov$Wcongress)
summary(Dataset_WomeninGov$Wgov)
summary(Dataset_WomeninGov$Wleg)
Any help would be appreciated.
When you knit a document, knitr essentially creates a new R session for the code to run in. So any packages loaded session will not be accessible unless you load them directly within the .Rmd file.
I always find it easiest to include a chunk at the start of the document loading any packages used:
```{r LoadPackages, include = FALSE}
library(readlx)
# add other packages here
```

error using write_csv within R Markdown dowcument

When inputting
write_csv(MyComplicationData,"Data/MyComplicationData.csv")
as part of a .Rmd file, I get the following error when trying to knit the document:
Error in open.connection(path, "wb") : cannot open the connection
Calls: ... write_delim -> stream_delim -> open.connection
Execution halted
When I input the same command from the console, it works without a problem.
MyComplicationData is a tibble with 4812 observations of 7 variables.
You can use the getwd() command that return your working directory if the directory in which you want to save it is inside, otherwise you should give the entire path.
It would give something like this :
write_csv(MyComplicationData,paste(getwd(),"/Data/MyComplicationData.csv", sep = "")
or
paste0(getwd(),"/Data/MyComplicationData.csv")

Error knitting Rmarkdown doc: Error in bzfile(filename, "rb") : cannot open the connection

So I am trying to knit my code for an assignment on Coursera but I am encountering a strange error and I cant figure out the issue. Here is the code that I believe is the problem
setInternet2(use = TRUE)
fileUrl<- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileUrl, destfile = "c:/Users/musto101/Dropbox/DataScience/ReproducibleResearch/Assignment2/data/stormData.csv.bz2")
dateDownloaded<- date()
library(R.utils)
stormData<-bunzip2( filename = "stormData.csv", destname = "stormData3.csv")
head(stormData)
and the error:
Quitting from lines 13-35 (PA2.Rmd)
Error in bzfile(filename, "rb") : cannot open the connection
Calls: <Anonymous> ... eval -> eval -> bunzip2 -> bunzip2.default -> bzfile
In addition: Warning messages:
1: package 'R.utils' was built under R version 3.1.1
2: package 'R.oo' was built under R version 3.1.1
3: package 'R.methodsS3' was built under R version 3.1.1
4: In bzfile(filename, "rb") :
cannot open bzip2-ed file 'stormData.csv', probable reason 'No such file or directory'
Execution halted
So does anyone have a clue what is going on here? Any advice would be gratefully received.
Thanks
Looks like you forgot the .BZ2 suffix on the input filename. Code below should fix this.
stormData<-bunzip2( filename = "stormData.csv.bz2", destname = "stormData3.csv")
Just put a
require(R.utils)
at the top of that chunk and it will work fine. I had the same issue. I have this with a few other packages like dplyr too. Same resolution or put in R.utils:::bunzip2()

RWeka read.arff issues

I am reading a (sparse) arff using read.arff method of RWeka package. However I get the following error:
Error in .jnew("java/io/FileReader", file) :
java.io.FileNotFoundException: (No such file or directory)
The file I am trying to read in exists (file.exists('myfile.arff') return TRUE).
My one line code is:
data = read.arff(system.file('arff', 'myfile.arff', package='RWeka'))
Any ideas what might be going on?
Thanks.
Edit 1: traceback() output
> traceback()
4: stop(list(message = "java.io.FileNotFoundException: (No such file or directory)",
call = .jnew("java/io/FileReader", file), jobj = <S4 object of class "jobjRef">))
3: .External("RcreateObject", class, ..., silent = silent, PACKAGE = "rJava")
2: .jnew("java/io/FileReader", file)
1: read.arff(system.file("arff", "/home/andy/r/myfile.arff",
package = "RWeka"))
You seem to be using the format of the example in help(read.arff) without understanding why it is written using the system.file() function. If your .arff file is not in the package directory (and it does not appear to be so located), then you should not be using that function. Try instead:
mydat <- read.arff(file= "/home/andy/r/myfile.arff")
Or maybe just this if it is in your working directory:
mydat <- read.arff(file= "myfile.arff")

Resources