在R中,通过shell.exec打开一个保存到Excel的对象

我希望能够在保存后在Excel中快速打开文件。 我从R开始学习,在SO上使用shell.exec 1在excel工作簿中打开一个特定的工作表

在我的Windows系统上,我可以用下面的代码来完成,也许可以把它变成一个函数:saveOpen <_ function {…。 但是,我怀疑有更好的方法来实现这个温和的目标。

我将不胜感激任何改善这一多步骤工作的build议。

# create tiny data frame df <- data.frame(names = c("Alpha", "Baker"), cities = c("NYC", "Rome")) # save the data frame to an Excel file in the working directory save.xls(df, filename "test file.xlsx") # I have to reenter the file name and add a forward slash for the paste() command below to create a proper file path name <- "/test file.xlsx" # add the working directory path to the file name file <- paste0(getwd(), name) # with shell and .exec for Windows, open the Excel file shell.exec(file = file) 

你只是想创build一个帮手function,使这更容易? 怎么样

 save.xls.and.open <- function(dataframe, filename, ...) { save.xls(df, filename=filename, ...) cmd <- file.path(getwd(), filename) shell.exec(cmd) } 

那么你只是跑步

 save.xls.and.open(df, filename ="testfile.xlsx") 

我想这似乎不是那么多步骤对我来说。