Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:04:10+00:00 2026-06-10T19:04:10+00:00

From this post I got a script that exports a list as separate worksheets

  • 0

From this post I got a script that exports a list as separate worksheets in an Excel file (code as below). Now I would like to wrap it in a convenient function to reproduce this behavior by providing the input list name and output file name.

Sample data:

var1 <- list('2003' = 1:3, '2004' = c(4:3), '2005' = c(6,4,1), '2006' = 1:4 )

Current script:

require("XLConnect")
wb <- loadWorkbook("var1.xlsx", create = TRUE)
createSheet(wb, names(var1))
writeWorksheet(wb, var1, names(var1),header=FALSE)
saveWorkbook(wb)

Disclaimer: While I blush to ask such a simple question, I’m sure many other visitors to SO will love to find this information here ;7)

EDIT :: ready-to-use function:

save.excel <-function(.list, default = 'var1', path = ''){
    require("XLConnect")
    .name <- as.list(match.call())[2]
    if(is.language(.name[[1]])) wb_name <- paste0(paste0(path, default, collapse = '/'), '.xlsx')
    if(is.symbol(.name[[1]])) wb_name <- paste0(paste0(path, as.character(.name), collapse = '/'), '.xlsx')
    wb <- loadWorkbook(wb_name, create = TRUE)
    createSheet(wb, names(.list))
    writeWorksheet(wb,.list, names(.list),header=FALSE)
    saveWorkbook(wb)
    }

The only difference from the solution below is that I added XLConnect as a library requested from inside the function, in case you didn’t do it manually before ;7)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:04:11+00:00Added an answer on June 10, 2026 at 7:04 pm

    This is untested, as XLConnect will not install on my machine. but something like the following might work

    Simple approach

    A function with two arguments

    • my_list – the list which you wish to export elements of as separate worksheets
    • wb_name – the name of the workbook

    The function looks like this

    write_list <-function(my_list, wb_name = 'var1.xlsx') {    
      wb <- loadWorkbook(wb_name, create = TRUE)
      createSheet(wb, names(my_list))
      writeWorksheet(wb, my_list, names(my_list),header=FALSE)
      saveWorkbook(wb)
     }
    

    fancy option that will use the name of the list

    If you want to use the name of the list to create the file then you can have fun with
    match.call, is.symbol and is.language. Details of why you do this are below

    write_list_name <-function(.list, default = 'var1', path = ''){
      .name <- as.list(match.call())[2]
       if(is.language(.name[[1]])){
         wb_name <- sprintf("%s/%s.xlsx", path, default)
       }
       if(is.symbol(.name[[1]])) {
        wb_name <- sprintf("%s/%s.xlsx", path, as.character(.name))
       }
      wb <- loadWorkbook(wb_name, create = TRUE)
      createSheet(wb, names(.list))
      writeWorksheet(wb,.list, names(.list),header=FALSE)
      saveWorkbook(wb)
      }
    

    the is.language / is.symbol /match.call deal with the two situations

    write_list_name(var1)
    
    #in which case .name[[1]] is the symbol var1
    

    and

    write_list_name(list(n=2:3))
    # in which case .name[[1]] is list(n=2:3), and class language 
    # a file called list(n=2:3).xlsx would be not ideal, hence the `default` argument.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this ajax_update script that updates file.php every 60 seconds.. Now file.php outputs
I got this script from here $(document).ready(function() { $('#choose').change(function(event) { $.post('select-ajax.php', { selected: $('#choose').val()
From this post : I would first suggest that you optimize your dependentObservable (a.k.a.
I have learnt from this post that always use <a> tags or <button> tags
This is a separate question stemming from this post: How to use the filename
Should be an easy one. I thought, from reading this blog post that I
I've got a post-commit hook script that performs a SVN update of a working
I've got a JSON string that I'm posting to my Python script. This is
I have a php script that is triggered from my app. This script uses
i got this SQL query where post_title taken from $_GET $sql = SELECT ID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.