Encoding KNIME R-Snippet in pdf export (Special Characters) - r

I have a Encoding Problem in KNIME.
Following code works perfectly in RStudio, the symbol ° is printed out correctly.
library(grid)
library(gridBase)
library(gridExtra)
library(ggplot2)
fn <- "C:/Temp/textR.pdf"
pdf(file=fn)
df <- data.frame("crit °C", 1)
g1 <- tableGrob(format(df, core.just="left"))
grid.arrange(g1, ncol = 1)
dev.off()
I want to use this code in an R Snippet in KNIME, unfortunately it won´t work there, instead of "°" I get "°".
What i already tried:
setting Preferences to UTF-8 in KNIME ->Preferences->General->Workspace
https://tech.knime.org/forum/knime-textprocessing/problems-exporting-utf-8-csv-writer
use ggsave
use Pdf Cairo instead (solution from using Unicode 'dingbat-like' glyphs in R graphics, across devices & platforms, especially PDF) but i am not sure if I had the family package installed...
Can anyone help me? I am using KNIME 3.1.1 and R_3_2_1

It was working well for me (also Windows, in my case Windows 10, 64 bit, English locale, KNIME 3.1.2, R 3.0.3).
You might give a try with the following change:
df <- data.frame("crit \u00B0C", 1)
(\u00B0 stands for the unicode degree sign.)

Related

In R, how do I save a data.tree plot to a file?

I'm unable to save a plot generated by the plot.Node function in data.tree. I've tried the following:
### Create tree object and plot it
data(acme);
plot(acme);
This works fine, showing the plot, as one would expect.
### Try saving it as png
png(filename='file.png', type='cairo-png');
plot(acme);
dev.off();
This creates an empty file. ggsave does the same. Apparantly, plot.Node uses DiagrammeR under the hood, so I looked into that package. It has a function to export graphs:
export_graph(acme, file_name="file.png");
This gives the error:
Error in file.exists(diagram) : invalid 'file' argument
When I transform to GraphViz first, I get a different error:
export_graph(ToGraphViz(acme), file_name="file.png");
Error in graph$dot_code : $ operator is invalid for atomic vectors
Clearly, exporting to GraphViz doesn't quite export to what DiagrammeR expects.
I'm in RStudio and so could in theory just save the plot using the GUI, but I need this for in a script.
Apparently, plot.Node doesn't actually plot anything - instead it seems to generate html/js. Does this mean that that result cannot be stored as a graphic? Or is there some export/conversion function somewhere that I'm completely missing? it certainly feels like I'm missing something obvious - I assume the need to store plotted data.trees as images is quite common. But I have no idea which potential solutions I can still explore.
I'd be very grateful for any pointers anybody has!
As sebastian-c suggested, things work now a bit differently than suggested by Matherion, as of R 3.3.3 with data.tree 0.7.0 and DiagrammeR 0.9.0
Pre-requisite: DiagrmmeRsvg and dependencies need to be installed. Depending on your OS, for this to work you might have to install V8. For example on ubuntu:
apt-get install libv8-3.14-delibv8-3.14-dev
And then in R:
install.packages("DiagrammeRsvg")
On Windows, I didn't have to install anything (maybe because Chrome is installed?).
Once DiagrammeRsvg is available, run:
library(data.tree)
data(acme)
library(DiagrammeR)
export_graph(ToDiagrammeRGraph(acme), "export.pdf")
I've found the answer, at least, partially. There exists a package dedicated to exporting GraphViz diagrams to SVG: DiagrammeRsvg.
So this works:
treeAsSVG <- export_svg(grViz(ToGraphViz(acme)));
writeLines(treeAsSVG, "filename.svg"));
The grViz is necessary to actually convert the ToGRaphViz output to something that can be interpreted by export_svg. I'm still not really sure (yet) what all goes on under the hood - for example, this does not work:
export_graph(grViz(ToGraphViz(acme)), file_name="filename.svg");
But, if anybody else has a similar problem and stumbles upon this question, perhaps this partial answer can help them to at least export something that can then be integrated in e.g. html pages.
By converting acme with as.phylo() it works, but it looks a little boring:
plot(as.phylo(acme),
show.node.label=TRUE,
node.pos=2,
no.margin=TRUE
)
# adding edge labels
edgelabels(as.vector(acme$Get("cost"))[-1],
adj = c(0,-0.5),
frame = "none")
solution with as.phylo()
I also tried as.igraph. However, the nodes overlap and it looks even less pretty:
plot(0, type="n", ann=FALSE, axes=FALSE,
xlim=extendrange(ig[,1]),
ylim=extendrange(ig[,2]))
plot(ig,
layout=layout_as_tree(ig,root=1),
vertex.shape="rectangle",
vertex.size=(strwidth(V(ig)$name) + strwidth("oo")) * 100,
#vertex.size2=strheight("I") * 2 * 100,
edge.label=acme$Get("p",traversal = "level")[-1]
)
solution with as.igraph()

Putting a Cyrillic character in an R plot title: is there a command equivalent to "vfont"?

I would like to add a Cyrillic character to a plot title, but seem to be missing some crucial piece of information. Using "expression(...)" isn't suitable as it doesn't have the characters I require.
I currently have an oldish mac running OS X 10.10.5 with
R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
Platform: x86_64-apple-darwin13.4.0 (64-bit)
I can add Cyrillic to a plot, say
x<-runif(10,0,1); y<-runif(10,0,1); plot(x,y)
# first choose the Hershey font family
par(family="Hershey")
# The fifth table of demo(Hershey) to helps find the three digit code I need
# for my desired symbol, \366 .
# Adding the character to the plot proceeds by using a snippet found
# elsewhere to get an octal code:
code<-366
ocode <- 64*(code%/%100) + 8*(code%/%10)%%10 + code%%10
string <- rawToChar(as.raw(ocode), multiple=TRUE)
# Adding the Cyrillic character to the plot is achieved by choosing the particular
# face in Hershey I require using vfont
text(x=0.5,y=0.5,vfont=c("serif","cyrillic"),labels=paste("Cyrillic sample", string[1] ) )
However, I cannot seem to find an equivalent of "vfont" for the plot title.
I've read many webpages and haven't seen this addressed. It could be that there's something subtle that I haven't understood. Any tips are welcome.
This is a kind of hacky answer (but still an answer) that isn't very general or elegant. As I don't know much about fonts or unicode, this is probably as well as I can do in the short term. I will be very happy for a more general solution from someone who knows about such things.
Changing the font encoding solves the problem in this limited case
Sys.setlocale(category="LC_CTYPE", locale="ru_RU.KOI8-R")
quartz(); plot.new(); plot(z,y)
title(main="testing cyrillic \366")
[![An example plot with Cyrillic character in title][1]][1]
I'm sure there must be better ways of doing this, that don't require so much researching and playing around. Suggestions welcome!

Unicode character with subscript

I want to add a Unicode character which has two letters as subscripts to my plot legend in R.
The character is an r with an accent breve (ř) and the two letters are i and j.
I already looked at this question: Unicode character with superscript and tried to adapt the answers to my problem.
Here is what I tried:
plot(1,pch=NA,ylab="",xlab="",axes=F)
legend("top",legend=paste("1-","\u{0159}"),bty ="n",bg = "white",cex=2)
legend("center",legend=paste("1-","\u{0159}","\u{0069}","\u{006A}"),bty="n",bg = "white",cex=2)
legend("bottomleft",legend=expression("1-"*"\u0159"["\u0069"*"\u006A"]),bty="n",bg = "white",cex=2)
legend("bottomright", legend = quote("1-" *"\u0159"["\u0069"*"\u006A"]),bty="n",bg = "white",cex=2)
The resulting plot can be found below
Both the Unicode letter and the subscript work fine by themselves but not together.
paste() with any combination of [ ] does return an error, but I think this is to be expected as paste can't handle [ ] for subscripts.
The FAQ site on CRAN might give a hint as I am using Windows but I am not sure how to implement this:
3.6 I don't see characters with accents at the R console, for example in ?text.
You need to specify a font in Rconsole (see Q5.2) that supports the encoding in use. This used to be a problem in earlier versions of Windows, but now it is hard to find a font which does not.
Support for these characters within Rterm depends on the environment (the terminal window and shell, including locale and codepage settings) within which it is run as well as the font used by the terminal window. Those are usually on legacy DOS settings and need to altered.
It has to do with the system locale, as seen e.g. if you try
# intToUtf8(345)
# [1] "ř"
# iconv(intToUtf8(345), "utf-8", localeToCharset())
# [1] "r"
This should fix it (I used Czech but other locales will probably work too):
Sys.setlocale("LC_CTYPE", "czech")
# [1] "Czech_Czech Republic.1250"
text(..., labels = quote("\u{0159}"[ij]))

Output from 'choice' in R's kml

I'm having trouble getting 'choice' to create output. When the graphical interface launches, I am selecting a partition with the space bar. This creates a black circle around the partition, indicating it has been selected. When I click 'return', nothing happens.
I checked my working directory to look for the output files, but they are not there. I used getwd() to ensure that I have the correct setwd(). No dice.
There was a similar question posted: Exporting result from kml package in R; however, the answer does not work for me.
Any suggestions? I am using R 3.1.0 GUI Mavericks build(6734) and XQuartz 2.7.6. Thanks for any help getting this working.
Here is my code:
setwd("/Users/eightfrench")
mydata <- read.csv("hcris_long3.csv")
cldHCRIS <- clusterLongData(traj=mydata)
kml(cldHCRIS,nbClusters=2:4,nbRedrawing=2,toPlot="both")
X11(type="Xlib")
choice(cldHCRIS, typeGraph= "bmp")
I had the same problem when using RStudio and i solved it just opening the x11 device with no specification. Like this:
setwd("/Users/eightfrench")
mydata <- read.csv("hcris_long3.csv")
cldHCRIS <- clusterLongData(traj=mydata)
kml(cldHCRIS,nbClusters=2:4,nbRedrawing=2,toPlot="both")
X11()
choice(cldHCRIS, typeGraph= "bmp")
Hope this also works for you.
The new version of kml (2.3) fix that, it provide a key to export data with linux as well (not "return" since getGraphicsEvent does not accept "return" using linux, so I have to map another key : 'm')
Christophe

Importing non-English shapefiles

I'm trying to import a shape-file like this:
fn <- "Proj1"
my_shp <- readShapeSpatial(fn)
On a windows-computer (32-bit) it works ok, but when I do the same from a Ubuntu-machine (64-bit, English OS, R2.14.0), I get "Error in make.names(onames, unique = TRUE) : invalid multibyte string 9".
I suspect it is because the shapefile has Spanish origins, i.e. the names of polygons in it have accents like in "México" (not "Mexico").
As a quick fix, I did the import in windows, saved as .rda and loaded it in Ubuntu, but then I get for example "M\xfexico" as polygon name.
I'm not so experienced in Linux so I don't know if the fix is in R or in Ubuntu. Your help is highly appreciated.
The solution is to start R on the Ubuntu-computer by writing "LC_ALL=C R" in a terminal window. Thanks to Oscar Perpiñán for the solution.
Update: I use RStudio, where as far as I know it is not possible to start R with command-line parameters, but this works from inside RStudio:
Sys.setlocale(category = "LC_ALL", locale = "C")
/Chris

Resources