R: Insert new rows of data into a LabKey Server

labkey.insertRows {Rlabkey} | R Documentation

## Insert new rows of data into a LabKey Server

### Description

Insert new rows of data into the database.

### Usage

    labkey.insertRows(baseUrl, folderPath,
        schemaName, queryName, toInsert, na)

### Arguments

`baseUrl` | a string specifying the `baseUrl`for the labkey server
`folderPath` | a string specifying the `folderPath`
`schemaName` | a string specifying the `schemaName` for the query
`queryName` | a string specifying the `queryName`
`toInsert` | a data frame containing rows of data to be inserted
`na` | (optional) the value to convert NA's to, defaults to NULL

### Details

A single row or multiple rows of data can be inserted. The `toInsert` data frame must contain values for each column in the dataset and must be created with the `stringsAsFactors` option set to FALSE. The names of the data in the data frame must be the column names from the LabKey Server.To insert a value of NULL, use an empty string ("") in the data frame (regardless of the database column type). Also, when inserting data into a study dataset, the sequence number must be specified..

### Value

A list is returned with named categories of command, rowsAffected, rows, queryName, containerPath and schemaName. The schemaName, queryName and containerPath properties contain the same schema, query and folder path used in the request. The rowsAffected property indicates the number of rows affected by the API action. This will typically be the same number as passed in the request. The rows property contains a list of row objects corresponding to the rows inserted.

### Author(s)

Valerie Obenchain

### See Also

`labkey.selectRows`, `labkey.executeSql`, `makeFilter`, `labkey.importRows`, `labkey.updateRows`,
`labkey.deleteRows`

### Examples

    ## Insert, update and delete
    ## Note that users must have the necessary permissions in the database
    ## to be able to modify data through the use of these functions
    # library(Rlabkey)

    newrow <- data.frame(
    	DisplayFld="Inserted from R"
    	, TextFld="how its done"
    	, IntFld= 98
    	, DoubleFld = 12.345
    	, DateTimeFld = "03/01/2010"
    	, BooleanFld= FALSE
    	, LongTextFld = "Four score and seven years ago"
    #	, AttachmentFld = NA    #attachment fields not supported
    	, RequiredText = "Veni, vidi, vici"
    	, RequiredInt = 0
    	, Category = "LOOKUP2"
    	, stringsAsFactors=FALSE)

    insertedRow <- labkey.insertRows("http://localhost:8080/labkey",
        folderPath="/apisamples", schemaName="lists", queryName="AllTypes",
        toInsert=newrow)
    newRowId <- insertedRow$rows[[1]]$RowId

    selectedRow<-labkey.selectRows("http://localhost:8080/labkey",
        folderPath="/apisamples", schemaName="lists", queryName="AllTypes",
        colFilter=makeFilter(c("RowId", "EQUALS", newRowId)))
    updaterow=data.frame(
    	RowId=newRowId
    	, DisplayFld="Updated from R"
    	, TextFld="how to update"
    	, IntFld= 777
    	, stringsAsFactors=FALSE)

    updatedRow <- labkey.updateRows("http://localhost:8080/labkey",
        folderPath="/apisamples", schemaName="lists", queryName="AllTypes",
        toUpdate=updaterow)
    selectedRow<-labkey.selectRows("http://localhost:8080/labkey",
        folderPath="/apisamples", schemaName="lists", queryName="AllTypes",
        colFilter=makeFilter(c("RowId", "EQUALS", newRowId)))

    deleterow <- data.frame(RowId=newRowId, stringsAsFactors=FALSE)
    result <- labkey.deleteRows(baseUrl="http://localhost:8080/labkey",
        folderPath="/apisamples", schemaName="lists", queryName="AllTypes",
        toDelete=deleterow)

[Package Rlabkey version 2.4.0 Index]