R脚本中的here()问题 [英] here() issue in R scripts

查看:262
本文介绍了R脚本中的here()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解here()如何以可移植的方式工作.找到了:稍后在最终答案-TL; DR -底线下查看有效的方法,here()在命令行中运行script.R并没有太大用处.

I am trying to understand how would here() work in a portable way. Found it: See what works later under Final answer - TL;DR - the bottom line, here() is not really that useful running a script.R from commandline.

我在JBGruber的帮助下了解它的方式:here()查找项目(例如,RStudio项目,Git项目或其他使用.here文件定义的项目)的根目录.从当前开始工作目录,然后向上移动直到找到任何项目.如果找不到任何内容,则退回到使用完整的工作目录.如果是由cron运行的脚本,它将默认使用我的主目录.当然,可以通过cron命令将目录作为参数传递,但这很麻烦.下面的答案提供了很好的解释,并且我总结了我在最终答案部分"下立即发现的最有用的内容.但是请不要误会,尼古拉的回答也很好,也很有帮助.

The way I understand it with help from JBGruber: here() looks for the root directory of a project (e.g., an RStudio project, Git project or other project defined with a .here file) starting at the current working directory and moving up until it finds any project. If it doesn't find anything it falls back to using the full working directory. Which in case of a script run by cron will default to my home directory. One could, of course, pass directory as a parameter via cron command, but it is rather cumbersome. Below answers provide good explanations and I have summarised what I found most immediately useful under "Final Answer section". But make no mistake, Nicola's answer is very good and helpful too.

原始目标-编写一组R脚本,包括R-markdown .Rmd,以便我可以压缩目录,发送给其他人,然后该目录就可以在他们的计算机上运行.可能在非常低端的计算机上-例如RaspberryPi或运行linux的旧硬件.

Original Objective - write a set of R scripts, including R-markdown .Rmd so that I can zip the directory, send to someone else and it would run on their computer. Potentially on a very low end computer - such as RaspberryPi or old hardware running linux.

条件:

  • 可以通过Rscript
  • 从命令行运行
  • 如上,但通过cron
  • 安排
  • 设置工作目录的主要方法是set_here()-从控制台执行一次,然后该文件夹可移植,因为.here文件包含在压缩目录中.
  • 不需要Rstudio-因此不想执行R项目
  • 也可以从Rstudio(开发)
  • 交互式运行
  • 可以从shiny执行(我认为如果满足上述条件就可以了)
  • can be run from commandline via Rscript
  • as above but scheduled via cron
  • main method for setting up working directory is set_here() - executed once from console and then the folder is portable because the .here file is included on the zipped directory.
  • does not need Rstudio - hence do not want to do R-projects
  • can also be run interactively from Rstudio (development)
  • can be executed from shiny (I assume that will be OK if the above conditions are met)

我特别不想创建Rstudio项目,因为在我看来它需要安装和使用Rstudio,但是我希望我的脚本尽可能地可移植并且可以在资源贫乏,无头的平台上运行.

I specifically do not want to create Rstudio projects because in my view it necessitates to install and use Rstudio, but I want my scripts to be as portable as possible and run on low resource, headless platforms.

让我们假设工作目录为myGoodScripts,如下所示:

Let us assume the working directory will be myGoodScripts as follows:

/Users/john/src/myGoodScripts/

开始开发时,我将使用setwd()转到上述目录并执行set_here()以创建.here文件.然后有2个脚本dataFetcherMailer.RdataFetcher.Rmd和一个子目录bkp:

when starting development I would go to the above directory with setwd() and execute set_here() to create .here file. Then there are 2 scripts dataFetcherMailer.R, dataFetcher.Rmd and a subdirectory bkp:

library(here)
library(knitr)

basedir <- here()
# this is where here should give path to .here file

rmarkdown::render(paste0(basedir,"/dataFetcher.Rmd"))

# email the created report
# email_routine_with_gmailr(paste0(basedir,"dataFetcher.pdf"))
# now substituted with verification that a pdf report was created
file.exists(paste0(basedir,"/dataFetcher.pdf"))

dataFetcher.Rmd

---
title: "Data collection control report"
author: "HAL"
date: "`r Sys.Date()`"
output: pdf_document
---

```{r setup, include=FALSE}
library(knitr)
library(here)

basedir <- here()

# in actual program this reads data from a changing online data source
df.main <- mtcars

# data backup
datestamp <- format(Sys.time(),format="%Y-%m-%d_%H-%M")
backupName <- paste0(basedir,"/bkp/dataBackup_",datestamp,"csv.gz")
write.csv(df.main, gzfile(backupName))
```

# This is data collection report

Yesterday's data total records: `r nrow(df.main)`. 

The basedir was `r basedir`

The current directory is `r getwd()`

The here path is `r here()`

我想报告中的最后3行会匹配.即使getwd()与其他两个都不匹配,也没关系,因为here()将确保绝对的基本路径.

The last 3 lines in the report would be matching, I guess. Even if getwd() does not match the other two, it should not matter, because here() would ensure an absolute basepath.

当然-上面的方法不起作用.仅当我从相同的myGoodScripts/目录执行Rscript ./dataFetcherMailer.R时,它才起作用.

Of course - the above does not work. It only works if I execute Rscript ./dataFetcherMailer.R from the same myGoodScripts/ directory.

我的目标是了解如何执行脚本,以便相对于脚本位置解析相对路径,并且可以独立于当前工作目录从命令行运行脚本.现在,仅当我对包含脚本的目录执行cd时,才可以从bash运行此命令.如果我安排cron执行脚本,则默认的工作目录将是/home/user并且脚本失败.我天真的方法不管外壳的当前工作目录basedir <- here()都应该为文件系统提供一个可以解析相对路径的点,这是行不通的.

My aim is to understand how to execute the scripts so that relative paths are resolved relative to the script's location and the script can be run from commandline independent of the current working directory. I now can run this from bash only if I have done cd to the directory containing the script. If I schedule cron to execute the script the default working directory would be /home/user and script fails. My naive approach that regardless of the shell's current working directory basedir <- here() should give a filesystem point from which relative paths could be resolved is not working.

从Rstudio开始,没有先前的setwd()

From Rstudio without prior setwd()

here() starts at /home/user
Error in abs_path(input) : 
The file '/home/user/dataFetcher.Rmd' does not exist.

如果未将cwd设置为脚本目录,则使用Rscript从bash进行.

From bash with Rscript if cwd not set to the script directory.

$ cd /home/user/scrc
$ Rscript ./myGoodScripts/dataFetcherMailer.R 
here() starts at /home/user/src
Error in abs_path(input) : 
The file '/home/user/src/dataFetcher.Rmd' does not exist.
Calls: <Anonymous> -> setwd -> dirname -> abs_path

如果有人可以帮助我理解和解决此问题,那就太好了.如果存在另一种可靠的方法来设置不带here()的基本路径,我很想知道.最终,从Rstudio执行脚本比了解如何从commandline/cron执行此类脚本要重要得多.

If someone could help me understand and resolve this problem, that would be fantastic. If another reliable method to set basepath without here() exists, I would love to know. Ultimately executing script from Rstudio matters a lot less than understanding how to execute such scripts from commandline/cron.

我对函数进行了一些修改,以便它可以返回文件的文件名或目录.我目前正在尝试对其进行修改,以使其在从 Rstudio 编织.Rmd文件并通过R文件同样运行时可以正常工作.

I modified the function a little so that it could return either filename or directory for the file. I am currently trying to modify it so that it would work when .Rmd file is knitted from Rstudio and equally run via R file.

here2 <- function(type = 'dir') {
  args <- commandArgs(trailingOnly = FALSE)
  if ("RStudio" %in% args) {
    filepath <- rstudioapi::getActiveDocumentContext()$path
  } else if ("interactive" %in% args) {
    file_arg <- "--file="
    filepath <- sub(file_arg, "", grep(file_arg, args, value = TRUE))
  } else if ("--slave" %in% args) {
    string <- args[6]
    mBtwSquotes <- "(?<=')[^']*[^']*(?=')"
    filepath <- regmatches(string,regexpr(mBtwSquotes,string,perl = T))
  } else if (pmatch("--file=" ,args)) {
    file_arg <- "--file="
    filepath <- sub(file_arg, "", grep(file_arg, args, value = TRUE))
  } else {
    if (type == 'dir') {
      filepath <- '.'
      return(filepath)
    } else {
      filepath <- "error"
      return(filepath)
    }
  }
  if (type == 'dir') {
    filepath <- dirname(filepath)
  }  
  return(filepath)
}

但是我发现commandArgs()是从R脚本继承的,即,当.Rmd文档是从script.R编织而成的时,它们仍然是相同的.因此,只能通用使用script.R中的basepath,而不是文件名.换句话说,将此函数放置在.Rmd文件中时,它将指向调用的script.R路径,而不是.Rmd文件路径.

I discovered however that commandArgs() are inherited from the R script i.e. they remain the same for the .Rmd document when it is knit from a script.R. Therefore only the basepath from script.R location can be used universally, not file name. In other words this function when placed in a .Rmd file will point towards the calling script.R path not the .Rmd file path.

此功能的较短版本将更加有用:

The shorter version of this function will therefore be more useful:

here2 <- function() {
  args <- commandArgs(trailingOnly = FALSE)
  if ("RStudio" %in% args) {
    # R script called from Rstudio with "source file button"
    filepath <- rstudioapi::getActiveDocumentContext()$path
  } else if ("--slave" %in% args) {
    # Rmd file called from Rstudio with "knit button"  
    # (if we placed this function in a .Rmd file)
    file_arg <- "rmarkdown::render"
    string <- grep(file_arg, args, value = TRUE)
    mBtwQuotes <- "(?<=')[^']*[^']*(?=')"
    filepath <- regmatches(string,regexpr(mBtwQuotes,string,perl = T))
  } else if ((sum(grepl("--file=" ,args))) >0) {
    # called in some other way that passes --file= argument
    # R script called via cron or commandline using Rscript
    file_arg <- "--file="
    filepath <- sub(file_arg, "", grep(file_arg, args, value = TRUE))
  } else if (sum(grepl("rmarkdown::render" ,args)) >0 ) {
    # Rmd file called to render from commandline with 
    # Rscript -e 'rmarkdown::render("RmdFileName")'
    file_arg <- "rmarkdown::render"
    string <- grep(file_arg, args, value = TRUE)
    mBtwQuotes <- "(?<=\")[^\"]*[^\"]*(?=\")"
    filepath <- regmatches(string,regexpr(mBtwQuotes,string,perl = T))
  } else {
    # we do not know what is happening; taking a chance; could have  error later
    filepath <- normalizePath(".")
    return(filepath)
  }
  filepath <- dirname(filepath)
  return(filepath)
}

.Rmd文件中的

NB:进入文件的包含目录,只要调用normalizePath(".")就足够了-无论您从脚本中调用.Rmd文件,该文件都可以工作,命令行或Rstudio.

NB: from within .Rmd file to get to the containing directory of the file it is enough to call normalizePath(".") - which works whether you call the .Rmd file from a script, commandline or from Rstudio.

推荐答案

您的要求

我认为 here()的行为并不是您真正想要的.相反,您要查找的是确定源文件(即.R文件)的路径.我对here()命令进行了一些扩展,使其表现出您所期望的方式:

what you asked for

The behaviour of here() isn't really what you want here, I think. Instead, what you are looking for is to determine the path of the source file aka the .R file. I extended the here() command a little to behave the way you expect:

here2 <- function() {
  args <- commandArgs(trailingOnly = FALSE)
  if ("RStudio" %in% args) {
    dirname(rstudioapi::getActiveDocumentContext()$path)
  } else {
    file_arg <- "--file="
    filepath <- sub(file_arg, "", grep(file_arg, args, value = TRUE))
    dirname(filepath)
  }
}

关于脚本未在RStudio中运行的情况的想法来自此答案.我通过在dataFetcherMailer.R文件的开头粘贴函数定义来进行尝试.您还可以考虑将其放在主目录中的另一个文件中,然后用例如source("here2.R")而不是library(here)进行调用,或者为此目的可以编写一个小的R包.

The idea for the case when the script is not run in RStudio comes from this answer. I tried this by pasting the function definition at the beginning of your dataFetcherMailer.R file. You could also think about putting this in another file in your home directory and call it with, e.g., source("here2.R") instead of library(here) or you could write a small R package for this purpose.

here2 <- function() {
  args <- commandArgs(trailingOnly = FALSE)
  if ("RStudio" %in% args) {
    # R script called from Rstudio with "source file button"
    filepath <- rstudioapi::getActiveDocumentContext()$path
  } else if ("--slave" %in% args) {
    # Rmd file called from Rstudio with "knit button"  
    # (if we placed this function in a .Rmd file)
    file_arg <- "rmarkdown::render"
    string <- grep(file_arg, args, value = TRUE)
    mBtwQuotes <- "(?<=')[^']*[^']*(?=')"
    filepath <- regmatches(string,regexpr(mBtwQuotes,string,perl = T))
  } else if ((sum(grepl("--file=" ,args))) >0) {
    # called in some other way that passes --file= argument
    # R script called via cron or commandline using Rscript
    file_arg <- "--file="
    filepath <- sub(file_arg, "", grep(file_arg, args, value = TRUE))
  } else if (sum(grepl("rmarkdown::render" ,args)) >0 ) {
    # Rmd file called to render from commandline with 
    # Rscript -e 'rmarkdown::render("RmdFileName")'
    file_arg <- "rmarkdown::render"
    string <- grep(file_arg, args, value = TRUE)
    mBtwQuotes <- "(?<=\")[^\"]*[^\"]*(?=\")"
    filepath <- regmatches(string,regexpr(mBtwQuotes,string,perl = T))
  } else {
    # we do not know what is happening; taking a chance; could have  error later
    filepath <- normalizePath(".")
    return(filepath)
  }
  filepath <- dirname(filepath)
  return(filepath)
}

我认为大多数人真正需要什么

我前一段时间发现了这种方法,但实际上将我的工作流程完全更改为仅使用R Markdown文件(和RStudio项目).优点之一是Rmd文件的工作目录始终是文件的位置.因此,您不必费心设置工作目录,而只需在脚本中编写相对于Rmd文件位置的所有路径.

what I think most people actually need

I found this way a while ago but then actually changed my workflow entirely to only use R Markdown files (and RStudio projects). One of the advantages of this is that the working directory of Rmd files is always the location of the file. So instead of bothering with setting a working directory, you can just write all paths in your script relative to the Rmd file location.

---
title: "Data collection control report"
author: "HAL"
date: "`r Sys.Date()`"
output: pdf_document
---

```{r setup, include=FALSE}
library(knitr)

# in actual program this reads data from a changing online data source
df.main <- mtcars

# data backup
datestamp <- format(Sys.time(),format="%Y-%m-%d_%H-%M")

# create bkp folder if it doesn't exist
if (!dir.exists(paste0("./bkp/"))) dir.create("./bkp/")

backupName <- paste0("./bkp/dataBackup_", datestamp, "csv.gz")
write.csv(df.main, gzfile(backupName))
```

# This is data collection report

Yesterday's data total records: `r nrow(df.main)`. 

The current directory is `r getwd()`

请注意,以./开头的路径意味着要在Rmd文件的文件夹中开始. ../表示您上一级. ../../您进入两个级别,依此类推.因此,如果您的Rmd文件位于名为"scripts"的文件夹中,在您的根文件夹中,并且您想将数据保存在名为"data"的文件夹中.在根文件夹中,输入saveRDS(data, "../data/dat.RDS").

Note that paths starting with ./ mean to start in the folder of the Rmd file. ../ means you go one level up. ../../ you go two levels up and so on. So if your Rmd file is in a folder called "scripts" in your root folder, and you want to save your data in a folder called "data" in your root folder, you write saveRDS(data, "../data/dat.RDS").

您可以使用Rscript -e 'rmarkdown::render("/home/johannes/Desktop/myGoodScripts/dataFetcher.Rmd")'从命令行/cron运行Rmd文件.

You can run the Rmd file from command line/cron with Rscript -e 'rmarkdown::render("/home/johannes/Desktop/myGoodScripts/dataFetcher.Rmd")'.

这篇关于R脚本中的here()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆