https://github.com/blmoore/pythonistr
Write more pythonic R
https://github.com/blmoore/pythonistr
python r rstats
Last synced: about 1 month ago
JSON representation
Write more pythonic R
- Host: GitHub
- URL: https://github.com/blmoore/pythonistr
- Owner: blmoore
- License: mit
- Created: 2017-05-13T21:58:44.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-03T09:51:23.000Z (about 9 years ago)
- Last Synced: 2025-06-01T18:40:05.290Z (about 1 year ago)
- Topics: python, r, rstats
- Language: R
- Homepage: http://blm.io/pythonistr
- Size: 48.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# pythonistr
[](https://travis-ci.org/blmoore/pythonistr)
[](https://codecov.io/gh/blmoore/pythonistr)
[](http://blm.io/pythonistr)
[](https://cran.r-project.org/package=pythonistry)
pythonistr brings over a few ideas from the python language into R.
## Install
Install from github with:
```{r, eval = FALSE}
devtools::install_github("blmoore/pythonistr")
```
## Usage
```{r load_pkg, echo=FALSE}
library(pythonistr)
```
### Shortcuts
In python, to instantiate a list of strings with minimal typing you could use:
```{python}
print('i want a list of words'.split())
```
pythonistr adds `separate` (or `s` for short):
```{r}
s("saves you typing and matching quotes")
```
----------
String and sub-string matching intent is clear in Python:
```{python}
if 'char' in 'character string':
print('Found!')
```
pythonistr adds `%within%` as an alias for `grepl(pattern, string)`:
```{r}
if ('char' %within% 'character string')
print('Found!')
```
------
### Python functions
Python has a great context management system that closes file connections
when they fall out of scope:
```{python with_example, eval=FALSE}
with open('file.csv', 'r') as f:
for line in f:
print(line)
```
Pythonistr adds a `with` method to connections which mirrors this behaviour:
```{r with_r, eval=FALSE}
file_conn <- file("file.csv", "r")
with(file_conn, {
while (TRUE) {
line <- readLines(file_conn, n = 1)
if (length(line) == 0) {
break
}
print(line)
}
})
isOpen(file_conn) # FALSE (well, error)
```
Even better, there's some support for line-by-line processing. In Python you
might write:
```{python iterate_stop, eval=FALSE}
with open('log.txt', 'r') as l:
for line in l:
print line
if 'stop' in line:
break
```
With pythonistr, you could write this as:
```{r iterate_stop_r, eval=FALSE}
log <- file('log.txt', 'r')
with(log,
by_line(log,
print, # function to apply to each line
function(l) grepl("stop", l) # stop when true
)
)
```
Anything executed by `by_line` is currently only useful for its side-effects.
### Aliases
If you regularly switch between R and Python you might be used to
getting tripped up by `length` vs. `len` but this isn't the only
example. For example: which language prefers `reversed` over `rev`? Which
implements `sorted` as well as `sort`?
pythonistr includes a few shortcuts to lower the context
switching overhead, though it's probably a bad idea to rely
on these in normal R programming.