You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Shiny生产环境无法读取CSV文件,执行终止问题求助

R Shiny部署后读取CSV文件失败的解决方法

问题描述

开发的R Shiny应用在R Studio本地运行正常,但部署到生产环境后,读取CSV文件环节报错。具体报错信息:

here() starts at /home/production/workspace.git
Warning in file(file, "rt") :
  cannot open file '/home/production/workspace.git/Shiny_App_for_CFM/status_var_list.csv': No such file or directory
Error in file(file, "rt") : cannot open the connection
Calls: runApp ... eval -> ..stacktraceon.. -> read.csv -> read.table -> file
Execution halted

读取CSV的代码如下:

library(here)
status_var <- read.csv(here('Shiny_App_for_CFM/status_var_list.csv'))
status_var <- data.frame(status_var)

问题原因

  • here()包依赖本地的.Rproj文件识别项目根目录,生产环境通常没有这个文件,导致它错误地将/home/production/workspace.git当作根目录,和实际文件路径不匹配。
  • 本地开发的路径结构和生产环境部署路径不一致,相对路径的引用逻辑失效。

解决方法

方法1:用Shiny适配性更强的文件路径函数

直接使用system.file(),它能自动识别应用部署后的文件位置:

status_var <- read.csv(system.file("status_var_list.csv", package = "Shiny_App_for_CFM"))

如果CSV文件和你的app.R在同一目录下,也可以直接用最简相对路径:

status_var <- read.csv("status_var_list.csv")

方法2:手动指定here()的根目录

如果一定要用here(),可以在生产环境中手动设置正确的根目录:

library(here)
# 替换为你的Shiny应用在生产环境的实际根目录
set_here("/home/production/workspace.git/Shiny_App_for_CFM")
status_var <- read.csv(here("status_var_list.csv"))

方法3:排查文件部署情况

  • 确认部署时status_var_list.csv已经被完整上传到生产环境对应目录,没有被遗漏。
  • 可以在代码中添加调试语句,打印当前工作目录,验证路径是否正确:
print(getwd())

根据打印出的路径,调整CSV文件的读取路径。

内容的提问来源于stack exchange,提问作者FRECEENA FRANCIS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 19:30:37