R output to screen error - r

I get Rscript error of:
Error in dev.copy2pdf(file = fname, out.type = "pdf") :
can only print from a screen device
Execution halted
I am running the following R source in my Mac OSX console with:
Rscript --vanulla charts.R
I am using R version 3.3.2. Here is my source:
library(quantmod)
sym <- 'IBM'
d <- getSymbols(sym,src = "yahoo", auto.assign = FALSE)
chartSeries(d, name = sym, theme = "white", bar.type = 'ohlc',
line.type = "l",TA = "addVo();addSMA()",
subset = 'last 6 months')
addRSI()
dev <- dev.prev()
fname <- sprintf("%s.pdf",sym)
dev.copy2pdf(file = fname, out.type = "pdf")
dev.off()
How do I fix the dev.copy2pdf() if I want to output a PDF running on the conole. It runs fine within my RStudio,
Thanks

This seems to have fixed it
#https://stackoverflow.com/questions/5625394/problem-saving-pdf-file-in-r-with-ggplot2
pdf(fname)
chartSeries(d, name=sym, theme="white",bar.type='ohlc',line.type="l",TA="addRSI();addVo();addBBands();addSMA()",subset='last 6 months')
dev.off()

Related

get_file() error in google colab R enviroment

I have this error:
Error in get_file(fname = "flores.zip", origin = "https://drive.google.com/u/0/uc?export=download&confirm=sgo2&id=107ocoPLxNddbHp2MDsIWIYX9qb196WUv", : argument "file" is missing, with no default
Here is My code:
data_dir <- get_file(fname ="flores.zip",
origin ="https://drive.google.com/u/0/uc?export=download&confirm=sgo2&id=107ocoPLxNddbHp2MDsIWIYX9qb196WUv",
extract = TRUE
)
data_dir <- file.path(dirname(data_dir), "flores")
images <- list.files(data_dir, pattern = ".jpg", recursive = TRUE)
length(images)
In my desktop version RStudio works! But not in google colab R enviroment
can anybody help me?
Thanks!

Conceptual Stucture doesn't accept clust

I use the latest version of package and I try to run this:
I try to run this example:
library(bibliometrix)
download.file("https://www.bibliometrix.org/datasets/joi.zip", destfile = temp<-tempfile())
M <- convert2df(readLines(unz(temp, "joi.txt")), dbsource="isi",format="plaintext")
CS <- conceptualStructure(M, method="MCA", field="ID", minDegree=10, clust=5, stemming=FALSE, labelsize=8,documents=20)
but I receive this error:
Error in conceptualStructure(M, method = "MCA", field = "ID", minDegree = 10, :
unused argument (clust = 5)
What should change?

File paths with drake on a shared drive

I am encountering some odd drake behaviour which I just can't figure out. I am trying to add a .rmd to my drake plan. I am working on a remote machine AND on a network drive on that machine. If I try to add an .rmd file to my plan like this:
> library(drake)
> library(rmarkdown)
>
> list.files()
[1] "drake_testing.Rproj" "foo.png" "report.Rmd"
>
> plan <- drake_plan(
+ png("foo.png"),
+ plot(iris$Sepal.Length ~ iris$Sepal.Width),
+ dev.off(),
+ report = render(
+ input = knitr_in("report.Rmd"),
+ output_file = "report.html",
+ quiet = TRUE
+ )
+
+ )
>
> plan
# A tibble: 4 x 2
target command
<chr> <expr>
1 drake_target_1 png("foo.png")
2 drake_target_2 plot(iris$Sepal.Length ~ iris$Sepal.Width)
3 drake_target_3 dev.off()
4 report render(input = knitr_in("report.Rmd"), output_file = "report.html", quiet = TRUE)
>
> ## Turn your plan into a set of instructions
> config <- drake_config(plan)
Error: The specified file is not readable: report.Rmd
>
> traceback()
13: stop(txt, obj, call. = FALSE)
12: .errorhandler("The specified file is not readable: ", object,
mode = errormode)
11: digest::digest(object = file, algo = config$hash_algorithm, file = TRUE,
serialize = FALSE)
10: rehash_file(file, config)
9: rehash_storage(target = target, file = file, config = config)
8: FUN(X[[i]], ...)
7: lapply(X = X, FUN = FUN, ...)
6: weak_mclapply(X = keys, FUN = FUN, mc.cores = jobs, ...)
5: lightly_parallelize_atomic(X = X, FUN = FUN, jobs = jobs, ...)
4: lightly_parallelize(X = knitr_files, FUN = storage_hash, jobs = config$jobs,
config = config)
3: cdl_get_knitr_hash(config)
2: create_drake_layout(plan = plan, envir = envir, verbose = verbose,
jobs = jobs_preprocess, console_log_file = console_log_file,
trigger = trigger, cache = cache)
1: drake_config(plan)
I have tried the following permutations to make this work:
Move the .rmd to the local drive and call it with the full path to there
Add in file.path inside and outside of knitr_in to complete a full path.
Try using file_in for each of the scenarios above.
I have also tried debugging but I get a little lost when drake turns the file name into a hash then turns it back into the basename of the file (i.e. report.Rmd). The error ultimately happens when digest::digest is called.
Does anyone have experience attempting to figure out something like this?
I think the answer depends on whether you get the same error when you call digest("report.Rmd", file = TRUE) on its own outside drake_config(plan). If it errors (which I am betting it does) there may be something strange about your file system that clashes with R. If that is the case, then there is unfortunately nothing drake can do.
I also suggest some changes to your plan:
plan <- drake_plan(
plot_step = {
png(file_out("foo.png")),
plot(iris$Sepal.Length ~ iris$Sepal.Width),
dev.off()
},
report = render(
input = knitr_in("report.Rmd"),
output_file = "report.html",
quiet = TRUE
)
)
Or better yet, compartmentalize your work in reusable functions:
plot_foo = function(filename) {
png(filename),
plot(iris$Sepal.Length ~ iris$Sepal.Width),
dev.off()
}
plan <- drake_plan(
foo = plot_foo(file_out("foo.png")),
report = render(
input = knitr_in("report.Rmd"),
output_file = "report.html",
quiet = TRUE
)
)
A target is a skippable workflow step with a meaningful return value and/or output file(s). png() and dev.off() are part of the plotting step, and file_out() tells drake to watch foo.png for changes. Also, it is good practice to name your targets. Usually, the return values of targets are meaningful, just like variables in R.

How to modify R program to support RHadoop

I am new to RHadoop and R. I am having a normal R program which has a library(Methylkit). I am wondering can someone give some insights on how do I run this R program on hadoop. What do I need to modify in the original R program? It would be really help if some one gives me some idea.
The Code:
library(methylKit)
file.list=list( "new_sample1.txt","new_sample2.txt","n_sample3.txt")
myobj=read(file.list,sample.id=list("test1","test2","ctrl1"),assembly="hg19",treatment=c(1,1,0),context="CpG", pipeline=list(fraction=TRUE,chr.col=1,start.col=2,end.col=2,
coverage.col=6,strand.col=3,freqC.col=5 ))
getMethylationStats(myobj[[1]],plot=F,both.strands=F)
pdf("sample1_statistics.pdf")
getMethylationStats(myobj[[1]],plot=T,both.strands=F)
dev.off()
getMethylationStats(myobj[[2]],plot=F,both.strands=F)
pdf("sample2_statistics.pdf")
getMethylationStats(myobj[[2]],plot=T,both.strands=F)
dev.off()
getCoverageStats(myobj[[3]],plot=F,both.strands=F)
pdf("sample3_statistics.pdf")
getMethylationStats(myobj[[3]],plot=T,both.strands=F)
dev.off()
library("graphics")
pdf("sample1_coverage.pdf")
getCoverageStats(myobj[[1]], plot = T, both.strands = F)
dev.off()
pdf("sample2_coverage.pdf")
getCoverageStats(myobj[[2]], plot = T, both.strands = F)
dev.off()
pdf("sample3_coverage.pdf")
getCoverageStats(myobj[[3]], plot = T, both.strands = F)
dev.off()
meth=unite(myobj, destrand=FALSE)
pdf("correlation.pdf")
getCorrelation(meth,plot=T)
dev.off()
pdf("cluster.pdf")
clusterSamples(meth, dist="correlation",method="ward", plot=TRUE)
dev.off()
hc <- clusterSamples(meth, dist = "correlation", method = "ward",plot = FALSE)
pdf("pca.pdf")
PCASamples(meth, screeplot = TRUE)
PCASamples(meth)
myDiff=calculateDiffMeth(meth)
write.table(myDiff, "mydiff.txt", sep='\t')
myDiff25p.hyper <-get.methylDiff(myDiff,differenc=25,qvalue=0.01,type="hyper")
myDiff25p.hyper
write.table(myDiff25p.hyper,"hyper_methylated.txt",sep='\t')
myDiff25p.hypo <-get.methylDiff(myDiff,differenc=25,qvalue=0.01,type="hypo")
myDiff25p.hypo
write.table(myDiff25p.hypo,"hypo_methylated.txt",sep='\t')
myDiff25p <-get.methylDiff(myDiff,differenc=25,qvalue=0.01)
myDiff25p
write.table(myDiff25p,"differentialy_methylated.txt",sep='\t')
diffMethPerChr(myDiff,plot=FALSE,qvalue.cutoff=0.01,meth.cutoff=25)
pdf("diffMethPerChr.pdf")
diffMethPerChr(myDiff,plot=TRUE,qvalue.cutoff=0.01,meth.cutoff=25)
dev.off()
gene.obj <- read.transcript.features(system.file("extdata","refseq.hg18.bed.txt", package = "methylKit"))
write.table(gene.obj,"gene_obj.txt", sep='\t')
annotate.WithGenicParts(myDiff25p, gene.obj)
cpg.obj <- read.feature.flank(system.file("extdata","cpgi.hg18.bed.txt", package = "methylKit"),feature.flank.name = c("CpGi","shores"))
write.table(cpg.obj,"cpg_obj.txt", sep='\t')
diffCpGann <- annotate.WithFeature.Flank(myDiff25p,cpg.obj$CpGi, cpg.obj$shores, feature.name = "CpGi",flank.name = "shores")
write.table(diffCpGann,"diffCpCann.txt", sep='\t')
diffCpGann
promoters <- regionCounts(myobj, gene.obj$promoters)
head(promoters[[1]])
write.table(promoters,"promoters.txt", sep='\t')
diffAnn <- annotate.WithGenicParts(myDiff25p, gene.obj)
head(getAssociationWithTSS(diffAnn))
diffAnn
write.table(getAssociationWithTSS(diffAnn),"diff_ann.txt", sep='\t')
getTargetAnnotationStats(diffAnn, percentage = TRUE,precedence = TRUE)
pdf("piechart1.pdf")
plotTargetAnnotation(diffAnn, precedence = TRUE, main ="differential methylation annotation")
dev.off()
pdf("piechart2.pdf")
plotTargetAnnotation(diffCpGann, col = c("green","gray", "white"), main = "differential methylation annotation")
dev.off()
getFeatsWithTargetsStats(diffAnn, percentage = TRUE)
Are the *.txt files located in hdfs? If not, do put. You can use hadoop streaming to read data from hadoop.
line1 <- file('stdin')
open(line1)
while(length(line <- readLines(line1,n=1)) > 0) {
}
'stdin' is the input param to R-program from hadoop streaming jar. 'line' gets new line of data every time loop iterates.
Inside while loop do write the logic on what to do with line.
Use hadoop jar $HADOOP_HOME/contrib/streaming/hadoop-streaming.jar -input hdfs_input_file1, file2,n-files -output hdfs_output_dir -file mapper_file -file reducer_file -mapper mapper.R -reducer reducer.R to run the program.
-input accepts n-input files. Hadoop streaming jar reads one by one and feed to stdin

How to capture warnings with the console output?

I am trying to capture complete console log of my R script. I want a chronological order of everything, warnings printed as they occur. I tried this:
options(warn = 1)
tmpSinkfileName <- tempfile()
sink(tmpSinkfileName, split = TRUE)
cat("Doing something\n")
warning("Hi here")
cat("Doing something else\n")
warning("Hi there")
sink()
console.out <- readChar(tmpSinkfileName, file.info(tmpSinkfileName)$size)
unlink(tmpSinkfileName)
cat(console.out)
# Doing something
# Doing something else
warnings()
# NULL
but unfortunatelly the warnings are missing in console.out. How can I do this? According to the docs, options(warn = 1) should print the warnings as they occur. Unfortunatelly, they were not captured by sink().
Almost got it, but it's pretty complicated and it's pretty annoying that unlike the standard output, the message output cannot be split, i.e. redirected to the file and kept in the output at the same time (UNIX tee behaviour)!
options(warn = 1)
tmpSinkfileName <- tempfile()
tmpFD <- file(tmpSinkfileName, open = "wt")
sink(tmpFD, split = TRUE)
sink(tmpFD, type = "message")
cat("Doing something\n")
warning("Hi here")
cat("Doing something else\n")
warning("Hi there")
sink(type = "message")
sink()
console.out <- readChar(tmpSinkfileName, file.info(tmpSinkfileName)$size)
unlink(tmpSinkfileName)
cat(console.out)
If I try
sink(tmpFD, type = "message", split = TRUE)
it says
Error in sink(tmpFD, type = "message", split = TRUE) : cannot split
the message connection
which is pretty annoying!
I wrote the following function to capture ouput and messages:
create_log <- function(logfile_name, path) {
if (file.exists(paste0(path, logfile_name))) {
file.remove(paste0(path, logfile_name))
}
fid <- file(paste0(path, logfile_name), open = "wt")
sink(fid, type = "message", split = F)
sink(fid, append = T, type = "output", split = T)
warning("Use closeAllConnections() in the end of the script")
}

Resources