使用 fix() 或 edit() 函数更改 R 中的另一个函数

Using the fix() or edit() function to alter another function in R

我正在尝试在 R 中编辑一个函数,该函数在一个名为 'TwoSampleMR' 的包中,该函数本身称为 'forest_plot_1_to_many',该函数绘制了多次曝光的森林图结果。

我想更改图中文本的大小,不幸的是字体大小是预先设置的,无法在调用函数时进行编辑。因此,我正在尝试编辑函数本身。很多文章说使用 trace() 但这让我感到困惑,我认为这不是我要找的,因为我没有修复代码中的错误,我只是更改它。

我看到一篇文章说使用 fix()edit() 来编辑函数,所以我尝试: 修复(fores_plot_1_to_many)

结果弹出编辑,我将 - 'size=10' 调整为 'size=15'。

但是该函数根本无法运行并出现错误:

could not find function "forest_plot_names2"

我的问题是 fix()edit() 可以用来改变函数吗?

可行的例子:

install_github('MRCIEU/TWOSampleMR')
library(TwoSampleMR)
library(cowplot)
library(gridExtra)
library(ggplot2)
#you then need data formated like this(use dget):

structure(list(id.exposure = c("1", "1", "1", "1", "1", "100", "104", "2", "2", "2", "2", "2", "72", "72", "72", "72", "72", "999", "999", "999", "999", "999"), id.outcome = c("7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7"), outcome = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Coronary heart disease || id:7", class = "factor"), exposure = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L), .Label = c("Adiponectin || id:1", "Hip circumference || id:100", "Waist circumference || id:104", "Body mass index || id:2", "Waist-to-hip ratio || id:72", "Body fat || id:999"), class = "factor"), method = structure(c(2L, 5L, 1L, 3L, 6L, 1L, 4L, 2L, 5L, 1L, 3L, 6L, 2L, 5L, 1L, 3L, 6L, 2L, 5L, 1L, 3L, 6L), .Label = c("Inverse variance weighted", "MR Egger", "Simple mode", "Wald ratio", "Weighted median", "Weighted mode"), class = "factor"), nsnp = c(14, 14, 14, 14, 14, 2, 1, 79, 79, 79, 79, 79, 30, 30, 30, 30, 30, 10, 10, 10, 10, 10), b = c(-0.117626170585508, -0.0897929616150017, -0.0859881930532524, -0.0481657038212953, -0.0924464446740587, -0.186310066104502, -0.446296296296296, 0.502493509729729, 0.387006481544447, 0.445909096953398, 0.34015542866459, 0.388824880964004, 0.319237123956867, 0.440777466374666, 0.479474060003586, 0.613267898718917, 0.539371930718197, -0.099234917723361, 0.492007216440334, 0.266451632000445, 0.625148495006193, 0.584255954193752), se = c(0.099327158876229, 0.052718121960683, 0.0701141773426372, 0.0805966230812049, 0.0480249872448083, 0.212900382630982, 0.348251851851852, 0.143960561723383, 0.073084143930427, 0.0589830187629256, 0.159504867346875, 0.103010766708134, 0.670839958692018, 0.130066610555476, 0.147334903140841, 0.314200209490734, 0.220010589431754, 1.62221372607913, 0.165297837802832, 0.329449407451525, 0.239252578485129, 0.171657887534756), pval = c(0.259248511221647, 0.0885183822967944, 0.220047295734388, 0.560363740959668, 0.0763956662641734, 0.381517091710096, 0.200006460243279, 0.000801258991849775, 1.18785466550157e-07, 4.03202032734861e-14, 0.0361057370718699, 0.000310917456068699, 0.637854263026472, 0.000701839665612893, 0.00113666506799647, 0.0606742038535203, 0.0204847853665443, 0.952722454601399, 0.00291569423908827, 0.418642505104635, 0.0281355793252735, 0.00782847174837027)), .Names = c("id.exposure", "id.outcome", "outcome", "exposure", "method", "nsnp", "b", "se", "pval"), row.names = c(NA, -22L), class = "data.frame")

#once you have the data saved as a dataframe you can use 
res1<-subset_on_method(res1)
res1<-sort_1_to_many(res1,b="b",sort_action=4)
forest_plot_1_to_many(res1,b="b",se="se",
                       exponentiate=F,ao_slc=F,lo=NULL,up=NULL,
                       TraitM="exposure",col1_width=1.3,by=NULL,
                       trans="identity",xlab= "(95% confidence interval)",
                       addcols=c("nsnp","b","se","pval"),addcol_widths=c(0.6,1.1,1.1,1.1),
                       addcol_titles=c("No.SNPs","Beta", "se","P-val")  
)  
#this will give a goo looking forest plot, you can then try edit the function using fix() or edit()
fix(forest_plot_1_to_many)
## OR 
var1 <- edit(forest_plot_1_to_many)

希望有用:) 谢谢

这可能是因为 forest_plot_1_to_many 使用了未导出的 forest_plot_names2 函数。因为 forest_plot_1_to_manyTwoSampleMR 命名空间中,它可以 'see' 该命名空间中的其他函数,即使它们没有被导出。 当您将函数复制到您的全局环境时 如果您更改函数的环境,它就不能再'see' 那些未导出的函数。 如果您使用 fix,或者 如果您简单地打印函数并手动 copy/paste/edit/evaluate 它,这是真的。

要快速解决问题,您可以使用 ::: 调用未导出的函数。当您 fix forest_plot_1_to_many 将所有对 forest_plot_names2 的调用替换为 TwoSampleMR:::forest_plot_names2。更复杂的事情是可能的,例如 fixInNamespace 但我没有太多经验。