简体   繁体   English

从R脚本在excel中打开CSV文件

[英]Open CSV file in excel from R script

I have generated a csv file in R and would like my script to open this file in excel so that someone with no knowledge of R can modify the data. 我已经在R中生成了一个csv文件,并希望我的脚本在excel中打开该文件,以便不了解R的人可以修改数据。 (Ideally when I run the script excel opens and loads this file). (理想情况下,当我运行脚本时,excel将打开并加载该文件)。 What is the best way to accomplish this? 做到这一点的最佳方法是什么?

The cmd command to open an excel file is open excel file.xlsx . 打开excel文件的cmd命令是open excel file.xlsx In R you can run the command line using system . R您可以使用system运行命令行。 So putting these together: 因此,将它们放在一起:

excel_file_with_path <- "C:/Users/myusername/Documents/file.xlsx"
command <- paste("open excel", excel_file_with_path)
system(command)

The accepted answer does not work for me, although not sure why (Windows OS), and I keep having to search for this answer again and again- so here is the answer that works for future reference. 接受的答案对我不起作用,尽管不确定为什么(Windows OS),并且我一直不得不一次又一次地搜索此答案-因此,这里的答案可供将来参考。

shell.exec(here::here("filename.csv"))

This should open any file with the default program. 这应该使用默认程序打开任何文件。 credit: answer lifted directly from Uwe Ligges outside of SO 信用: SO之外的 Uwe Ligges直接提出了答案

(note: my use of the here::here() function is just an easy way to create a complete filepath, referenced to your RStudio project directory) (注意:我对here::here()函数的使用只是创建完整文件路径的简单方法,该文件路径引用了您的RStudio项目目录)

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

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