Android with root Git for version control Lircd with Raspberry Pi for IR receiver and sender Tips for Windows Depolying your own password management tool -- KeeWeb Depoly your flask app into Heroku Fix shit IE code manually ISBN to Book Category by Scraping DangDang A Generic Makefile for C/C++ Program Configure Raspberry pi Remove watermark with PyPDF2 tips for docker Anaconda+TensorFlow+CUDA Snippets Configure Remote Mathematica Kernel Build your own ngrok server Access Array SSL VPN 使用Rstudio制作html5幻灯片 tips for Mac OS X system Tips for ipython notebook 配置Ubuntu server + Openbox (Obuntu) tips for Vimperator tips for Vim 安装CUDA My First Jekyll Blog rsync常见选项 在Linux中读取Ipod touch的文件 tip for texmacs 在VPS上建站的一些tip Gnuplot绘图札记 Samba系统和autofs自动挂载 Linux中alsamixer声卡无法录音 搭建自己的RSS订阅器——Tiny Tiny RSS Grub2引导安装Ubuntu awk tips 将Ubuntu系统装入U盘 The Great Rtorrent 编译GCC 再这样剁手!!!该死的libgd 使用ulimit进行资源限制 使用SSH代理上IPV6 使用RCurl抓取网页数据 修复Ubuntu Grub记 openbox中的文件关联 在Ubuntu 12.04下编译qtiplot 处理BCM4312网卡驱动纪实 配置我的Ubuntu Server记 Cygwin杂记 Linux 使普通用户具有以超级权限执行脚本 让firefox自定义地处理文件类型 WordPress优秀主题及插件 在phpcloud上搭建wordpress UBUNTU下用pptpd做VPN server ubuntu升级内核过后的一些问题 安装telnet服务 kubuntu札记 64位kubuntu札记 统计软件R Virtualbox stardict星际译王 Ubuntu重装windows系统后的grub引导修复 SSH服务及花生壳域名解析 采用cbp2make工具由code::blocks工程创建makefile文件 UBUNTU 札记

tips for R

2015年08月11日

reticulate to interact with python

  • Use reticulate::py_discover_config() to detect the python used.

  • reticulate need python compiled with --enable-shared flag or spit libpython not found error. Use python3 -c "import sysconfig; print(sysconfig.get_config_vars('Py_ENABLE_SHARED'))" to find out whether the python support it or not.

  • reticulate only recognize libpython.so located in standard position(one of python3 -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 or reticulate still complain libpython not found.

  • RStudio Server IDE can not display matplotlib image(a bug?) unless import and show it in R 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

  1. read.table读取数据多出一列NA值

    采用read.table读取数据时,默认是以’\t’为分割符,当每行最后一个字符不是实际的数据而是’\t’时,不会认为这里有新的一列。而人为指定分割符时,每行最后一个分割符会开启一个新的列,从而引起读入的数据多一列,并且该列的值全部为NA。

  2. 怎样来计算函数运行使用时间?

    使用system.time()proc.time()可以获得R进程存在的时间,system.time()通过调用两次proc.time()来计算函数运行的时间。

  3. 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.

  4. 可以通过Sys.getenv()和Sys.setenv()设置当前R的shell环境变量。

  5. 编写R包的时候, 可以通过在src目录下放置Makevars文件指定特殊的编译选项,比如需要GCC的C++11选项

     PKG_CXXFLAGS=-std=c++11
    
  6. reshape2包里面cast函数的公式写法形为不变列+...~因子转变量列+...,被转换的value列不会出现在公式中。如data.frame(a,b,c,d)通过公式a+b~c不改变ab列的情形下将c列的不同元素转换到列上,并以d列数据填充这些新列。

  7. 运用identify/locator读取figure中数据点位置可实现交互式绘图。

  8. R官方在Windows和Mac OS X系统中提供了已编译好的二进制包,在install.packages()中指定参数type='mac.binary.mavericks'等即可。

  9. 采用函数get(var.name)可以获取当前环境下var.name这一名称变量指定对象。这在data.table等包里是有用的。例如,var.name <- "A", dt <- dt[,B:=get(var.name)]可以实现获取dt中的”A”这一列的功能。

  10. 采用x <- factor(x,levels(x)[new_order])可以方便地重新排序已经是因子型的变量x.