reticulate
to interact with python
-
Use
reticulate::py_discover_config()
to detect the python used. -
reticulate
needpython
compiled with--enable-shared
flag or spitlibpython
not found error. Usepython3 -c "import sysconfig; print(sysconfig.get_config_vars('Py_ENABLE_SHARED'))"
to find out whether the python support it or not. -
reticulate
only recognizelibpython.so
located in standard position(one ofpython3 -c "from sysconfig import get_config_vars as kv; print('{LIBDIR}/{LDLIBRARY}:{LIBPL}/{LDLIBRARY}'.format_map({k: kv(k)[0] for k in ('LIBDIR', 'LIBPL', 'LDLIBRARY')}))"
). Link the corresponding.so
file to proper position orreticulate
still complainlibpython
not found. -
RStudio Server IDE can not display
matplotlib
image(a bug?) unless import and show it inR
environment for the first time`` `{r PreparePython,fig.show='hide',echo=FALSE,message=FALSE,warning=FALSE} with(list( matplotlib=reticulate::import("matplotlib", convert = TRUE), plt=reticulate::import("matplotlib.pyplot", as="plt") ),{ matplotlib$use("Agg") plt$plot(0, 0) plt$show() }) `` `
Tips
-
read.table读取数据多出一列NA值
采用read.table读取数据时,默认是以’\t’为分割符,当每行最后一个字符不是实际的数据而是’\t’时,不会认为这里有新的一列。而人为指定分割符时,每行最后一个分割符会开启一个新的列,从而引起读入的数据多一列,并且该列的值全部为NA。
-
怎样来计算函数运行使用时间?
使用
system.time()
。proc.time()
可以获得R进程存在的时间,system.time()
通过调用两次proc.time()
来计算函数运行的时间。 -
Why doesn’t R think these numbers are equal?
The only numbers that can be represented exactly in R’s numeric type are integers and fractions whose denominator is a power of 2. Other numbers have to be rounded to (typically) 53 binary digits accuracy. As a result, two floating point numbers will not reliably be equal unless they have been computed by the same algorithm, and not always even then. For example
R> a <- sqrt(2) R> a * a == 2 [1] FALSE R> a * a - 2 [1] 4.440892e-16
The function all.equal() compares two objects using a numeric tolerance of .Machine$double.eps ^ 0.5. If you want much greater accuracy than this you will need to consider error propagation carefully.
For more information, see e.g. David Goldberg (1991), “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys, 23/1, 5–48, also available via http://www.validlab.com/goldberg/paper.pdf.
-
可以通过Sys.getenv()和Sys.setenv()设置当前R的shell环境变量。
-
编写R包的时候, 可以通过在src目录下放置Makevars文件指定特殊的编译选项,比如需要GCC的C++11选项
PKG_CXXFLAGS=-std=c++11
-
reshape2包里面cast函数的公式写法形为
不变列+...~因子转变量列+...
,被转换的value列不会出现在公式中。如data.frame(a,b,c,d)通过公式a+b~c
不改变ab列的情形下将c列的不同元素转换到列上,并以d列数据填充这些新列。 -
运用
identify/locator
读取figure中数据点位置可实现交互式绘图。 -
R官方在Windows和Mac OS X系统中提供了已编译好的二进制包,在
install.packages()
中指定参数type='mac.binary.mavericks'
等即可。 -
采用函数
get(var.name)
可以获取当前环境下var.name
这一名称变量指定对象。这在data.table
等包里是有用的。例如,var.name <- "A", dt <- dt[,B:=get(var.name)]
可以实现获取dt中的”A”这一列的功能。 -
采用
x <- factor(x,levels(x)[new_order])
可以方便地重新排序已经是因子型的变量x
.