-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa7b17c
commit cc6618d
Showing
12 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
""" | ||
@author: Walter Verwer | ||
@git: https://github.com/walterverwer | ||
""" | ||
from .r_importer import r_importer | ||
from .suppress import suppress | ||
from .write_run import write_run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import rpy2.robjects.packages as rpackages | ||
from rpy2.robjects.vectors import StrVector | ||
from walpy import suppress | ||
|
||
def r_importer(modules, install_only=[], log=False): | ||
""" | ||
Import and install R packages. If the desired packages are not installed it will | ||
automatically install them. Note that this function will act as a one time | ||
delay in running time, if modules need to be installed. Import R packages | ||
manually as e.g. <stargazer = rpy2.robjects.packages.importr('stargazer')>. | ||
So, the same name used for installing, should be used to import the functions. | ||
Important to note, this function imports the following modules from rpy2: | ||
"rpy2.robjects.packages" and "rpy2.robjects.vectors". | ||
Args: | ||
modules: list of the desired packages. The packages to be included should | ||
be as a string. E.g. modules = ['stargazer', 'tidyverse']. | ||
install_only: default=None. list or string of packages to be installed | ||
only. Note, combinations are possible. | ||
log: default=False. Prints a log message if true, of the packages that are | ||
(succesfully) installed. | ||
Returns: | ||
None | ||
""" | ||
# import R's utility package: | ||
utils = rpackages.importr('utils') | ||
|
||
# R package names: | ||
packnames = tuple(modules) | ||
|
||
# Selectively install what needs to be install. Use CRAN cloud server: | ||
names_to_install = [x for x in packnames if not rpackages.isinstalled(x)] | ||
if len(names_to_install) > 0: | ||
print('Installing:', names_to_install) | ||
utils.install_packages(StrVector(names_to_install), | ||
repos='https://cloud.r-project.org/') | ||
print('Successfully installed:', names_to_install) | ||
|
||
# Make modules non-overlapping with install_only: | ||
modules = set(modules) - set(install_only) | ||
|
||
# Import modules to be automatically imported | ||
for module in modules: | ||
rpackages.importr(module) | ||
|
||
# Print log message if true: | ||
if log == True: | ||
print('Successfully imported:', [i for i in modules]) | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import sys | ||
import os | ||
class suppress(object): | ||
|
||
def __init__(self,stdout = None, stderr = None): | ||
self.devnull = open(os.devnull,'w') | ||
self._stdout = stdout or self.devnull or sys.stdout | ||
self._stderr = stderr or self.devnull or sys.stderr | ||
|
||
def __enter__(self): | ||
self.old_stdout, self.old_stderr = sys.stdout, sys.stderr | ||
self.old_stdout.flush(); self.old_stderr.flush() | ||
sys.stdout, sys.stderr = self._stdout, self._stderr | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
self._stdout.flush(); self._stderr.flush() | ||
sys.stdout = self.old_stdout | ||
sys.stderr = self.old_stderr | ||
self.devnull.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from IPython.core.magic import register_cell_magic | ||
from IPython import get_ipython | ||
@register_cell_magic | ||
def write_run(line, cell): | ||
""" | ||
Write and run the cell. Use as follows: | ||
%%write_and_run script.py to overwrite existing scripts. | ||
%%write_and_run -a script.py to append to the existing script. | ||
For both cases, if the script does not exist, then a new one is | ||
created automatically. | ||
Args: | ||
line: ignore. See above. | ||
cell: ignore. See above. | ||
Returns: | ||
None | ||
""" | ||
argz = line.split() | ||
file = argz[-1] | ||
mode = 'w' | ||
if len(argz) == 2 and argz[0] == '-a': | ||
mode = 'a' | ||
with open(file, mode) as f: | ||
f.write(cell) | ||
get_ipython().run_cell(cell) | ||
|
||
return |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Metadata-Version: 2.1 | ||
Name: walpy | ||
Version: 0.2 | ||
Summary: General functions | ||
Home-page: https://github.com/walterverwer/walpy | ||
Author: Walter Verwer | ||
Author-email: walterverwer@gmail.com | ||
License: MIT | ||
Download-URL: https://github.com/walterverwer/walpy/archive/refs/tags/v_0.1.tar.gz | ||
Description: # Readme walpy | ||
Walpy is a general purpose package containing '30-second-functions'. | ||
|
||
Keywords: SOME,MEANINGFULL,KEYWORDS | ||
Platform: UNKNOWN | ||
Classifier: Development Status :: 3 - Alpha | ||
Classifier: Intended Audience :: Developers | ||
Classifier: Topic :: Software Development :: Build Tools | ||
Classifier: License :: OSI Approved :: MIT License | ||
Classifier: Programming Language :: Python :: 3 | ||
Classifier: Programming Language :: Python :: 3.4 | ||
Classifier: Programming Language :: Python :: 3.5 | ||
Classifier: Programming Language :: Python :: 3.6 | ||
Description-Content-Type: text/markdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
LICENCE.txt | ||
README.md | ||
setup.cfg | ||
setup.py | ||
walpy/__init__.py | ||
walpy/r_importer.py | ||
walpy/suppress.py | ||
walpy/write_run.py | ||
walpy.egg-info/PKG-INFO | ||
walpy.egg-info/SOURCES.txt | ||
walpy.egg-info/dependency_links.txt | ||
walpy.egg-info/requires.txt | ||
walpy.egg-info/top_level.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rpy2 | ||
IPython |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
walpy |