How do I set pages = odd pages using pdf_convert() in r - r

library(pdftools)
sapply(list, function(x)
pdf_convert(x, format = "png", pages = NULL, filenames = NULL, dpi = 300, opw = "", upw = "", verbose = TRUE))
I know that pages = NULL means for all pages. I am having trouble how to do only odd pages. Does anyone know what I should put to do only odd pages? Thank you in advance.

The pages argument accepts a vector of the page numbers you want. You could make a sequence of the odd page numbers with the help of the total available in pdftools::pdf_info().
sapply(list, function(x)
pdf_convert(x,
format = "png",
pages = seq(from = 1, to = pdf_info(x)$pages, by = 2),
filenames = NULL,
dpi = 300,
opw = "",
upw = "",
verbose = TRUE))

Related

saving and naming files in R automatically based on input filename

I have generated several Utilisation Distributions (UD) with AdehabitatHR and stored them as Geotiffs. I am now using the same UDs with the Lattice package to generate some maps and saving them to a high-res tiff image with LZW compression. Problem is that I have literally hundreds of maps to make, save and name. Is there a way automatically do this once i have loaded all the necessary files from a directory? Each one of my UDs has the following structure of the filename "UD_resolution_species_area_year_season. tif" and in the final name I give to my map I would like to keep the same structure (or entire filename) but add the prefix "blablabla_" e.g. "blablabla_UD_resolution_species_area_year_season.tiff". The image also include a main name, a capital letter, which should also change.
At the moment I am using the following:
rlist = list.files(getwd(), pattern = "tif$", full.names = FALSE)
for (i in rlist) {
assign(unlist(strsplit(i, "[.]"))[1], raster(i))
}
shplist = list.files(getwd(), pattern = "shp$", full.names = FALSE)
for (i in shplist) {
assign(unlist(strsplit(i, "[.]"))[1], readOGR(i))
}
UD <- 'UD_resolution_species_area_year_season'
ext <- extent(UD) + 0.3 # set the extent for the plot
aa <-
quantile(UD,
probs = c(0.25, 0.75),
type = 8,
names = TRUE)
my.at <- c(aa[1], aa[2])
my.at <- round(my.at, 3)
maxval <- maxValue(UD)
tiff(
"C:/myworkingdirectory/maps/blablabla_UD_resolution_species_area_year_season.tiff",
res = 600,
compression = "lzw",
width = 15,
height = 15,
units = "cm"
)
levelplot(
UD,
xlab = "",
ylab = "",
xlim = c(ext[1], ext[2]),
ylim = c(ext[3], ext[4]),
margin = FALSE,
contour = FALSE,
col.regions = viridis(1000),
colorkey = list(at = seq(0, maxval)),
main = "A",
maxpixels = 2e5
) + latticeExtra::layer(sp.polygons(Land, fill = "grey50", col = NA)) + contourplot(
`UD`,
at = my.at[1],
labels = FALSE,
margin = FALSE,
lty = 2,
col = "orange",
pretty = TRUE
) + contourplot(
UD,
at = my.at[2],
labels = FALSE,
margin = FALSE,
lty = 2,
col = "red",
pretty = TRUE,
)
dev.off()
It is a common beginners mistake to use assign. Do not use it, it creates the type of trouble you are now facing. In stead, you can make lists and/or use a loop.
Also what you are asking is basic R stuff, but you are complicating the question with adding lots of irrelevant detail about setting the extent, and levelplot. It is better to learn about doing these basic things by removing the clutter and focus on a simple case first. That is also how you should write questions for this forum.
In essence you have a bunch of files you want to process. Below I show how you can loop over a vector of the names and then loop and do what you need to do in that loop.
library(raster)
rastfiles <- list.files(pattern = "tif$", full.names=TRUE)
outputfiles <- file.path("output/path", paste0("prefix_", basename(rastfiles)))
for (i in 1:length(rastfiles))
r <- raster(rastfiles[i])
png(outputfiles[i])
plot(r)
dev.off()
}
You can also first read all the files into a list
rastfiles <- list.files(pattern = "tif$", full.names=TRUE)
rlist <- lapply(rastfiles, raster)
names(rlist) <- gsub(".tif$", "", basename(rastfiles))
rastfiles <- list.files(pattern = "shp$", full.names=TRUE)
slist <- lapply(shpfiles, readOGR)
names(slist) <- gsub(".shp$", "", basename(shpfiles))
And perhaps create a vector of output filenames
outputtif <- file.path("output/dir", basename(rastfiles))
And then loop over the items in the list, or the output filenames

R to latex: sanitize both % and \latexfunction at the same time

After some R computations I obtained a matrix that looks like that:
matrix <- cbind(c(00,01,02),c("some text","random stuff","special characters'"), c("0.12%","\\cellcolor{red!25}3.67%","1.61%"))
I am trying to export it to latex as follow:
file.name <- "file.name"
file.caption <- "file.caption"
print(xtable(matrix, align = c("l","r","r","r"),
label = paste("tab:", file.name, sep = "", collapse = NULL),
caption = file.caption),
type = "latex",
size="\\normalsize",
caption.placement = "top",
# file = paste("graphs/", file.name, ".tex", sep = "", collapse = NULL),
floating = FALSE,
tabular.environment = "longtable",
sanitize.text.function = function(x) x)
If I do not sanitize it, the pdf displays "\cellcolor{red!25}" (and obviously, I would prefer to have the cell colored). If I sanitize I can not typset the file.tex because of the "%".
I tried sanitize.text.function = function(x) x and sanitize.text.function = identity... without success.
Any idea?

How can I render a networkDynamic network using the MDSJ rendering engine in R?

I would like to render a dynamic network in R using the fast MDSJ library. Unfortunately, however, all the vertices' coordinates seem to be 0,0 using this rendering engine, which is not the case when using one of the other layouts (kamadakawai or Graphviz. If you paste the code below, you should be able to reproduce the problem.
if (!require("pacman")) install.packages("pacman")
library("pacman")
pacman::p_load(network, networkDynamic, ndtv)
#animation.mode = "MDSJ"
#animation.mode = "Graphviz"
animation.mode = "kamadakawai"
people <- c("A","B","C","D","E")
documents <- paste0("a",1:10)
edges <- data.frame(from = c("A","A","A","B","B","C","D"),
to = c("a1","a2","a3","a4","a5","a1","a1"),
active = c(1,2,3,3,4,4,4))
net <- network.initialize(0, directed = TRUE, bipartite = length(people))
add.vertices.networkDynamic(net, 5, vertex.pid = people)
add.vertices.networkDynamic(net, 10, vertex.pid = documents)
net %v% "vertex.names" <- c(people, documents)
net %v% "vertex.col" <- c(rep("blue", length(people)), rep("gray", length(documents)))
set.network.attribute(net,'vertex.pid','vertex.names')
add.edges.networkDynamic(net,
tail = get.vertex.id(net, edges[[1]]),
head = get.vertex.id(net, edges[[2]]),
edge.pid = paste0(edges[[1]], "->", edges[[2]]))
activate.edges(net, e = 1:7, at = edges[[3]])
reconcile.vertex.activity(net = net, mode = "encompass.edges", edge.active.default = FALSE)
slice.par <- list(start = 1, end = 4, interval = 1, aggregate.dur = 2, rule = "earliest")
compute.animation(net,
animation.mode = animation.mode,
slice.par = slice.par)
render.d3movie(net,
slice.par = slice.par,
displaylabels = TRUE,
output.mode = "htmlWidget",
vertex.col = 'vertex.col')
Using kamadakawai, one gets a dynamic view like this:
Using MDSJ, all slides look like this:
This code works on my system with MDSJ. Does it install correctly on yours? When it's first used, it has to download and install a Java application mdsj.jar.

PerformanceAnalytics charts.RollingRegression plots initial window values. How do I make it not do that?

I am using the PerformanceAnalytics package to analyze some monthly returns. The charts.RollingRegression should plot the n-month rolling regression against some benchmark.
The data is just 6 returns series from April 08 to December 2014, trying to regress against the SPY.
indexReturns <- read.table("quantIndices.csv", stringsAsFactors = FALSE, sep = ",", fill = TRUE, row.names = 1, header=TRUE)
hfIndexReturns <- read.table("quantHFIndices.csv", stringsAsFactors = FALSE, sep = ",", fill = TRUE, row.names = 1, header=TRUE)
peerReturns <- read.table("quantPeers.csv", stringsAsFactors = FALSE, sep = ",", fill = TRUE, row.names = 1, header=TRUE)
splits <- as.data.frame(strsplit(rownames(indexReturns), "/"))
rownames(indexReturns) <- unname(sapply(splits, function(x) paste0(x[3], "-", x[1], "-", x[2])))
splits <- as.data.frame(strsplit(rownames(peerReturns), "/"))
rownames(peerReturns) <- unname(sapply(splits, function(x) paste0(x[3], "-", x[1], "-", x[2])))
Ret <- xts(peerReturns, order.by = as.Date(row.names(peerReturns)))
Rb <- xts(indexReturns, order.by = as.Date(row.names(indexReturns)))
charts.RollingRegression(Ret, Rb[,2, drop = FALSE], Rf = 0.001, na.pad = TRUE)
This produces the following chart:
I would like it to omit the "meaningless) first 12 months, but there is no documentation on how this is done, and any other depiction of this chart I can find looks like this:
Looking at the source, in the main meat of the function, I see:
for (column.a in 1:columns.a) {
for (column.b in 1:columns.b) {
merged.assets = merge(Ra.excess[, column.a, drop = FALSE],
Rb.excess[, column.b, drop = FALSE])
if (attribute == "Alpha")
column.result = rollapply(na.omit(merged.assets),
width = width, FUN = function(x) lm(x[, 1,
drop = FALSE] ~ x[, 2, drop = FALSE])$coefficients[1],
by = 1, by.column = FALSE, fill = na.pad, align = "right")
if (attribute == "Beta")
column.result = rollapply(na.omit(merged.assets),
width = width, FUN = function(x) lm(x[, 1,
drop = FALSE] ~ x[, 2, drop = FALSE])$coefficients[2],
by = 1, by.column = FALSE, fill = na.pad, align = "right")
if (attribute == "R-Squared")
column.result = rollapply(na.omit(merged.assets),
width = width, FUN = function(x) summary(lm(x[,
1, drop = FALSE] ~ x[, 2, drop = FALSE]))$r.squared,
by = 1, by.column = FALSE, align = "right")
column.result.tmp = xts(column.result)
colnames(column.result.tmp) = paste(columnnames.a[column.a],
columnnames.b[column.b], sep = " to ")
column.result = xts(column.result.tmp, order.by = time(column.result))
if (column.a == 1 & column.b == 1)
Result.calc = column.result
else Result.calc = merge(Result.calc, column.result)
}
}
And we can see there is no na.pad being passed to the final "R-Squared" function, which results in the graph I would expect to see for both the first two charts. I would like to fix this, but I cannot edit the package code. I tried using "assignInNamespace", but it doesn't work. The function seems to work, but the function code does not change in the package. I would also like to remove the leading blank space in the graphs as well, but if you guys could let me know how to edit this, or know any workarounds please let me know. (And thanks! You guys are gods!)
OH! And PS - Why the heck is my version of the package seemingly the only one that has this problem??? Why don't my graphs look right by default?
EDIT: This is not the only piece of code from this package which is suspect. I keep having things break and not work as it seems to be documented (Error in R[, nc] - coredata(Rf) : non-numeric argument to binary operator seems to happen about every other function call.) Anyone have any suggestions for better packages for this type stuff?
The subsetting of the data should be done prior to passing to the charts.RollingRegression function. The mighty xts provides this functionality:
charts.RollingRegression(Ret["2009-04::",], Rb["2009-04::",2, drop = FALSE], Rf = 0.001, na.pad = TRUE)
You can read more about how to subset with xts by looking at the help page in R via ?subset.xts.
Let's break this down a bit:
The charts.RollingRegression is just a wrapper to calculate the rolling beta then plots it.
Here is an example with the rolling alpha and beta:
require(PerformanceAnalytics)
data(managers)
capm_xts = xts(matrix(nrow=nrow(managers),ncol=2),order.by=index(managers))
for(i in 12:nrow(managers)){
capm_xts[i,] = coef(lm(managers[(i-11):i,1]~managers[(i-11):i,4]))
}
colnames(capm_xts) = c('alpha','beta')
chart.TimeSeries(capm_xts[12:nrow(capm_xts),2])

multiple files for reading and saving

I have multiple files to read and also do heatmap after which the outputs will be saved. Somehow, there is a problem(s) with my code below and I can't figure out why it is not working. Files: mxn.dat, scu.dat, emun.dat, ser.dat
files <- list.files(pattern=".dat")
for (i in length(files)){
data <-read.table(files[i],row.names=1,header=T,sep='\t')
for in length(files){
png('i.png')
pheatmap(t(data[i]), cellwidth = 32, cellheight = 14, fontsize = 5, show_colnames = T, cluster_cols = FALSE)
dev.off()
}
}
Any help will be appreciated to get the code working.
Thanks
Rob
Not a reproducible example so I have no way to test if this will work (and somehow I think that this is too localised to be useful to future visitors), but perhaps try this...
files <- list.files(pattern=".dat")
for (i in 1:length(files)){
data <-read.table( files[i],row.names=1,header=T,sep='\t')
png( paste0( i , '.png' ) )
pheatmap( t( data ), cellwidth = 32, cellheight = 14, fontsize = 5, show_colnames = T, cluster_cols = FALSE)
dev.off()
}
Variation of SimonO101's solution:
files <- list.files(pattern=".dat")
for( f in files )
{
data <-read.table( f, row.names = TRUE, header = TRUE, sep = '\t' )
png( gsub( "pdf", "png", f ) )
pheatmap( t( data ), cellwidth = 32, cellheight = 14, fontsize = 5, show_colnames = T, cluster_cols = FALSE)
dev.off()
}
Easier to read (I believe) and has the advantage (?) of preserving the original file names, only changing the extension.

Resources