搜索此博客

2015年12月31日星期四

ORE 使用说明

大多数Oracle R Enterprise的方法和对象以ore为前缀
,ore表明它存储了对应于Oracle 数据库的元数据。ore.frame为Oracle R Enterprise元数据对象,对应于数据库中的表。
ore.frame类似于R中的数据框。
ore.drop(table="IRIS_TABLE")
iris$id <- 1:150
ore.create(iris,table="IRIS_TABLE")

#加载R data frame到Database
ore.create(data_frame, table="TABLE_NAME")
#将R对象存储到数据库中作为一个临时对象,并返回用于处理的ore对象。
ore.push(data.frame)
#测试是否在ORE schema中存在ore.frame对象。但必须先用 ore.sync()进行同步。
ore.exists()
#删除数据库中的表v。
ore.drop(table="v")
#将数据库中的NARROW表或视图创建为R中名为df_narrow数据框,需要注意数据框的大小,防止超过R的限制。
df_narrow <- ore.pull(NARROW)

#数据库中的表没有定义行的数据,不能完全映射到R中的数据结构。可以在数据库中使用ORDER BY语句。
#row.names提供定义排序,但没有添加索引。可用在单列或多列上, row.names用在未排序的数据,产生错误。
#ORE object has no unique key - using random order
#检索指定的ore.frame对象,代表数据库中的表或视图,在Oracle R Enterprise会话中提供schema。
IRIS_TABLE <- ore.get("IRIS_TABLE")
#将一个无重复无缺失值的变量作为数据框的行名属性
row.names(IRIS_TABLE) <- IRIS_TABLE$id
#查看结果
row.names(head(IRIS_TABLE))
#按照索引列出
IRIS_TABLE[c(1L,2L,3L),]


#将R对象存贮在数据库中,使用当点用户的schema
ore.save(iris,name="iris",append = F)
#加载指定数据库中的R对象
ore.load("iris")
#从用户的schema中删除指定的数据存储
ore.delete("iris")
#列出用户schema中的数据存储和基本描述信息
ore.datastore()
#返回一个描述数据存储的名字和汇总信息的dataframe
ore.datastoreSummary("iris")

ore.sync()
ore.ls()

set.seed(123)
N <- 1000000
mydata <- data.frame(x = rnorm(N, mean = 20, sd = 2),
                     group = sample(letters, N, replace = TRUE,
                              prob = (26:1)/sum(26:1)))
mydata$y <-rbinom(N, 1,
         1/(1+exp(-(.5 - 0.25 * mydata$x + .1 * as.integer(mydata$group)))))
MYDATA <- ore.push(mydata)

rm(mydata)
# Create a function that creates random row indices from large tables
mysampler <- function(n, size, replace = FALSE)
{
  #' Random Whole Number Sampler
  #' @param n number of observations in sample
     #' @param size total number of observations
     #' @param replace indicator for sampling with replacement
     #' @return numeric vector containing the sample indices
     n <- round(n)
     size <- round(size)
     if (n < 0) stop("'n' must be a non-negative number")
     if (size < 1) stop("'size' must be a positive number")
     if (!replace && (n > size))
       stop("'n' cannot exceed 'size' when 'replace = FALSE'")
     if (n == 0)
       numeric()
     else if (replace)
       round(runif(n, min = 0.5, max = size + 0.5))
     else
     {
       maxsamp <- seq(size + 0.5, by = -1, length.out = n)
       samp <- round(runif(n, min = 0.5, max = maxsamp))
       while(length(bump1 <- which(duplicated(samp))))
         samp[bump1] <- samp[bump1] + 1
       samp
     }
}

N <- nrow(MYDATA)
sampleSize <- 500
#简单随机抽样Simple random sampling
srs <- mysampler(sampleSize, N)
simpleRandomSample <- ore.pull(MYDATA[srs, , drop = FALSE])

#系统抽样Systematic sampling
systematic <- round(seq(1, N, length.out = sampleSize))
systematicSample <- ore.pull(MYDATA[systematic, , drop = FALSE])

#分层抽样Stratified sampling
stratifiedSample <-
  do.call(rbind,lapply(split(MYDATA, MYDATA$group),
                 function(y){
                   ny <- nrow(y)
                   y[mysampler(sampleSize * ny/N, ny), , drop = FALSE]
                 }))
#整群抽样Cluster sampling
clusterSample <- do.call(rbind, sample(split(MYDATA, MYDATA$group), 2))

#便利抽样Accidental/Convenience sampling(via row order access)
convenientSample1 <- head(MYDATA, sampleSize)

#便利抽样Accidental/Convenience sampling (via hashing)
maxHash <- 2^32 # maximum allowed in ore.hash
convenient2 <- (ore.hash(rownames(MYDATA), maxHash)/maxHash) <= (sampleSize/N)
convenientSample2 <- ore.pull(MYDATA[convenient2, , drop = FALSE])

#随机分组(Random Partitioning)
#靠给透明层中的ore.frame增加一个分组变量完成
k <- 5
nrowX <- nrow(IRIS_TABLE)
IRIS_TABLE$partition <- sample(rep(1:k, each = nrowX/k, length.out = nrowX), replace =
                        TRUE)

results <- ore.groupApply(IRIS_TABLE, IRIS_TABLE$partition, function(y) {...}, parallel = TRUE)

Error: role "RQADMIN" is required to perform this operation


运行ore.groupApply(IRIS_TABLE, IRIS_TABLE$partition, function(y) {...}, parallel = TRUE)
提示
Error: role "RQADMIN" is required to perform this operation
解决
sqlplus c##RQUSER/rquser@pdborcl
grant RQADMIN to c##rquser;

2015年12月28日星期一

oracle pdb数据库操作

#查看所有的用户
select * from all_users;

#给用户登录权限
grant connect to RQUSER;
grant connect to RQSYS;

#用户解锁
alter user RQSYS account unlock;

#查看当前数据库是否为多租户数据库,v$database的cdb字段显示yes,表明当前数据库为一个CDB数据库:
select name,cdb from v$database;

#查看当前的CDB数据库中pdb的状态:
Select name,con_id,open_mode from v$pdbs;
有mounted和redad wirte两种状态。PDB$SEED,类似于master数据库。
select pdb_id,pdb_name,dbid,status,creation_scn from dba_pdbs;

#修改pdb数据库的状态为READ WRITE
Alter pluggable database PDBORCL open;

#查看当前连接的是cdb还是pdb
select name,open_mode from v$database;

#使用命令show con_name可以查看当前连接的是哪个数据库
show con_name;

#切换cdb和pdb数据库
alter session set container=pdborcl;
show con_name;
alter session set container = cdb$root;

#创建公用用户RQUSER, 需要使用C##或者c##作为该用户名的开头
#在Oracle 12C中,账号分为两种,一种是公用账号,一种是本地账号(亦可理解为私有账号)。共有账号是指在CDB下创建,并在全部PDB中生效的账号,另一种是在PDB中创建的账号。
create user c##RQUSER identified by rquser;
grant dba to c##RQUSER container = all;

公用用户分别登录CDB、PDB看看是否通用
sqlplus c##RQUSER/rquser
sqlplus c##RQUSER/rquser@pdborcl

conn c##RQUSER/rquser;

#添加表和内容
create table test(id number(2),name varchar(20));
insert into test values(1,'xuefliang');
#提交
commit;

#显示当前用户
show user;

#关闭pdb数据库
alter pluggable database pdborcl close;

#打开和关闭所有pdb数据库
alter pluggable database all open;
alter pluggable database all close;

2015年12月23日星期三

Error in if (timezone != "")

ORE连接出现
Error in if (timezone != "") try(.ore.dbGetQuery(sprintf("alter session set time_zone = '%s'",  : 
  missing value where TRUE/FALSE needed
解决办法:
Sys.setenv(TZ = "EDT") 

ORA-21561: OID generation failed

sudo vi /etc/hosts
添加
10.112.6.251  xuefliang

Setting up ORE 1.4.1 with Oracle 12c (12.1.0.1) On Windows Machine.

1、在sqlplus中执行(sqlplus / as sysdba)
Select name,con_id,open_mode from v$pdbs;
#修改 pluggable database status to READ WRITE.
Alter pluggable database PDBORCL open;
Select name,con_id,open_mode from v$pdbs;

2、下载ore-server-win-x86_64-1.4.1.zip和ore-supporting-win-x86_64-1.4.1.zip,解压后,将server、supporting和server.bat复制c:\Windows\system32下,以管理员身份运行server.bat --pdb PDBORCL --perm SYSAUX  --temp TEMP  --rqsys rqsys --user-perm SYSAUX  --user-temp TEMP  --pass rquser  --user RQUSER

3、R
library(ORE)
Sys.setenv(TZ = "EDT")
ore.connect(user="system", service_name="PDBORCL", host="10.112.6.251", password="52332580", port=1521, all=F)
ore.is.connected()
ore.sync()
ore.ls()
ore.exists("MVIEW_FILTER")
ore.get("MVIEW_FILTER")
ore.disconnect()

参考:https://explorebigdataanalytics.wordpress.com/2014/12/16/setting-up-ore-1-4-1-with-oracle-12c-12-1-0-1-r-3-1-2-on-windows-machine/

https://docs.oracle.com/cd/E57012_01/doc.141/e57007/toc.htm

2015年12月21日星期一

Install RODBC with Oracle 12c ODBC in Ubuntu 14.04 64-bit

1、安装 Oracle Instant Cilent

sudo alien -i oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
sudo alien -i oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
sudo alien -i oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
sudo alien -i oracle-instantclient12.1-odbc-12.1.0.2.0-1.x86_64.rpm

2、sudo gedit /etc/ld.so.conf.d/oracle.conf
添加  /usr/lib/oracle/12.1/client64/lib
sudo ldconfig

# 导致不能正常启动
# sudo gedit  /etc/profile.d/oracle.sh
# 添加 export ORACLE_HOME= /usr/lib/oracle/12.1/client64

3、安装 unixodbc
sudo apt-get install unixodc
检查64-bit unixodbc

which isql
file <the output above>

结果类似
ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID

4、配置连接
sudo gedit /etc/odbc.ini
添加
[MYORACLEDSN]
Application Attributes = T
Attributes = W
BatchAutocommitMode = IfAllSuccessful
CloseCursor = F
DisableDPM = F
DisableMTS = T
Driver = Oracle
EXECSchemaOpt =
EXECSyntax = T
Failover = T
FailoverDelay = 10
FailoverRetryCount = 10
FetchBufferSize = 64000
ForceWCHAR = F
Lobs = T
Longs = T
MetadataIdDefault = F
QueryTimeout = T
ResultSets = T
ServerName = //10.112.0.6:1521/orcl
SQLGetData extensions = F
Translation DLL =
Translation Option = 0
UserName = "gssvac"
Password = "gssvac"

sudo gedit  /etc/odbcinst.ini
添加
[Oracle]
Description = Oracle ODBC Connection
Driver = /usr/lib/oracle/12.1/client64/lib/libsqora.so.12.1
Setup =
FileUsage =
CPTimeout =
CPReuse =

5、 测试
isql MYORACLEDSN gssvac gssvac

可能出现问题
ldd /usr/lib/oracle/12.1/client64/lib/libsqora.so.12.1

出现
libodbcinst.so.2 => not found
sudo updatedb
locate libodbcinst
结果
/usr/lib/x86_64-linux-gnu/libodbcinst.so.1
/usr/lib/x86_64-linux-gnu/libodbcinst.so.1.0.0

解决
sudo ln -s /usr/lib/x86_64-linux-gnu/libodbcinst.so.1 /usr/lib/libodbcinst.so.2


library(RODBC)
channel <- odbcConnect("MYORACLEDSN",uid="gssvac",pwd="gssvac")
tb_zyf <- sqlQuery(channel,'SELECT * FROM sys_log')
odbcClose(channel)

2015年12月12日星期六

安装ROracle

一、Linux mint
1、下载instantclient-basic-linux.x64-12.1.0.2.0.zip、instantclient-sdk-linux.x64-12.1.0.2.0.zip和instantclient-sqlplus-linux.x64-12.1.0.2.0.zip

2、
unzip instantclient-basic-linux.x64-12.1.0.2.0.zip
unzip instantclient-sqlplus-linux.x64-12.1.0.2.0.zip
unzip instantclient-sdk-linux.x64-12.1.0.2.0.zip

ln -s libclntsh.so.12.1 libclntsh.so
ln -s libocci.so.12.1 libocci.so

export LD_LIBRARY_PATH=$(pwd)

./sqlplus  #测试


3、R CMD INSTALL --configure-args='--with-oci-inc=/home/xuefliang/instantclient_12_1/sdk/include --with-oci-lib=/home/xuefliang/instantclient_12_1/' /home/xuefliang/ROracle_1.2-1.tar.gz

mkdir rdbms
cd rdbms
ln -s /home/xuefliang/Downloads/instantclient_12_1/sdk/include  public
cd ..
mkdir lib
#复制 instantclient_12_1 下所有文件到 lib目录
cp ./* ./lib

sudo vi /etc/ld.so.conf.d/oracle.conf && sudo chmod o+r /etc/ld.so.conf.d/oracle.conf

add the oracle library path

/usr/lib/oracle/12.1/client64/lib/

add the oracle library path

/home/xuefliang/Downloads/instantclient_12_1/lib

sudo ldconfig

Sys.setenv(ORACLE_HOME='/home/xuefliang/Downloads/instantclient_12_1')

install.packages("ROracle")

方式二:
sudo apt-get install alien

alien -i oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
alien -i oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
alien -i oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm

export LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib/

sudo vi /etc/ld.so.conf.d/oracle.conf && sudo chmod o+r /etc/ld.so.conf.d/oracle.conf

add the oracle library path

/usr/lib/oracle/12.1/client64/lib/

sudo ldconfig

export ORACLE_HOME=/usr/lib/oracle/12.1/client64

sudo ln -s /usr/include/oracle/12.1/client64 $ORACLE_HOME/include

mkdir /usr/lib/oracle/12.1/client64/rdbms

sudo ln -s /usr/include/oracle/12.1/client64 $ORACLE_HOME/rdbms/public

sqlplus  sqlplus64 #测试

R CMD INSTALL --configure-args='--with-oci-inc=/usr/lib/oracle/12.1/client64/include --with-oci-lib=/usr/lib/oracle/12.1/client64/lib' /home/xuefliang/ROracle_1.2-1.tar.gz

Sys.setenv(ORACLE_HOME='/usr/lib/oracle/12.1/client64')
library(ROracle)

二、windows 10(64位)
1、下载并安装Rtools
2、环境变量-系统变量-Path中添加C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin;
3、下载并解压instantclient-basic-windows.x64-12.1.0.2.0.zip、instantclient-sdk-windows.x64-12.1.0.2.0.zip和instantclient-sqlplus-windows.x64-12.1.0.2.0.zip,解压到一个文件夹中instantclient_12_1中。
4、环境变量-系统变量添加OCI_INC C:\instantclient\x64\instantclient_12_1\sdk\include
OCI_LIB64 C:\instantclient\x64\instantclient_12_1\
5、安装DBI包
6、重启后(环境变量生效)
  cmd cd C:\Program Files\R\R-3.2.2\bin\x64

2015年12月11日星期五

R 连接 Oracle

一、windows
1、安装java
2、下载并解压 instantclient-basic-win32-11.2.0.1.0
3、
方式一 RODBC
library(RODBC)
#本地连接
channel <- odbcConnect("DSN_xuefliang",uid="system",pwd="52332580")
tb_zyf <- sqlQuery(channel,'SELECT * FROM t')
odbcClose(channel)

#不需要设置ODBC
dbconnection <- odbcDriverConnect("Driver=Oracle in OraDB12Home1;
                                  Server=127.0.0.1; Database=orcl;
                                  uid=system; pwd=52332580")

#远程连接
#下载instantclient-odbc-windows.x64-12.1.0.2.0.zip
#1、odbc配置TNS service name 10.112.0.6:1521/orcl
channel <- odbcConnect("dsn",uid="gssvac",pwd="gssvac")
tb_zyf <- sqlQuery(channel,'SELECT * FROM sys_log')
odbcClose(channel)


tb_zyf <- sqlQuery(dbconnection,'SELECT * FROM t')
odbcClose(dbconnection)

方式二 RJDBC
Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jre1.8.0_66')
library(RJDBC)
#本地连接
drv <- JDBC("oracle.jdbc.OracleDriver",
            classPath="C:\\app\\Admin\\product\\instantclient_11_2\\ojdbc5.jar", " ")
con <- dbConnect(drv, "jdbc:oracle:thin:@127.0.0.1:1521:orcl", "system", "52332580")
#dbWriteTable(con, "TEST_TABLE", t)
dbGetQuery(con, "select * from T")
dbDisconnect(con)

#远程连接
drv <- JDBC("oracle.jdbc.OracleDriver",
            classPath="C:\\app\\Admin\\product\\instantclient_11_2\\ojdbc5.jar", " ")
con <- dbConnect(drv, "jdbc:oracle:thin:@10.112.0.6:1521:orcl", "gssvac", "gssvac")
log <- dbGetQuery(con, "select * from sys_log")
names(log)
head(log$USER_NAME)
dbDisconnect(con)

方式三 ROracle
library(ROracle)
drv <- dbDriver("Oracle")
connect.string <- paste(
  "(DESCRIPTION=",
  "(ADDRESS=(PROTOCOL=tcp)(HOST=10.112.0.6)(PORT=1521))",
  "(CONNECT_DATA=(SERVICE_NAME=orcl)))", sep = "")
#疫苗
con <- dbConnect(drv, username = "gssvac", password = "gssvac",
                 dbname = connect.string)
depart <- dbGetQuery(con, "select * from sys_log")
dbDisconnect(con)

#金卫信
con <- dbConnect(drv, username = "vaccuser", password = "gscdc20140516",
                 dbname = connect.string)

depart <- dbGetQuery(con, "select * from department")
child <- dbGetQuery(con, "select count(CHIL_NAME) from child")
inoculation <- dbGetQuery(con,"select * from inoculation")
bacterin <- dbGetQuery(con,"select * from bacterin")
dbDisconnect(con)

二、linux
方式一 RJDBC
1、下载instantclient_12_1,并解压。
2、
library(RJDBC)
drv <- JDBC("oracle.jdbc.OracleDriver",
            classPath="/home/xuefliang/Downloads/instantclient_12_1/ojdbc6.jar", " ")
#疫苗
#服务器:管理工具-高级Window防火墙-入站规则-新建规则-端口:1521
con <- dbConnect(drv, "jdbc:oracle:thin:@10.112.0.6:1521:orcl", "gssvac", "gssvac")
log <- dbGetQuery(con, "select * from sys_log")
dbDisconnect(con)

#金卫信
con <- dbConnect(drv, "jdbc:oracle:thin:@10.112.0.6:1521:orcl", "vaccuser", "gscdc20140516")
depart <- dbGetQuery(con, "select * from department")
dbDisconnect(con)

方式二 ROracle
Sys.setenv(ORACLE_HOME='/usr/lib/oracle/12.1/client64')
#疫苗
#select userenv("language") from dual; 查询编码集
Sys.setenv(NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK")
library(ROracle)
drv <- dbDriver("Oracle")
connect.string <- paste(
  "(DESCRIPTION=",
  "(ADDRESS=(PROTOCOL=tcp)(HOST=10.112.0.6)(PORT=1521))",
  "(CONNECT_DATA=(SERVICE_NAME=orcl)))", sep = "")
con <- dbConnect(drv, username = "gssvac", password = "gssvac",
                 dbname = connect.string)

log <- dbGetQuery(con, "select * from sys_log")
log$USER_NAME <- iconv(log$USER_NAME,from="GBK",to="UTF-8")
log$REGION_NAME <- iconv(log$REGION_NAME,from="GBK",to="UTF-8")
dbDisconnect(con)

#金卫信
Sys.setenv(NLS_LANG="AMERICAN_AMERICAN.ZHS16GBK")
library(ROracle)
drv <- dbDriver("Oracle")
connect.string <- paste(
  "(DESCRIPTION=",
  "(ADDRESS=(PROTOCOL=tcp)(HOST=10.112.0.6)(PORT=1521))",
  "(CONNECT_DATA=(SERVICE_NAME=orcl)))", sep = "")
con <- dbConnect(drv, username = "vaccuser", password = "gscdc20140516",
                 dbname = connect.string)

depart <- dbGetQuery(con, "select * from department")
depart$DEPA_NAME <- iconv(depart$DEPA_NAME,from="GBK",to="UTF-8")
dbDisconnect(con)

2015年12月9日星期三

ubuntu install ibus-libpinyin

1、sudo apt-get install libglib2.0-dev libdb-dev libgtk-3-dev gnome-common libibus-1.0-dev sqlite3
2、下载libpinyin-1.3.0.tar.gz( git clone https://github.com/libpinyin/libpinyin)
./autogen.sh
make
sudo make install
3、下载ibus-libpinyin-1.7.3( git clone https://github.com/libpinyin/ibus-libpinyin)
./autogen.sh
make
sudo make install

4、 sudo cp /usr/local/share/ibus/component/libpinyin.xml /usr/share/ibus/component/

2015年12月1日星期二

Rstudio 使用代理

启动Rstudio 后运行
Sys.setenv(http_proxy="http://127.0.0.1:1080")
Sys.getenv("http_proxy")
成功

getwd()获得工作目录,在此工作目录下有.Renviron文件,修改文件内容为:
http_proxy="http://127.0.0.1:1080"
在Rstudio启动后,可以用Sys.getenv("http_proxy")进行测试。

1、在VPS上安装OpenConnect server(参考http://www.fanyueciyuan.info/fq/ocserv-debian.html)
2、PC上安装Cisco AnyConnect Secure Mobility VPN(下载地址:http://helpdesk.ugent.be/vpn/en/asa.php)

sudo apt-get install network-manager-openconnect
sudo bash vpn_install.sh

3、使用144.168.56.190:999地址,用户名和密码进行连接。实现全局代理。 
测试通过。



ubuntu 安装 privoxy

1、安装
sudo proxychains apt-get install privoxy

2、配置 privoxy
sudo gedit /etc/privoxy/config。
编辑它增加一行:
forward-socks5 / 127.0.0.1:1080 .

forward-socks5代表转发到socks5代理,/代表所有的URL都转发(也可以在这里写url patten),127.0.0.1:1080是socks代理的位置,最后的一点.代表没有http代理
privoxy 的监听端口号,默认是 8118,默认绑定的地址是 127.0.0.1(这个地址代表“当前系统”)。由于默认是绑定在 127.0.0.1 这个地址,所以只有当前系统的软件才可以连接到 Privoxy 的监听端口。
修改 privoxy 的监听端口和地址
在尾部增加如下一行
listen-address 0.0.0.0:8118

重启  sudo /etc/init.d/privoxy restart

3、使用:127.0.0.1:8118


2015年11月17日星期二

Git SSH Key 生成

ssh-keygen -t rsa -C "xuefliang@gmail.com"
按3个回车,密码为空。

 添加 ~/.ssh id_rsa.pub中的内容

debian or ubuntu install chromium


sudo apt-get install chromium chromium-l10n  #debian

sudo apt-get install chromium-browser  #ubuntu

 

2015年11月13日星期五

sudo免密码

sudo vim /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root ALL=(ALL:ALL) ALL
xuefliang ALL=(ALL)  ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
xuefliang ALL=(ALL) NOPASSWD:ALL

# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
xuefliang ALL=(ALL) NOPASSWD:ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

2015年11月10日星期二

ubuntu install sublime text 3


wget http://c758482.r82.cf2.rackcdn.com/sublime-text_build-3083_amd64.deb
# http://docs.sublimetext.info/en/latest/getting_started/install.html#ubuntu
dpkg -i sudo dpkg -i /home/xuefliang/sublime-text_build-3083_amd64.deb 
Sublime Text 3.x
—– BEGIN LICENSE —–
Andrew Weber
Single User License
EA7E-855605
813A03DD 5E4AD9E6 6C0EEB94 BC99798F
942194A6 02396E98 E62C9979 4BB979FE
91424C9D A45400BF F6747D88 2FB88078
90F5CC94 1CDC92DC 8457107A F151657B
1D22E383 A997F016 42397640 33F41CFC
E1D0AE85 A0BBD039 0E9C8D55 E1B89D5D
5CDB7036 E56DE1C0 EFCC0840 650CD3A6
B98FC99C 8FAC73EE D2B95564 DF450523
—— END LICENSE ——
—– BEGIN LICENSE —–
K-20
Single User License
EA7E-940129
3A099EC1 C0B5C7C5 33EBF0CF BE82FE3B
EAC2164A 4F8EC954 4E87F1E5 7E4E85D6
C5605DE6 DAB003B4 D60CA4D0 77CB1533
3C47F579 FB3E8476 EB3AA9A7 68C43CD9
8C60B563 80FE367D 8CAD14B3 54FB7A9F
4123FFC4 D63312BA 141AF702 F6BBA254
B094B9C0 FAA4B04C 06CC9AFC FD412671
82E3AEE0 0F0FAAA7 8FA773C9 383A9E18
—— END LICENSE ——
—– BEGIN LICENSE —–
J2TeaM
2 User License
EA7E-940282
45CB0D8F 09100037 7D1056EB A1DDC1A2
39C102C5 DF8D0BF0 FC3B1A94 4F2892B4
0AEE61BA 65758D3B 2EED551F A3E3478C
C1C0E04E CA4E4541 1FC1A2C1 3F5FB6DB
CFDA1551 51B05B5D 2D3C8CFE FA8B4285
051750E3 22D1422A 7AE3A8A1 3B4188AC
346372DA 37AA8ABA 6EB30E41 781BC81F
B5CA66E3 A09DBD3A 3FE85BBD 69893DBD
—— END LICENSE ——
install package control
 Ctrl+`
import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
usage
  1. Press Command-Shift-P (Mac OS X) or Ctrl-Shift-P (Windows) to open the Command Palette.
  2. Start typing Package Control until you see the appropriate commands.



2015年10月11日星期日

安装tor浏览器

sudo add-apt-repository ppa:webupd8team/tor-browser
sudo apt-get update
sudo apt-get install tor-browser

2015年10月3日星期六

2015年9月17日星期四

为 Git 设置SSH代理


修改 ssh 的配置文件 .ssh/config

vim .ssh/config

Host github.com
ProxyCommand nc -X 5 -x 127.0.0.2:1080 %h %p

这样链接到 github.com 的 ssh 都会使用 socks5://127.0.0.2:1080 的代理,ssh 的配置可以参考 man ssh_config ,nc 命令的使用可以参考 man nc

2015年9月15日星期二

VPS部署 DNS 服务

apt-get install dnsmasq dnsutils ufw

vim /etc/dnsmasq.conf
   增加
port=5353
server=8.8.8.8 
server=8.8.4.4

容许5353端口
ufw allow 5353
 
重启dnsmasq:
 
service dnsmasq start
/etc/init.d/dnsmasq restart
 
测试
vps 
dig @127.0.0.1 -p 5353 www.youtube.com
 
dig @144.168.56.190 -p 5353 www.youtube.com
 
 
 
 
 

2015年9月14日星期一

teamviewer 安装

sudo apt-get install lib32z1 libc6-i386
下载:http://download.teamviewer.com/download/teamviewer_amd64.deb
sudo dpkg -i /home/xuefliang/Downloads/teamviewer_10.0.46203_amd64.deb

2015年9月8日星期二

linux shadowsocks客户端安装

sudo add-apt-repository ppa:hzwhuang/ss-qt5
sudo apt-get update
sudo apt-get install shadowsocks-qt5

2015年8月29日星期六

python 环境搭建

sudo apt-get install python python-dev python-pip python-setuptools

#Install Oracle Java JDK/JRE 8
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
java -version
sudo apt-get install oracle-java8-set-default

#Install PyCharm
sudo apt-add-repository ppa:mystic-mirage/pycharm
sudo apt-get update
sudo apt-get install pycharm
or
sudo apt-get install pycharm-community

用户名:yueting3527
注册码:
===== LICENSE BEGIN =====
93347-12042010
00001FMHemWIs"6wozMZnat3IgXKXJ
2!nV2I6kSO48hgGLa9JNgjQ5oKz1Us
FFR8k"nGzJHzjQT6IBG!1fbQZn9!Vi
===== LICENSE END =====

2015年8月15日星期六

Dot Density Maps in R

city = readShapePoly("/home/xuefliang/RInMedicine/city/city_region.shp") 
gpclibPermit()  #install.packages("gpclib", type = "source")
tract <- fortify(city,region="CNTY_CODE")

city@data$USE_CODE8 <- sample(1000,14)

dots.rand <- dotsInPolys(city, as.integer(city@data$USE_CODE8))
#table(iconv(city$NAME, from = "GBK"))
dots <- data.frame(coordinates(dots.rand)[,1:2])

ggplot(tract, aes(x = long, y = lat)) +
  geom_polygon(aes(group = group), size=0.2, fill = "white") +
  coord_equal()+geom_point(data=dots, aes(x=x,y=y), size=0.8,colour="red")

ggmap Overlay shapefile with filled polygon of regions

city = readShapePoly("/home/xuefliang/RInMedicine/city/city_region.shp") 
gpclibPermit()  #install.packages("gpclib", type = "source")
tract <- fortify(city,region="CNTY_CODE")

gansu <- get_map(location = 'gansu', zoom = 5,maptype = 'roadmap')
ggmap(gansu)+
  geom_polygon(data = tract, aes(x = long, y = lat, group = group), colour = "black",fill='grey' ,alpha = 0.2) +
  theme_nothing(legend = TRUE)+
  coord_cartesian(xlim=c(90, 110), ylim=c(32, 43))

2015年8月13日星期四

Dot Density Electoral Map

doInstall <- TRUE
toInstall <- c("XML", "maps", "ggplot2", "sp","RCurl","httr","plyr")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)

myURL <- "https://en.wikipedia.org/wiki/United_States_presidential_election,_2012"
tabs <- getURL(myURL)
allTables <- readHTMLTable(tabs)

#library(httr)
#tabs <- GET(myURL)
#allTables <- readHTMLTable(rawToChar(tabs$content), stringsAsFactors = F)
#stateTable = readHTMLTable(readLines(myURL, encoding = "UTF-8"), which = 14, header = T)

#str(allTables)  # Look at the allTables object to find the specific table we want
stateTable <- allTables[[14]]  # We want the 14th table in the list (maybe 13th?)
#head(stateTable)

# Clean up:
stateTable <- stateTable[2:(nrow(stateTable)-1), ]  # Drop summary lines
stateTable <- rename(stateTable,c("V1"="State","V3"="Obama","V6"="Romney","V20"="Total"))
stateTable$State <- do.call(rbind, strsplit(as.character(stateTable$State), "\\["))[, 1]
stateTable$State[stateTable$State == "District of ColumbiaDistrict of Columbia"] <- "District of Columbia"
stateTable$State <-gsub(",", "",stateTable$State)
whichAreNumeric <- colMeans(apply(stateTable, 2, function(cc){
  regexpr(",", cc) != -1})) > 0  #判断是否为数字列
stateTable[, whichAreNumeric] <- apply(stateTable[, whichAreNumeric], 2, function(cc){
  as.numeric(gsub(",", "", cc))})  #删除数字列中的逗号

new_theme_empty <- theme_bw()  # Create our own, mostly blank, theme
new_theme_empty$line <- element_blank()
new_theme_empty$rect <- element_blank()
new_theme_empty$strip.text <- element_blank()
new_theme_empty$axis.text <- element_blank()
#new_theme_empty$axis.title <- element_blank()
new_theme_empty$plot.margin <- structure(c(0, 0, -1, -1), unit = "lines",
                                         valid.unit = 3L, class = "unit")

stateShapes <- map("state", plot = FALSE, fill = TRUE)
stateShapes <- fortify(stateShapes)  # 转为数据框

### New stuff begins here ###
pointCollector <- list()
perNCapita <- 1000

for(ss in stateTable$State){
  print(ss)
  stateShapeFrame <- stateShapes[stateShapes$region == tolower(ss), ]
  if(nrow(stateShapeFrame) < 1){next()}
  statePoly <- Polygons(lapply(split(stateShapeFrame[, c("long", "lat")],
                                     stateShapeFrame$group), Polygon), ID = "b")
  nDems <- ceiling(stateTable[stateTable$State == ss, "Obama"] / perNCapita)
  nReps <- ceiling(stateTable[stateTable$State == ss, "Romney"] / perNCapita)
  nOther <- ceiling(with(stateTable[stateTable$State == ss, ],
                         Total - Romney - Obama) / perNCapita)
 
  pDems <- data.frame(spsample(statePoly, nDems, type = "random")@coords,
                      Vote = "Obama") #空间数据抽样,样本数nDems,抽样方法random,regular,stratified,nonaliged,hexagonal,clustered,Fibonacci
  pReps <- data.frame(spsample(statePoly, nReps, type = "random")@coords,
                      Vote = "Romney")
  if(nOther < 1){
    pOther <- data.frame(x = NULL, y = NULL, Vote = NULL)
  } else {
    pOther <- data.frame(spsample(statePoly, nOther, type = "random")@coords,
                         Vote = "Other")
  }
  allPoints <- data.frame(State = ss, rbind(pDems, pReps, pOther))
  pointCollector[[ss]] <- allPoints
}

pointFrame <- do.call(rbind, pointCollector)
# Randomize, so we don't overplot "Other" on top of "Romney" on top of "Obama."
pointFrame <- pointFrame[sample(1:nrow(pointFrame), nrow(pointFrame)), ]
#head(pointFrame)

mapPlot <- ggplot(stateShapes)
mapPlot <- mapPlot + geom_point(data = pointFrame,
                                aes(x = x, y = y, colour = Vote),
                                shape = ".",
                                alpha = 1/2)
mapPlot <- mapPlot + geom_polygon(aes(x = long, y = lat, group = group),
                                  colour = "BLACK", fill = "transparent")
mapPlot <- mapPlot + coord_map(project="conic", lat0 = 30)
mapPlot <- mapPlot + new_theme_empty
mapPlot <- mapPlot + scale_colour_manual(values = c("blue", "red", "green"))
mapPlot <- mapPlot + ggtitle("2012 Election Returns by State")
mapPlot <- mapPlot + ylab("")
mapPlot <- mapPlot + xlab(paste("Each dot represents ",
                                perNCapita, " voters.", sep = ""))
mapPlot <- mapPlot + guides(colour = guide_legend(override.aes =
                                                    list(shape = 19, alpha = 1)))
print(mapPlot)


# 读取地理信息数据
city = readShapePoly("/home/xuefliang/RInMedicine/city/city_region.shp") 
# 将数据转为数据框
gpclibPermit()  #install.packages("gpclib", type = "source")
tract <- fortify(city,region="CNTY_CODE")
tract$group <- tract$id

#发病数据
data <- read.csv("/home/xuefliang/RInMedicine/city/data.csv", stringsAsFactors = FALSE)
data$id <- as.character(data$id)
data$A <- round(data$rand*1000)
data$B <- round(data$rand*100)

plotData <- left_join(tract, data)


pointCollector <- list()

for(ss in tract$id){
  stateShapeFrame <- tract[tract$id == ss, ]
  if(nrow(stateShapeFrame) < 1){next()}
  statePoly <- Polygons(lapply(split(stateShapeFrame[, c("long", "lat")],
                                     stateShapeFrame$group), Polygon), ID = "b")
  nA <- ceiling(data[data$id == ss, "A"])
  nB <- ceiling(data[data$id == ss, "B"])

  pA <- data.frame(spsample(statePoly, nA, type = "random")@coords,
                      Vote = "A") #空间数据抽样,样本数nDems,抽样方法random,regular,stratified,nonaliged,hexagonal,clustered,Fibonacci
  pB <- data.frame(spsample(statePoly, nB, type = "random")@coords,
                      Vote = "B")
 
  allPoints <- data.frame(State = ss, rbind(pA, pB))
  pointCollector[[ss]] <- allPoints
}

stateShapeFrame <- tract[tract$id== "62010000", ]
if(nrow(stateShapeFrame) < 1){next()}
statePoly <- Polygons(lapply(split(stateShapeFrame[, c("long", "lat")],
                                   stateShapeFrame$group), Polygon), ID = "b")
pDems <- data.frame(spsample(statePoly, nA, type = "random")@coords,
                    Vote = "Obama")

2015年7月15日星期三

向量的内积和距离


向量的内积(点积)



sum(a*b)
或者
t(a)%*%b


向量的距离

安装Rtreemix

source("http://bioconductor.org/biocLite.R")
biocLite("Rtreemix")
L1.dist计算L1距离,cosin.dist计算余弦距离,euclidian.dist计算欧氏距离,rank.cor.dist计算排名相关距离。向量必须有相同的长度。当使用rank.cor.dist向量必须有长度大于4

L1.dist(p, q)
cosin.dist(p, q)
euclidian.dist(x, y)
rank.cor.dist(x, y)


对称正交化


正交矩阵 orthogonal matrix: 正交矩阵P是一个方块矩阵,该矩阵的行和列皆为正交的单位向量。
正交矩阵满足下列性质:P^TP=PP^T=I和P^T=P^{-1}

对称矩阵 symmetric matrix:对称矩阵A是一个方块矩阵,其转置和自身等价,即 A^T=A

对角矩阵 diagonal matrix:主对角线之外的元素皆为0的矩阵,对角线上的元素可以为0,也可以不为0。

正交对角化: 矩阵被称为可正交对角化orthogonally diagonalizable,当且仅当一个正交矩阵P使得P^TAP=D, D是一个对角阵。

特征值 eigenvalue 和特征向量 eigenvectors:当方阵A与一个非零向量v相乘等于一个常数 乘以该向量时,v被称为A的特征向量, 被称为特征值,即Av=v。

AA <- matrix(c(7, 0, 9, 0, 2, 0, 9, 0, 7), ncol = 3)
eigen.A <- eigen(AA)  #get eigenvalues and eigen vectors 
lam <- eigen.A$values  #特征值默认以降序排列
vecs <- eigen.A$vectors  
    #特征向量对应于降序排列的特征值,如果想改变最后的对角阵,需要改变特征向量的顺序
DD <- round(t(vecs) %*% AA %*% vecs, 2)
print(DD)

2015年6月24日星期三

R 返回包含“社区卫生服务中心”数量


men <- read.csv("/home/xuefliang/Downloads/men.csv",header = F)
b <- grep("社区卫生服务中心",men$V1)
length(b)
a <- grepl("社区卫生服务中心",men$V1)

#grep仅返回匹配项的下标,而grepl返回所有的查询结果,并用逻辑向量表示有没有找到匹配。windows csv文件ANSI编码需用记事本修改为UTF-8

2015年5月28日星期四

R on vps tips Setting LC_COLLATE failed, using "C"

 sudo apt-get install language-pack-en-base
 sudo dpkg-reconfigure locales
 
 错误提示 “Unable to connect to service
 adduser xuefliang  #非root用户组
 
vi  /etc/rstudio/rserver.conf 
www-port = 8787
rsession-which-r=/usr/bin/R 

用rstudio-server verify-installation进行验证
 
 

locale::facet::_S_create_c_locale name not valid解决方案  

export LC_ALL="C"

 

2015年5月25日星期一

R计算排列和组合

library(combinat)
permn(3)
length(permn(3))

combn(1000, 2)
dim(combn(1000,2))[2]

组合(Combinations)和排列(Permutation)计算公式为
排列组合公式/排列组合计算公式

perm = function(n, x) {
  return(factorial(n) / factorial(n-x))
}

comb = function(n, x) {
  return(factorial(n) / (factorial(x) * factorial(n-x)))
}

组合数:choose(n, k)
注意:从n个中选出k个
阶乘(k!):factorial(k)
排列数:choose(n, k) * factorial(k)


2015年5月23日星期六

R 语言求极限

library(rSymPy)

sympy("limit(1/x, x, oo)")
sympy("limit(1/x, x, 0)")
sympy("limit(x*log(x), x, 0)") 
sympy("limit(sin(x)/x*x, x, 0)")
sympy("limit(exp(x)/x*x, x, 0)")
sympy("limit(x**x, x, 0)")
sympy("limit(x*exp(1)**-x, x, oo)")  ##**代表幂
sympy("limit((cos(x)-1)/x**2, x, 0)")

2015年4月26日星期日

ubuntu下安装goldendict及离线词库

1.sudo apt-get install goldendict

2.播放语音
sudo apt-get install mplayer

sudo apt-get install ubuntu-restricted-addons

sudo apt-get install ubuntu-restricted-extras

3.批量解压离线词库

#! /bin/bash
# run this script to install goldendict and off-line dictionaries
# Only for Ubuntu
# created by longbin <beangr@163.com>
# 2014-03-28


function goldendict_essential_install(){
    for file in ${FILE_LIST}
    do
        trap 'echo -e "\nInterrupted by user"; exit' INT
        echo -e "\n======================================="
        echo -e "Preparing to install ${file} ..."
        echo -e "======================================="
        echo -e "\tsudo apt-get install ${file}"
        sudo apt-get install ${file}
    done

    RET_VAL=$(which goldendict)
    if [[ "${RET_VAL}" == "" ]] ;then
        echo "goldendict installed error."
        exit
    else
        echo "goldendict installed successfully."
    fi
}

function select_offline_dict_dir(){
    read -p "    Press <Enter> to install off-line dictionaries. "
    if [[ -d "${OFFLINE_DICTS_DIR}" ]] ;then
        return
    fi

    DIR_LIST=$(find . -maxdepth 3 -type d | sed '/^\.$/d' | sed 's#\./##')
    if [[ "${DIR_LIST}" == "" ]] ;then
        echo "Off-line dictionary directory not exists."
        exit
    fi

    PS3="Please select your goldendict off-line dictionary's dir: "
    select option in ${DIR_LIST}
    do
        if [[ -d "${option}" ]] ;then
            OFFLINE_DICTS_DIR=${option}
            break
        fi
    done
}

function goldendict_offline_dict_setup(){
    select_offline_dict_dir
    GOLDENDICT_OFFLINE_DICT_DIR=/usr/share/goldendict-wordnet/dic
    if ! [[ -d "${GOLDENDICT_OFFLINE_DICT_DIR}" ]] ;then
        sudo mkdir -p ${GOLDENDICT_OFFLINE_DICT_DIR}
    fi

    if [[ -d ${OFFLINE_DICTS_DIR} ]] ; then
        pushd ${OFFLINE_DICTS_DIR}
        ###########################
        TGZ_FILES=$(ls |grep tgz)
        if [[ "${TGZ_FILES}" == "" ]] ;then
            echo "None tgz file exists."
            exit
        fi
        for file in ${TGZ_FILES}
        do
            if [[ -d "${GOLDENDICT_OFFLINE_DICT_DIR}/${file%.tgz}" ]] ;then
                echo "directory ${GOLDENDICT_OFFLINE_DICT_DIR}/${file%.tgz} has already exist."
                sudo rm -rf ${GOLDENDICT_OFFLINE_DICT_DIR}/${file%.tgz}
            fi
            echo "tar -zxvf ${file} -C ${GOLDENDICT_OFFLINE_DICT_DIR}"
            sudo tar -zxvf ${file} -C ${GOLDENDICT_OFFLINE_DICT_DIR}
        done
        ##########################
        popd
    else
        echo "Off-line dictionary directory \"${OFFLINE_DICTS_DIR}\" not exists."
        echo 'Please download dictionaries to \"${OFFLINE_DICTS_DIR}\" '
    fi
}

function install_mplayer_mutiple_support(){
    for file in ${MULTIPLE_SUPPORT} ${MULTIPLE_SUPPORT}
    do
        echo -e "\n======================================="
        echo -e "Preparing to install ${file} ..."
        echo -e "======================================="
        echo -e "\tsudo apt-get install ${file} ..."
        sudo apt-get install ${file}
    done
}

echo "Preparing to install goldendict and off-line dictionaries"

goldendict_essential_install
goldendict_offline_dict_setup




4.配置
Preferences  Files  词库位置 /usr/share/goldendict-wordnet/dic
Preferences Audio  Playback  mplayer

5.在线词典

http://www.iciba.com/%GDWORD%/

去在线词典广告,本地新建 dict.html

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <style>
        iframe{ width: 706px; height: 650px; margin-top:-190px; margin-left:-120px;
        }
    </style>
</head>

<body>
    <iframe id="a" frameborder="0">
    </iframe>
    <script>
    var word = location.href.slice(location.href.indexOf('?a')+3);
    document.getElementById('a').setAttribute(
        'src',
        'http://dict.youdao.com/search?le=eng&q=' + word + '&keyfrom=dict.top');
    </script>
</body>

网站  加入本地地址file:///home/xuefliang/Documents/goldendict_for_ubuntu/dict.html?a=%GDWORD%

R 3.1 -> 3.2 upgrade notes

1.cp -r ~/R/x86_64-pc-linux-gnu-library/3.1/* ~/R/x86_64-pc-linux-gnu-library/3.2 # at the shell prompt
2.update.packages(checkBuilt=TRUE, ask=FALSE) #at the R prompt

R语言的导数计算

求导通用法则
(u/v)'=(u'v-uv')/v^2

(uv)'=u'v+uv'

(cu)'=cu'

(u+v)'=u'+v'

dy/dt=dy/dx*dx/dt

D_{n}x_{n}=n!

一元初等函数的导数计算公式 

函数          原函数            导函数
常数函数      y=C               y'=0
幂函数        y=x^n             y'=n*x^(n-1)
指数函数      y=a^x             y'=a^x*ln(a)
              y=exp(1)^x        y'=exp(1)^x
对数函数      y=log(x,base=a)   y'=1/(x*ln(a)) (a>0,且a!=1,x>0)
              y=ln(x)           y'=1/x
正弦函数      y=sin(x)          y'=cos(x)
余弦函数      y=cos(x)          y'=-sin(x)
正切函数      y=tan(x)          y'=sec(x)^2=1/cos(x)^2
余切函数      y=cot(x)          y'=-csc(x)^2=1/sin(x)^2
正割函数      y=sec(x)          y'=sec(x)*tan(x)
余割函数      y=csc(x)          y'=-csc(x)*cot(x)
反正弦函数    y=arcsin(x)       y'=1/sqrt(1-x^2)
反余弦函数    y=arccos(x)       y'=-1/sqrt(1-x^2)
反正切函数    y=arctan(x)       y'=1/(1+x^2)
反余切函数    y=arccot(x)       y'=-1/(1+x^2)
反正割函数    y=arcsec(x)       y'=1/abs(x)*(x^2-1)
反余割函数    y=arccsc(x)       y'=-1/abs(x)*(x^2-1)



1、stats包
dx <- deriv(y~x^3,"x")
x<-1:2  #给自变量x赋值
eval(dx) #运行求导计算

deriv(expr, name)函数时通常要传2个参数,第一参数expr就是原函数公式,用~号来分隔公式的两边,第二参数name用于指定函数的自变量。deriv()函数会返回一个表达式expression类型变量,再用eval()函数运行这个表达式得到就可得到计算结果。希望以函数的形式调用计算公式,那么你还需要传第三个参数func,并让func参数为TRUE

dx <- deriv(y~(sin(x))^10,"x",func=T)  # 生成导数公式的调用函数
dx(4)   # 以参数作为自变量,进行函数调用

dx<-deriv3(y~A*x^2,"x",func = TRUE) #二阶导数

2、mosaic包
library(mosaic)
D(sin(x) ~ x)  #sinx求导,D()屏蔽了stats包中的D()
D(A * x^2~ x + x) #A*x的2次方的二阶导数
y <- D((sin(x))^10 ~ x) # (sinx)10次方求导
y(4)

单变量定积分

stats包
integrate(dnorm, -1.96, 1.96)
integrate(dnorm, -Inf, Inf)

## 自定义函数
integrand <- function(x) {1/((x+1)*sqrt(x))}
integrate(integrand, lower = 0, upper = Inf)



2015年4月14日星期二

formatR使用

1 建立R脚本文件
 demo.r
a<-1+1;a;matrix(rnorm(10),5);
if(a>2) { b=c('11',832);"#a>2";} else print('a is invalid!!')

x = "demo.r"

tidy_source(x)

2 GUI 工具
formatR::tidy_app()

2015年4月12日星期日

如何用R来处理图片

1 biOps安装
sudo apt-get install libfftw3-dev libtiff5-dev
devtools::install_github("cran/biOps")

2 使用
library(biOps)
# 读取本地的图像文件,观察到此图像是320*320像素的rgb图片
x <-  readJpeg("d:\\xccds.jpg")
print(x)
# 先尝试缩放操作,后面的参数是缩放的比例,可以采用四种方式进行插值,这里用的是最近邻法
plot(imgScale(x,1.3,1.3,interpolation='nearestneighbor'))
#之后进行柔化降噪,此处采用的是中位数滤镜
plot(imgBlockMedianFilter(x,5))

# 也很方便进行图片裁剪,先设定好左上角的坐标像素,再设定好图片的宽度和高度即可
plot(imgCrop(x,1,1,320,200))
# 修改后的结果可以用writeJpeg函数存到本地磁盘中
y <- imgCrop(x,1,1,320,200)
writeJpeg(filename='test.jpg',imgdata=y)

# 另外还有一些有用的处理函数
# 图像聚类以进行颜色的压缩
plot(imgKMeans(x,8))

# 翻转图像
plot(imgHorizontalMirroring(x))
# 负片效果
plot(imgNegative(x))
# 转灰度效果
plot(imgRGB2Grey(x))

#图像背景色更换
y <- unclass(x)
y1 <- y[,,1]
y2 <- y[,,2]
y3 <- y[,,3]
#白色255 255 255
z <- y1>220&y2>220&y3>220
y1[z] <- 255  #r
y2[z] <-0       #g
y3[z]<- 0       #b
# 修改后的矩阵再重新组合为图片格式
y <- imagedata(array(data=c(y1,y2,y3),dim=c(320,320,3)))
plot(y)

R软件矩阵使用总结

1 矩阵转置
A为m×n矩阵,求a'在R中可用函数t()
>A <- matrix(c(2,1,1,2),nrow = 2)
t>(A)
2 矩阵相加减
R中对同行同列矩阵相加减,可用符号:“+”、“-”,
3 数与矩阵相乘
A为m×n矩阵,c>0,在R中求cA可用符号:“*”
4  矩阵相乘
A为m×n矩阵,B为n×k矩阵,在R中求AB可用符号:“%*%”
> A=matrix(1:12,nrow=3,ncol=4)
> B=matrix(1:12,nrow=4,ncol=3)
> A%*%B
5 矩阵秩
>library(Matrix)
>rankMatrix(A)
6 矩阵的特征值与特征向量
>eigen(A)  #A为方阵
7 矩阵求逆
矩阵求逆可用函数solve(),应用solve(a, b)运算结果是解线性方程组ax = b,若b缺省,则系统默认为单位矩阵,因此可用其进行矩阵求逆
> solve(a)
8 矩阵对角元素相关运算
取一个方阵的对角元素
>diag(A)
函数将产生以这个向量为对角元素的对角矩阵
>diag(diag(A))
对一个正整数z应用diag()函数将产生以z维单位矩阵
>diag(3)
9 行列式的值
det(a)
 行列式的值=方阵的特征值的乘积
A的迹(矩阵对角元素的和)= 方阵的特征值的和
prod<- function(b){
  prod<- 1
  for(i in 1:length(b)){
    prod<- prod*b[i]
  }
  prod
}
prod(b$value)=det(a)
sum(b$value)=sum(diag(a))
10 对角阵
diag(eigen(a)$values)

例题  R解多元一次方程,方程如下
x+2y+2=7
2x-y+3z=7
3x+y+2z=18
R中求解如下
a <- matrix(c(1,2,3,2,-1,1,1,3,2),nrow = 3)
b <- c(7,7,18)
solve(a,b)

2015年4月10日星期五

Install R in Ubuntu

Step 1: Update Sources.List File
- Edit the sources.list file
sudo gedit /etc/apt/sources.list
- Add following entry
deb http://cran.rstudio.com/bin/linux/ubuntu utopic/
Step 2: Add the Public Keys
gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add -
Step 3: Install R-base
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install r-base build-essential gfortran libxml2-dev libcurl4-openssl-dev libfreetype6-dev libbz2-dev liblapack-dev  libpcre++-dev  r-base-dev  liblzma-dev r-cran-rcpp r-cran-rjava openjdk-7-jre

wants <- c("knitr","rmarkdown","devtools","epicalc","mosaic","showtext","pander","PerformanceAnalytics","fitdistrplus","CircStats","MASS","mixtools","boot","TrialSize","vcd","ggplot2","pspearman","gvlma","car","lmtest","leaps","plyr","bootstrap","elrm","rms","Deducer","bestglm","survival","robust","mlogit", "nnet", "VGAM","ordinal","Sample.Size","samplesize4surveys","SampleSizeMeans","SampleSizeProportions","mosaic","XML","pipeR","formatR","shiny","expm","koRpus")
has   <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])

devtools::install_github("rstudio/rticles")
devtools::install_github("rstudio/rmarkdown")


2015年3月15日星期日

proxychains安装使用

1.安装proxychains
sudo apt-get install proxychains
2. 修改配置
sudo /etc/proxychains.conf

[ProxyList]
# add proxy here ...
# meanwile
# defaults set to "tor"
socks5     127.0.0.1 1080

3.使用
sudo proxychains apt-get install ××××
sudo proxychains apt-get update