简体   繁体   English

无法在 Jupyter Notebook 中加载 processR 包的 `model1` 函数

[英]Can not load the `model1` function of the processR package in a Jupyter Notebook

I am very new to the whole R programming and trying to follow this tutorial , where the model1 function is used to find the Andrew F. Hayes correlation between three variables.我对整个 R 编程非常model1 ,并试图按照本教程进行操作,其中使用model1函数查找三个变量之间的 Andrew F. Hayes 相关性。 As indicated in the tutorial I have the packages installed:如教程中所示,我安装了软件包:

  1. install.packages("devtools")
  2. install.packages("processR")
  3. devtools::install_github("markhwhiteii/processr")

I have also followed the steps:我也遵循了以下步骤:

set.seed(1839)
var1 <- rnorm(100)
cond <- rbinom(100, 1, .5)
var2 <- var1 * cond + rnorm(100)
df3 <- data.frame(var1, var2, cond)
head(df3)

accordingly.因此。 However, when running:但是,在运行时:

mod1result <- model1(iv = "var1", dv = "var2", mod = "cond", data = df3)

I get the error message:我收到错误消息:

Error in model1(iv = "var1", dv = "var2", mod = "cond", data = df3): could not find function "model1" Traceback:模型 1 中的错误(iv = "var1", dv = "var2", mod = "cond", data = df3):找不到函数 "model1" 回溯:

and running和跑步

mod1result <- processr::model1(iv = "var1", dv = "var2", mod = "cond", data = df3)

Error in loadNamespace(name): there is no package called 'processr' Traceback: loadNamespace(name) 中的错误:没有名为“processr”的包回溯:

The strange thing is that the same code just worked yesterday and now it doesn't.奇怪的是,同样的代码昨天才起作用,现在却没有了。 I would appreciate it if you could help me understand what is wrong and how I can resolve it.如果您能帮助我了解问题所在以及如何解决,我将不胜感激。

PS1. PS1。 I'm not sure what .libPaths() is but for some reason it returns two paths on my mac:我不确定.libPaths()是什么,但由于某种原因,它在我的 mac 上返回了两条路径:

  • /usr/local/lib/R/3.6/site-library
  • /usr/local/Cellar/r/3.6.2/lib/R/library

does it mean that I have two installations of R and this is the main cause of the above issues?这是否意味着我安装了两个 R,这是导致上述问题的主要原因?

PS2. PS2。 OK.好的。 This seems to be Jupyter's fault as everything is just working fine in the terminal.这似乎是 Jupyter 的错,因为在终端中一切正常。

PS3. PS3。 What seems to be working in the terminal is:似乎在终端中工作的是:

  • sudo r
  • devtools::install_github("markhwhiteii/processr")
  • library(processr) notice lower case r in the processr library(processr)processr注意小写r

PS4. PS4。 I'm not sure if this is Jupyter's fault.我不确定这是否是 Jupyter 的错。

PS5. PS5。 I tried installing the packages on Windows as well.我也尝试在 Windows 上安装这些软件包。 It was even worse.情况更糟。 I can't get passed the issue:我无法通过这个问题:

Error: Failed to install 'processr' from GitHub: (converted from warning) cannot remove the prior installation of package 'digest'错误:无法从 GitHub 安装“处理器”:(从警告转换)无法删除先前安装的包“摘要”

I think maybe the key to solving this problem is to understand what is the difference between these packages:我想也许解决这个问题的关键是要了解这些包之间的区别是什么:

  • install.packages("processR")
  • devtools::install_github("markhwhiteii/processr")
  • devtools::install_github("cardiomoon/processR")

OK, after a couple of hours of trial and error, I think I have a messy workaround, not a solution though!好的,经过几个小时的反复试验,我想我有一个凌乱的解决方法,但不是解决方案!

  • run sudo r in one terminal在一个终端中运行sudo r
  • run jupyter notebook in another and open an R notebook (I suppose you have the kernel installed)在另一个中运行jupyter notebook并打开一个 R notebook(我想你已经安装了内核)
  • now you should understand that the devtools::install_github("markhwhiteii/processr") and install.packages("processR") are two different packages and you gave install both every time you restart your kernel in the Jupyter Notebook现在你应该明白devtools::install_github("markhwhiteii/processr")install.packages("processR")是两个不同的包,你每次在 Jupyter Notebook 中重新启动内核时都给了 install

  • install devtools::install_github("markhwhiteii/processr") first in the R terminal首先在 R 终端中安装devtools::install_github("markhwhiteii/processr")

  • now in the Jupyter side you should be able to library(processr) and run processr::model1现在在 Jupyter 方面,您应该能够使用library(processr)并运行processr::model1
  • next install the install.packages("processR") on the R terminal接下来在 R 终端上安装install.packages("processR")
  • now import the library(processR)现在导入library(processR)
  • now you should be able to run the functions such as pmacroModel etc现在您应该能够运行pmacroModel等功能

Basically you need both processr and processR !基本上你需要processrprocessR

BTW, the same issue is the R terminal.顺便说一句,同样的问题是 R 终端。 You have to run as sudo and follow the above steps to get everything working!您必须以sudo身份运行并按照上述步骤操作才能使一切正常!

From the tutorial page you references, install processr (not processR ) in the following way:从您引用的教程页面,按以下方式安装processr (而不是processR ):

# Run this if devtools isn't installed.
# install.packages("devtools")

# Run to install "processr"
devtools::install_github("markhwhiteii/processr")

After that successfully completes, processr::model1 will be defined.成功完成后,将定义processr::model1

When running the code in this post it produces a result:运行这篇文章中的代码时,它会产生一个结果:

set.seed(1839)
var1 <- rnorm(100)
cond <- rbinom(100, 1, .5)
var2 <- var1 * cond + rnorm(100)
df3 <- data.frame(var1, var2, cond)
head(df3)

mod1result <- processr::model1(iv = "var1", dv = "var2", mod = "cond", data = df3)

mod1result
## # A tibble: 6 x 5
##   term          estimate std.error statistic       p.value
##   <chr>            <dbl>     <dbl>     <dbl>         <dbl>
## 1 intercept       0.133      0.146     0.916 0.362        
## 2 var1            0.0696     0.156     0.445 0.657        
## 3 cond           -0.173      0.200    -0.865 0.389        
## 4 interaction     0.854      0.213     4.01  0.000118     
## 5 when cond = 0   0.0696     0.156     0.445 0.657        
## 6 when cond = 1   0.924      0.144     6.40  0.00000000577

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM