tkplot in igraph within R - r

Here is my code with the corresponding output
> tkplot(g.2,vertex.label=nodes,
+ canvas.width=700,
+ canvas.height=700)
[1] 6
> ?tkplot
Warning message:
In rm(list = cmd, envir = .tkplot.env) : object 'tkp.6' not found
I get this error no matter what command I run after building and viewing my plot.
This may be obvious, but I can't get at the data from the plot.
> tkp.6.getcoords
Error: object 'tkp.6.getcoords' not found
Any thoughts? On Windows 2007 Pro.

R is a functional programming language. tkplot is a bit odd (for R users anyway) in that it returns numeric handles to its creations. Try this instead:
tkplot.getcoords(6)
When I run the example on the tkplot page, I then get this from tkplot.getcoords(1) since it was my first igraph plot:
> tkplot.getcoords(1)
[,1] [,2]
[1,] 334.49319 33.82983
[2,] 362.43837 286.10754
[3,] 410.61862 324.98319
[4,] 148.00673 370.91116
[5,] 195.69191 20.00000
[6,] 29.49197 430.00000
[7,] 20.00000 155.05409
[8,] 388.51103 62.61010
[9,] 430.00000 133.44695
[10,] 312.76239 168.90260

Related

R: Using data table inside my own package: Error in lapply(.SD, mean) : object '.SD' not found

Following up on this question, I built my own package, which makes use of data.table.
Inside the package, I use data.table to calculate means per column in a matrix according to another column.
Something in the lines of this:
datam <- cbind(matrix(rexp(100), 10), IN=c(rep(1,2), rep(2,3), rep(3,2), rep(4,1), rep(5,2)))
dd <- data.table::data.table(datam)
result <- dd[, lapply(.SD, mean), by=IN]
The only way I get it to work is including data.table in the Depends: field of my DESCRIPTION file AND import(data.table) in the NAMESPACE file.
Now I want to be able to call the function from my package that uses data.table like this:
mypackage::myfunction(...)
However, I get the following error:
Error in lapply(.SD, mean) : object '.SD' not found
This error only goes away if I call my function after loading the package with library(), like this:
library(mypackage)
myfunction(...)
Since my package would be subsequently called from other packages, is there a way that I can make it work without having to use library() everytime, like I do for every other package I need a function from?
Thanks
EDIT
I have just made a MWE package that reproduces the error. Please download from the Google Drive link below:
https://drive.google.com/open?id=1yHxmQeoIOx9VIuL4EBrFWlGDBstnKJQs
I used the usethis package to build it, in the usethis_myexample.R file.
The package itself is called myexample and is contained in the myexample-package folder. Inside there you can see the DESCRIPTION file contains data.table in the Imports section, and the NAMESPACE file contains import(data.table).
There is only one function named aggregate_mean in the functions.R file inside the R folder.
Next to the myexample-package folder, there is a tests folder with a test file named mytest.R to run the aggregate_mean function like this:
mymat <- cbind(matrix(rexp(100), 10), IN=c(rep(1,2), rep(2,3), rep(3,2), rep(4,1), rep(5,2)))
mymat
mynewmat <- myexample::aggregate_mean(mymat, "IN")
mynewmat
I always encounter the error:
Error in lapply(.SD, mean) : object '.SD' not found
Thanks!
As there is an reproducible exaple now in your question, I was able to dig into it.
I downloaded zip file from your link, unzip it, renamed myexample-package to mypackage. Then...
R CMD build myexample
R CMD INSTALL myexample_0.0.0.9000.tar.gz
R -q
then in R.
mymat <- cbind(matrix(rexp(100), 10), IN=c(rep(1,2), rep(2,3), rep(3,2), rep(4,1), rep(5,2)))
mymat
# [1,] 0.83010264 0.4778802 1.15826121 0.304299143 0.5781483 1.81660550
# [2,] 0.03895798 2.3709480 0.69694839 0.730800823 0.3319984 0.53348461
# [3,] 0.03383199 0.2842029 1.74151827 1.019573035 0.1863635 0.89487309
# [4,] 0.53533254 0.2814782 0.78563371 0.309180422 1.4393098 1.07450638
# [5,] 0.53010142 1.3132409 0.67072292 1.212244007 0.1984360 0.06208641
# [6,] 0.45916016 0.5576434 0.67770401 0.056270575 0.5065829 0.83416626
# [7,] 0.25404953 0.2730706 0.01070633 0.132406274 1.6186573 0.37083771
# [8,] 3.42254715 0.6489950 0.81291881 0.003048744 1.3522848 0.18066361
# [9,] 1.29994319 0.3170614 1.71145805 1.141222719 1.1832478 0.18091907
#[10,] 0.23622615 0.4473482 3.07774816 1.441207092 0.9761152 0.28132707
# IN
# [1,] 6.1868517 2.44880203 0.55261438 0.3459453 1
# [2,] 0.8177218 0.90554629 1.00106158 1.0427756 1
# [3,] 4.3910329 0.56068780 0.24262243 1.7036666 2
# [4,] 0.8712083 0.02439399 0.80927766 1.6596570 2
# [5,] 0.6613734 0.12954737 1.01661648 1.2446795 2
# [6,] 0.2858442 2.32610958 0.26553789 0.4856818 3
# [7,] 3.6628536 0.26447698 0.70633274 2.0283399 3
# [8,] 0.0515666 0.99916985 0.06102821 0.9413485 4
# [9,] 4.7781407 1.47764414 1.92598562 0.4581395 5
#[10,] 0.8770661 2.78552022 0.07543095 0.6886183 5
mynewmat <- myexample::aggregate_mean(mymat, "IN")
mynewmat
# get V1 V2 V3 V4 V5 V6 V7
#1: 1 0.4345303 1.4244141 0.9276048 0.517549983 0.4550734 1.1750451 3.5022868
#2: 2 0.3664220 0.6263073 1.0659583 0.846999155 0.6080364 0.6771553 1.9745382
#3: 3 0.3566048 0.4153570 0.3442052 0.094338425 1.0626201 0.6025020 1.9743489
#4: 4 3.4225471 0.6489950 0.8129188 0.003048744 1.3522848 0.1806636 0.0515666
#5: 5 0.7680847 0.3822048 2.3946031 1.291214905 1.0796815 0.2311231 2.8276034
# V8 V9 V10 IN
#1: 1.6771742 0.77683798 0.6943604 1
#2: 0.2382097 0.68950553 1.5360010 2
#3: 1.2952933 0.48593531 1.2570109 3
#4: 0.9991699 0.06102821 0.9413485 4
#5: 2.1315822 1.00070829 0.5733789 5
So I am not able to reproduce your problem. I encourage you to follow the same steps as described above, to narrow down, if the issue lies somewhere in the way how you install your package.
If you have more followup question, rather than editing question, best to put them in comments under my answer.
Hope that helps!
Add Imports: data.table to your package description and declare .datatable.aware <- TRUE somewhere in an R-File of your package.
That solved the problem for me.

Quanteda with topicmodels: removed stopwords appear in results (Chinese)

My code:
library(quanteda)
library(topicmodels)
# Some raw text as a vector
postText <- c("普京 称 俄罗斯 未 乌克兰 施压 来自 头 条 新闻", "长期 电脑 前进 食 致癌 环球网 报道 乌克兰 学者 认为 电脑 前进 食 会 引发 癌症 等 病症 电磁 辐射 作用 电脑 旁 水 食物 会 逐渐 变质 有害 物质 累积 尽管 人体 短期 内 会 感到 适 会 渐渐 引发 出 癌症 阿尔茨海默 式 症 帕金森 症 等 兔子", "全 木 手表 乌克兰 木匠 瓦列里·达内维奇 木头 制作 手表 共计 154 手工 零部件 唯一 一个 非 木制 零件 金属 弹簧 驱动 指针 运行 其他 零部件 材料 取自 桦树 苹果树 杏树 坚果树 竹子 黄杨树 愈疮木 非洲 红木 总共 耗时 7 打造 手表 不仅 能够 正常 运行 天 时间 误差 保持 5 分钟 之内 ")
# Create a corpus of the posts
postCorpus <- corpus(postText)
# Make a dfm, removing numbers and punctuation
myDocTermMat <- dfm(postCorpus, stem = FALSE, removeNumbers = TRUE, removeTwitter = TRUE, removePunct = TRUE)
# Estimate a LDA Topic Model
if (require(topicmodels)) {
myLDAfit <- LDA(convert(myDocTermMat, to = "topicmodels"), k = 2)
}
terms(myLDAfit, 11)
The code works and I see a result. Here is an example of the output:
Topic 1 Topic 2
[1,] "木" "会"
[2,] "手表" "电脑"
[3,] "零" "乌克兰"
[4,] "部件" "前进"
[5,] "运行" "食"
[6,] "乌克兰" "引发"
[7,] "内" "癌症"
[8,] "全" "等"
[9,] "木匠" "症"
[10,] "瓦" "普"
[11,] "列" "京"
Here is the problem. All of my posts have been segmented (necessary pre-processing step for Chinese) and had stop words removed. Nonetheless, the topic model returns topics containing single-character stop terms that have already been removed. If I open the raw .txt files and do ctrl-f for a given single-character stop word, no results are returned. But those terms show up in the returned topics from the R code, perhaps because the individual characters occur as part of other multi-character words. E.g. 就 is a preposition treated as a stop word, but 成就 means "success."
Related to this, certain terms are split. For example, one of the events I am examining contains references to Russian president Putin ("普京"). In the topic model results, however, I see separate term entries for "普" and "京" and no entries for "普京". (See lines 10 and 11 in output topic 2, compared to the first word in the raw text.)
Is there an additional tokenization step occurring here?
Edit: Modified to make reproducible. For some reason it wouldn't let me post until I also deleted my introductory paragraph.
Here's a workaround, based on using a faster but "dumber" word tokeniser based on space ("\\s") splitting:
# fails
features(dfm(postText, verbose = FALSE))
## [1] "普" "京" "称" "俄罗斯" "未" "乌克兰" "施压" "来自" "头" "条" "新闻"
# works
features(dfm(postText, what = "fasterword", verbose = FALSE))
## [1] "普京" "称" "俄罗斯" "未" "乌克兰" "施压" "来自" "头" "条" "新闻"
So add what = "fasterword" to the dfm() call and you will get this as a result, where Putin ("普京") is not split.
terms(myLDAfit, 11)
## Topic 1 Topic 2
## [1,] "会" "手表"
## [2,] "电脑" "零部件"
## [3,] "乌克兰" "运行"
## [4,] "前进" "乌克兰"
## [5,] "食" "全"
## [6,] "引发" "木"
## [7,] "癌症" "木匠"
## [8,] "等" "瓦列里达内维奇"
## [9,] "症" "木头"
## [10,] "普京" "制作"
## [11,] "称" "共计"
This is an interesting case of where quanteda's default tokeniser, built on the definition of stringi's definition of text boundaries (see stri_split_boundaries, does not work in the default setting. It might after experimentation with locale, but these are not currently options that can be passed to quanteda::tokenize(), which dfm() calls.
Please file this as an issue at https://github.com/kbenoit/quanteda/issues and I'll try to get working on a better solution using the "smarter" word tokeniser.

windRose with Package openair

I am trying to plot Windrose using package openair. I have used a simple code and i am getting the error "Subscript out of bound". I couldn't figure out what the error means. Here, Obs_WR is my data and ws and wd are the column names for wind speed and wind direction respectively.
windRose(Obs_WR, ws="ws", wd="wd")
Error in mydata[[wd]] : subscript out of bounds
A part of dataframe is as follows :
> Obs_WR
ws wd
[1,] 3.715714 0.1250627
[2,] 4.491868 351.4789611
[3,] 5.312253 346.4029396
[4,] 6.047143 349.8645344
[5,] 6.071389 329.2137482
.... ........ ...........
[38,] 16.589769 274.0356269
[39,] 8.065556 273.2977654
[40,] 7.953387 130.6359338

Assigning variable names within a function in R

I am currently working on a dataset in R which is assigned to the global enviroment in R by a function of i, due to the nature of my work I am unable to disclose the dataset so let's use an example.
DATA
[,1] [,2] [,3] [,4] [,5]
[1,] 32320 27442 29275 45921 162306
[2,] 38506 29326 33290 45641 175386
[3,] 42805 30974 33797 47110 198358
[4,] 42107 34690 47224 62893 272305
[5,] 54448 39739 58548 69470 316550
[6,] 53358 48463 63793 79180 372685
Where DATA(i) is a function and the above is an output for a certain i
I want to assign variable names based on i such as:-
names(i)<-c(a(i),b(i),c(i),d(i),e(i))
for argument sake, let's say that the value of names for this specific i is
c("a","b","c","d","e")
I hope that it will produce the following:-
a b c d e
[1,] 32320 27442 29275 45921 162306
[2,] 38506 29326 33290 45641 175386
[3,] 42805 30974 33797 47110 198358
[4,] 42107 34690 47224 62893 272305
[5,] 54448 39739 58548 69470 316550
[6,] 53358 48463 63793 79180 372685
This is the code I currently use:-
VarName<-function(i){
colnames(DATA(i))<<-names(i)
}
However this produces an error message when I run it: "Error in colnames(DATA(i)) <- names(i)) :
target of assignment expands to non-language object" which we can see from my input that isn't true. Is there another way to do this?
Sorry for the basic questions. I'm fairly new to programming.

How to select a value from a table in R

I have the following data, called fit.2.sim:
An object of class "sim"
Slot "coef":
fit.2.sim
[,1] [,2]
[1,] -1.806363 5.148728
[2,] -3.599123 5.183769
[3,] 4.192562 4.855095
[4,] 2.658218 4.967007
[5,] -2.304084 5.220325
[6,] -1.010406 5.071663
[7,] 2.601671 5.129750
[8,] 5.977764 4.757826
[9,] 3.873432 4.932319
[10,] 1.281331 5.138091
Slot "sigma":
[1] 8.285497 10.659971 9.568340 8.649106 8.611894 9.041444 8.316859 7.990499 8.985450
[10] 7.947142
The command I have been using, to no avail unfortunately is:
fit.2.sim$coef[i,j]
i,j being the respective rows and columns. The error I get is:
"Error in fit.2.sim$coef : $ operator not defined for this S4 class"
Could you please tell me if there is another way to make this work?
S4 classes use # not $ to access slots, so you probably wanted
fit.t.sim#coef[i,j]

Resources