Menu Close

Working with SyncroSim folders using the rsyncrosim package

Syncrosim Forums SyncroSim – General Questions and Answers Working with SyncroSim folders using the rsyncrosim package

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #20794
    leonardo-fridleonardo-frid
    Keymaster

    A user recently asked about creating and managing SyncroSim library folders using the rsyncrosim package for R. Currently there are no functions for doing this with the rsyncrosim package. However, it is possible to do this via command line calls. The following shows a simple example that creates a folder in a library and places a scenario within that folder:


    #Load R libraries
    library(rsyncrosim)

    # Connect to a SyncroSim Library -----------------------------------------------
    # Load session
    mySession <- session()

    # Load ST-Sim non-spatial example Library
    myLibrary <- ssimLibrary(name = "folderDemo.ssim",
    session = mySession,
    package = "stsim",
    template = "non-spatial-example")

    # View current Scenarios in the Library
    scenarioDataframe <- scenario(myLibrary)
    scenarioDataframe

    #Make a console call to create a new example folder----------------------------
    # Find the Parent Project ID to create a folder within this Project
    pid <- scenarioDataframe$projectId[1]

    # Write the console command
    command <- paste0("\"", filepath(mySession), "/SyncroSim.Console.Exe\"",
    " --create --folder --lib=", filepath(myLibrary),
    " --name=example --tpid=", pid)

    # View the console command
    print(command)

    # Invoke a system command
    sysOut <- system(command, intern=TRUE)
    print(sysOut)

    # Grab the folder ID as a variable
    folderId <- strsplit(sysOut, ": ")[[1]][2]
    print(folderId)

    # Make a console call to move a Scenario to the folder--------------------------
    # Pick the first Scenario in the Library to move
    sid <- scenarioDataframe$scenarioId[1]

    # Write the console command; tfid is the folder ID flag
    command <- paste0("\"", filepath(mySession), "/SyncroSim.Console.Exe\"",
    " --move --scenario --lib=", filepath(myLibrary), " --sid=",
    sid, " --tfid=", folderId, " --tpid=", pid)

    # View the console command
    print(command)

    # Invoke a system command
    sysOut <- system(command, intern=TRUE)

    # If you open folderDemo.ssim in the SyncroSim UI, there should now be an
    # "example" folder within the Definitions Project, containing the first scenario

    #21922
    crosbya1crosbya1
    Participant

    Thanks for this post Leonardo. Is there a way to find the folder ID from within R after the fact? For example, when creating a new sub-scenario at a later date? I’ve been digging around, but I can’t find a way to get a list of folders within a project, or to find the Folder ID from within the SyncroSim UI.

    #21923
    katie-birchardkatie-birchard
    Keymaster

    Unfortunately, the rsyncrosim and pysyncrosim packages do not have built-in folder functions yet. However, being able to create, modify, and access folders in SyncroSim using rsyncrosim and pysyncrosim will become available in an upcoming release. In the meantime, you can use the SyncroSim console commands from R to retrieve a dataframe of existing folder IDs and other folder data.

    Here is a function that uses the SyncroSim console commands to retrieve folder data that you can implement in your script. Note that you will need rsyncrosim loaded to use this function.

    
    library(rsyncrosim)
    
    retrieveFolderData <- function(libpath){
      args <- list(list = NULL, folders = NULL, lib = libpath, csv=NULL)
      tt <- command(args)
      con <- textConnection(tt)
      out <- read.csv(con)
      
      return(out) 
    }
    
    retrieveFolderData("path/to/my/library.ssim")
    

    Similarly, in python you can add this function to your script to retrieve folder data. Note that you will need to import the pysyncrosim and io python packages to use this function.

    
    import pysyncrosim as ps
    import io
    
    def retrieve_folder_data(libpath):
        my_session = ps.Session()
        args = ["--list", "--folders", "--lib=%s" % libpath]
        tt = my_session._Session__call_console(args, decode=True, csv=True)
        out =  pd.read_csv(io.StringIO(tt))
    
        return out
    
    retrieve_folder_data("path/to/my/library.ssim")
    
    • This reply was modified 11 months, 1 week ago by katie-birchardkatie-birchard. Reason: Added python example
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.