ropenxlsxr-glue

Error while trying to append Year in the XLSX sheet using glue function in R Programming


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

Solution

  • 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"))