使用fix()或edit()函数更改R

时间:2019-01-04 22:06:50

标签: r edit

我正在尝试在R中编辑一个函数,该函数位于名为“ TwoSampleMR”的程序包中,该函数本身称为“ forest_plot_1_to_many”,该函数针对结果绘制多个曝光的森林图。

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

我看到一篇文章说使用 fix() edit()进行功能的编辑,所以我尝试:     修复(fores_plot_1_to_many)

并因此弹出编辑,我进行了调整-将'size = 10'调整为'size = 15'。

但是该函数根本无法工作并出现错误:

  

找不到函数“ 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.idoexpo = 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”),结果=结构(c(1L,   1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,   1L,1L,1L,1L,1L),. Label =“冠心病|| id:7”,类别=“因素”),       曝光=结构(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(“脂联素|| id:1”,       “臀围|| id:100”,“腰围|| id:104”,       “身体质量指数|| id:2”,“腰臀比|| id:72”,       “身体脂肪|| id:999”),类=“ factor”),方法=结构(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(“方差反比加权”,       “ MR Egger”,“简单模式”,“ Wald比”,“加权中位数”,       “加权模式”),类=“因子”),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))。名称= 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)

希望有效:) 谢谢

1 个答案:

答案 0 :(得分:1)

这可能是因为{ Id = xxx, Prefix = xxx, ArtistName = xxx, Artist = { Id = xxx, Name = xxx, Country = xxx } } 使用了未导出的forest_plot_1_to_many函数。因为forest_plot_names2forest_plot_1_to_many命名空间中,所以即使未导出它们,它也可以“看到”该命名空间中的其他函数。 将功能复制到全局环境中时如果更改功能的环境,它将不再能“看到”那些未导出的功能。如果您使用TwoSampleMR,这是正确的,或者如果您只是打印功能并手动复制/粘贴/编辑/评估,则为

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