0

Using R trying to print a sheet in xls output format. Trying to get the year name in dynamically using a parameter in R. Using this parameter trying to rename the Excel sheet year. But glue string is printing instead of parameter.

if (!require("librarian"))install.packages("librarian")
librarian::shelf(stringr,stringi,dplyr,glue,openxlsx)
         

Year <-2021
Output_Path <-"C:\\Z_R_Stuff\\"
Excel_Output_List <- list('glue({Year}) MTCARS are Great'=mtcars,'IRIS'=iris)
File_Name <- "Test_Output_Excel"           

write.xlsx(Excel_Output_List, paste0(Output_Path,File_Name,".xlsx"))
    Output Issue
    Error Output:
    Sheet Name : glue({Year}) MTCARS are Great          
    Expected Output:
    Sheet Name : 2021 MTCARS are Great
BobbyG
  • 45
  • 5

2 Answers2

1

Try this:

Excel_Output_List <- list(glue('{Year} MTCARS are Great')=mtcars,'IRIS'=iris)
XixiHaha
  • 138
  • 5
  • The above code is not working, still getting the same output ,sheet name as glue({Year}) MTCARS are Great – BobbyG Jul 29 '22 at 08:14
1

Sorry for that, try this:

Year <-2021
Output_Path <-"C:\\Z_R_Stuff\\"
Excel_Output_List <- list(mtcars,iris)
names(Excel_Output_List) <- c(glue('{Year} MTCARS are Great'), 'IRIS')
File_Name <- "Test_Output_Excel"

write.xlsx(Excel_Output_List, paste0(Output_Path,File_Name,".xlsx"))
XixiHaha
  • 138
  • 5
  • You are welcome. I am really sorry for that it did not work the first time because I did not run the code. In this community I should be responsible for my put. But luckily, I corrected my mistake and it helped you. Thank you too. – XixiHaha Jul 29 '22 at 09:04