![](/img/trans.png)
[英]R - "Error in file(file, ifelse(append, "a", "w")) : cannot open the connection"
[英]How to fix Error in file(file, ifelse(append, “a”, “w”)) : cannot open the connection in an R Notebook
我目前正在學習 R 筆記本電腦,但我遇到了路徑問題。 我制作了一個 R 項目,並將我的所有腳本和文件托管在包含 R 項目的文件夾中。 我正在嘗試運行:
接下來,安裝庫:
library(tidyverse)
library(CHNOSZ)
library(janitor)
然后使用以下命令導入和合並文件:
d <-list.files(path="./merge", pattern="*.csv", full.names = TRUE) %>%
map_df(~read_csv(., col_types = cols(.default = "c")))
d
結果是一個 0 行的小標題。 還,
write.csv(d,file="data_generated/FinalmergeAllCompounds_comb.csv")
拋出錯誤 Error in file(file, ifelse(append, "a", "w")): cannot open the connection
當我在 Rstudio 的同一個項目中將其作為普通腳本運行時,該腳本工作得很好。 我合並了.csv文件沒有問題,我可以將它導出回我的項目目錄,沒有問題。
這似乎是 RNotebook 和文件路徑之間的沖突。 我該如何解決?
問題的答案是添加:
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
根據 csgroen 的評論和這篇文章Setting work directory in knitr using opts_chunk$set(root.dir =...) 到筆記本腳本的頂部不起作用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.