前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux23-直接在命令行运行python或R

linux23-直接在命令行运行python或R

作者头像
北野茶缸子
发布2022-07-07 14:44:19
8700
发布2022-07-07 14:44:19
举报
  • Date : [[2022-05-01_Sun]]
  • Tags : #linux/index/01

前言

最近看到csvtk这款工具,里面提到作者的初衷:

sed/awk/cut等Shell命令主要用于通用的表格数据,不适合含有标题行的CSV格式;为了一个小操作写Python/R脚本也有点小题大作,且难以复用。

确实有的时候,我们需要结合python,R 或者一般shell 语法的特性,结合使用,甚至制作精巧的pipeline。

我们可以不可以直接在命令行中使用shell 语法呢?

就像在R 或python 中,我们可以分别使用:

  • system("echo 'test'");
  • import os; os.system('ls -lh')

既然你有了,那我呢?(shell 举了举手)

命令行内使用python或R

python 提供了-c 选项,我们可以直接利用该选项,输入python 命令:

代码语言:javascript
复制
$ python3 -c 'a=3;print(a)'
3

但无奈的是,我仅仅发现,R 只提供了一个-e EXPR 选项,让你输入R 命令后,R 会进入到环境并进行相关计算就退出:

代码语言:javascript
复制
$ R -e '1+1'

R version 3.2.2 (2015-08-14) -- "Fire Safety"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 1+1
[1] 2

这非常的不优雅。

既然Rscript,可以接受脚本进行R 命令运行,那么,我们可否在不书写R 脚本的情况下,直接把内容传递给Rscript 呢?

如果是重定向输入或者是管道符号呢?

代码语言:javascript
复制
$ Rscript << test
> 1+1
> test
Usage: /path/to/Rscript [--options] [-e expr [-e expr2 ...] | file] [args]

--options accepted are
  --help              Print usage and exit
  --version           Print version and exit
  --verbose           Print information on progress
  --default-packages=list
                      Where 'list' is a comma-separated set
                        of package names, or 'NULL'
or options to R, in addition to --slave --no-restore, such as
  --save              Do save workspace at the end of the session
  --no-environ        Don't read the site and user environment files
  --no-site-file      Don't read the site-wide Rprofile
  --no-init-file      Don't read the user R profile
  --restore           Do restore previously saved objects at startup
  --vanilla           Combine --no-save, --no-restore, --no-site-file
                        --no-init-file and --no-environ

'file' may contain spaces but not shell metacharacters
Expressions (one or more '-e <expr>') may be used *instead* of 'file'
See also  ?Rscript  from within R

并不好用。似乎Rscript 并不接受来自标准输入的内容来源。

我忽然想到了shell的<(),其可以将结果以文件的形式作为输入传递给其他命令:

代码语言:javascript
复制
$ Rscript <(echo "1+1")
[1] 2

好了,现在你知道,该如何在命令行中使用python 或R 了吗?

依然存在缺陷

比如,如果我们想要用管道符传递这样的结果,或者是,我们的shell 脚本中希望使用变量呢?似乎命令行内直接使用其他语言都不是一个明智的决定。当我们需要其他语言时,还是得依靠脚本,实现输入输出的传递。

而尴尬之处也在这里,不使用脚本的话,文件很难像shell 一样仅仅一两句命令实现输入输出。

命令行中直接运行的场景,目前我能想到的也仅仅是,你可以直接在命令行内进行数学计算了:

代码语言:javascript
复制
❯ 1+1
zsh: command not found: 1+1
❯ python3 -c 'print(1+2)'
3

然而,似乎直接进入ide,更加方便:

代码语言:javascript
复制
❯ python
Python 3.9.7 (default, Sep 16 2021, 08:50:36)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+2
3
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 北野茶缸子 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 命令行内使用python或R
  • 依然存在缺陷
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档