小编cle*_*ens的帖子

在 RETICULATE_PYTHON 环境变量中指定网状 python 路径

每当我在 RStudio 中使用 reticulate 时,默认 REPL 都会使用python2.7,但我想使用python3默认值。我已将 python 路径添加到python3环境.bashrc变量中RETICULATE_PYTHON,当我从命令行使用 R 和 reticulate 时,Sys.getenv('RETICUALTE_PYTHON')返回/usr/bin/python3. 如果使用在命令行中打开 REPL,我会得到正确的路径。如果我在 RStudio 中执行相同操作,我会得到一个空字符串。

R
Run Code Online (Sandbox Code Playgroud)
Sys.getenv('RETICULATE_PYTHON')
Run Code Online (Sandbox Code Playgroud)

在 R 中返回(从命令行):

[1] "/usr/bin/python3"

在 RStudio 中:

[1] ""

在 RStudio 终端中,输出是正确的:

Sys.getenv('RETICULATE_PYTHON')
Run Code Online (Sandbox Code Playgroud)

另外,当我从命令行启动 R 时,py_config()是这样的:

> library(reticulate)
> py_config()
python:         /usr/bin/python3
libpython:      /usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so
pythonhome:     /usr:/usr
version:        3.6.7 (default, Oct 22 2018, 11:32:17)  [GCC 8.2.0]
numpy:          /usr/lib/python3/dist-packages/numpy
numpy_version:  1.14.5

NOTE: Python version was forced …
Run Code Online (Sandbox Code Playgroud)

r rstudio reticulate

9
推荐指数
2
解决办法
7236
查看次数

测试包写入磁盘的功能

我正在尝试为 R 中的包函数编写测试。

\n\n

假设我们有一个函数,只需x使用以下命令将字符串写入磁盘writeLines()

\n\n
exporting_function <- function(x, file) {\n\n writeLines(x, con = file)\n\n invisible(NULL)\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

测试它的一种方法是检查文件是否存在。通常,它一开始不应该存在,但在运行导出函数之后它应该存在。另外,您可能想测试文件大小是否大于 0:

\n\n
library(testthat)\n\ntest_that("file is written to disk", {\n file = \'output.txt\'\n expect_false(file.exists(file))\n\n exporting_function("This is a test",\n                    file = file)\n\n\n expect_true(file.exists(file))\n\n expect_gt(file.info(\'output.txt\')$size, 0)\n})\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是一个很好的测试方法吗?在CRAN 存储库政策中,它指出Packages should not write in the user\xe2\x80\x99s home filespace (including clipboards), nor anywhere else on the file system apart from the R session\xe2\x80\x99s temporary directory. 这个测试会违反这个限制吗?

\n\n

有一个expect_output_file函数。从文档和示例中,我不确定这是否是测试该功能的更合适的期望。它需要一个object参数,该参数应该是 …

r cran testthat

7
推荐指数
1
解决办法
624
查看次数

过滤data.frame中的前n个最大组

对于示例数据:

set.seed(2222)
example_data <- data.frame(col1 = 1:15,
                           col2 = 16:30, 
                           group = sample(1:3, 15, replace = TRUE))

   col1 col2 group
1     1   16     2
2     2   17     1
3     3   18     3
4     4   19     2
5     5   20     3
6     6   21     1
7     7   22     3
8     8   23     1
9     9   24     3
10   10   25     1
11   11   26     2
12   12   27     2
13   13   28     2
14   14   29     3
15   15   30     3 …
Run Code Online (Sandbox Code Playgroud)

r

5
推荐指数
1
解决办法
106
查看次数

使用 rootless podman 公开端口

我正在尝试在 RHEL 8.3 上使用无根 podman 公开端口 8080。

我使用的podman版本是:

$ podman --version
podman version 2.2.1
Run Code Online (Sandbox Code Playgroud)

我正在使用一个简单的FlaskAPI 来测试它:

from flask import Flask

app = Flask(__name__)


@app.route("/")
def hello():
    return "Hello from the container!\n"


if __name__ == "__main__":
    app.run(host="0.0.0.0")
Run Code Online (Sandbox Code Playgroud)

容器文件如下所示:

FROM python:3.6-alpine

RUN pip3 install flask

COPY app.py app.py

EXPOSE 5000

ENTRYPOINT python3 app.py

Run Code Online (Sandbox Code Playgroud)

我正在使用以下方法构建图像:

FROM python:3.6-alpine

RUN pip3 install flask

COPY app.py app.py

EXPOSE 5000

ENTRYPOINT python3 app.py

Run Code Online (Sandbox Code Playgroud)

我正在创建一个 Pod 并在该 Pod 中启动一个容器

$ podman build -t testapi .
Run Code Online (Sandbox Code Playgroud)

所有容器都按预期运行: …

rhel podman

5
推荐指数
1
解决办法
8407
查看次数

重新排列数据框中的行

我在R中有一个数据框,看起来像这样,有两列:

ID     phone_number
Mark     866458
Paul     986564
Jack     987543
Mary     523422
Run Code Online (Sandbox Code Playgroud)

我想要这种输出,其中我只有一列

Mark
866458
Paul
986564
Jack
987543
Mary
523422
Run Code Online (Sandbox Code Playgroud)

有人知道我可以使用哪个R代码来获得输出吗?


可重复性数据:

structure(list(ID = c("Mark", "Paul", "Jack", "Mary"), phone_number = c(866458, 
                                                                        986564, 987543, 523422)), row.names = c(NA, -4L), class = c("tbl_df", "data.frame"))
Run Code Online (Sandbox Code Playgroud)

r

3
推荐指数
1
解决办法
98
查看次数

在R中转换日期格式

dates <- as.Date(dli$Dates)

class(dates)
[1] "Date"

dates
   [1] "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04" "2016-01-05" "2016-01-06"
   [7] "2016-01-07" "2016-01-08" "2016-01-09" "2016-01-10" "2016-01-11" "2016-01-12"
  [13] "2016-01-13" "2016-01-14" "2016-01-15" "2016-01-16" "2016-01-17" "2016-01-18"
  [19] "2016-01-19" "2016-01-20" "2016-01-21" "2016-01-22" "2016-01-23" "2016-01-24"
  [25] "2016-01-25" "2016-01-26" "2016-01-27" "2016-01-28" "2016-01-29" "2016-01-30"
  [31] "2016-01-31" "2016-02-01" "2016-02-02" "2016-02-03" "2016-02-04" "2016-02-05"
  [37] "2016-02-06" "2016-02-07" "2016-02-08" "2016-02-09" "2016-02-10" "2016-02-11"
Run Code Online (Sandbox Code Playgroud)

这是我的日期格式,所以我需要将其转换为“ 2016-month-day”,我正在获取NA值

dates <- as.Date(dli$Dates,"%d/%b/%Y")

class(dates)
[1] "Date"

dates
   [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA …
Run Code Online (Sandbox Code Playgroud)

r date

0
推荐指数
3
解决办法
1万
查看次数

标签 统计

r ×5

cran ×1

date ×1

podman ×1

reticulate ×1

rhel ×1

rstudio ×1

testthat ×1