R中避免重复
在R中要加载多个包,进行全局的配置,定义全局变量,我们可以下一个config.R文件,在分析的代码开头source一下这个config.R文件即可避免每次都要加载包的冗余代码
using是之前定义的一个函数,作用是一次性加载多个R包,并且不打印包加载信息,可以参考**《如何优雅地管理R包》**,当然使用suppressPackageStartupMessages(library())也可以只是要写很多行代码
config.R中加载了R包,设置了函数的优先级,定义了全局变量(常量)
# config.R file
# 加载R包
suppressPackageStartupMessages(library(data.table))
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(magrittr))
suppressPackageStartupMessages(library(conflicted))
suppressPackageStartupMessages(library(Seurat))
suppressPackageStartupMessages(library(WGCNA))
suppressPackageStartupMessages(library(GSVA))
suppressPackageStartupMessages(library(impute))
suppressPackageStartupMessages(library(clusterProfiler))
suppressPackageStartupMessages(library(enrichplot))
# 加载R包或者写成
using(data.table,tidyverse,magrittr,conflicted,Seurat,WGCNA,GSVA,impute,enrichplot,clusterProfiler)
conflicted::conflict_prefer("filter", "dplyr", quiet = TRUE)
conflicted::conflict_prefer("select", "dplyr", quiet = TRUE)
options(bitmapType = "cairo") # for VS code plot
# 工作路径
WORK_DIR <- "/Volumes/Elements/Project"
# 绘图颜色
COLORS <- c("#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E", "#E6AB02", "#A6761D", "#666666")
多个脚本文件中只需一行代码就可以执行上述内容
source("config.R")
# 其他分析代码 ...
Python中避免重复
# config.py file
# 导入函数
from pathlib import Path
import re
import warnings
import site
import numpy as np
from scipy import sparse
import scipy.stats as spss
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import scanpy as sc
import squidpy as sq
import celltypist
import gseapy
# 自己写的包sckit、bioquest,导入需要在python库中加上路径
WORK_DIR="/Volumes/Elements/Project"
site.addsitedir(WORK_DIR)
import sckit as sk
import bioquest as bq
# 单细胞scanpy设置
warnings.filterwarnings(action="ignore")
sc.settings.verbosity = 3 # verbosity: errors (0), warnings (1), info (2), hints (3)
sc.logging.print_header()
sc.settings.set_figure_params(dpi=80, facecolor='white')
# 调色包常量
COLORS=["#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E", "#E6AB02", "#A6761D", "#666666"]
其他脚本文件中只需一行代码就可以执行上述内容,从而避免了 多次设置,导入包函数
execfile("config.py")
# 其他分析代码 ...