Jump to content
  • Write back to database from Spotfire using R Data Functions


    R and Python are great programming languages that can be leveraged with TIBCO Spotifre via Data Functions. R Data functions have been around for some time while Python data functions were introduced in version 10.7. These languages contain many packages. Some of these packages makes it easy to write back to databases, images, files or connect to data sources. In this example we are going to connect to a ODBC data source and perform SQL queries to insert data back to the database.

     

    Introduction

    R and Python are great programming languages that can be leveraged with TIBCO Spotfire via Data Functions. R Data functions have been around for some time while Python data functions were introduced in version 10.7. These languages contain many packages. Some of these packages makes it easy to write back to databases, images, files or connect to data sources. In this example we are going to connect to a ODBC data source and perform SQL queries to insert data back to the database.

    aa.thumb.gif.1868ce63a9a5bdc850766c8596cc6b95.gif

    Setup a System DSN 

    Whether you are in Linux, Windows or Mac, setup a System Data Source Name (DSN). You will need the proper driver for your databas. In this example we are usin PostgreSQL

    systemdsn.png.bc8add3ed22d306985139c8f23081465.png

    Create R Data functions

    Install required packages 

    For the R data function to run, you need to install odbc and DBI packages using TERR tools. TERR tools are available from the Tools menu. If you do not see this option, ask your system administrator to enable it. 

    rpackages.png.605a277db7102d308d812a7146e93a0f.png

    Register a new data function from Tools > Register data functions... and make sure packages are properly installed by running an empty script:

    registerr.png.f2608add9877cd980494739aada17994.png

    Close the parameters window and make sure the empty script did not throw any errors. Look at the top right bell  for errors.  If you see a bell with a red dot circle, it means the packages are not properly declared or installed. If there are no errors, click [No] on the Register Data Function promp to embed the data function in the analysis. We can write to the library later. 

    dialog.png.f0889707f2bbfb7e28f19d0f3a50f7f6.png

    Edit the R data function

    Go to Data > Data Function Properties... and edit the script and setup proper input parameters. This function has no outputs.

     

    odbcr.png.e3b51141fbbd908da149267d00c1af35.png

    #prepare SQL statement
    dt<-Sys.time()
    sql = sprintf("INSERT INTO rating(dt, name, value) VALUES ('%s', '%s', %s)",dt,comments,rating)
    
    #execute insert statement
    con <- DBI::dbConnect(odbc::odbc(), dsn="PostgreSQL35W")
    dbExecute(con, SQL)
     

     

    To run the data function, add a button on a text area and link the data function to it

    executer.png.b605d7e71324d32b845a6adbed2a6457.png

    If you need to see the values right after inserting the values in the database, add the datetime variable (dt) as an output parameter to the R script to point to a DateTime document property (dateR)

    Then make the datatable setup on demand and set the data table to load automatically by setting the date column as a Range. Leave Min blank and Max to point to the RDate document property. This will update the database with all the records up to date.

    ondemand.png.1d7c3f8e79fb48c7d9fdeccb38a1d7d8.png

    ondemand2.png.394fd3a7643d43e06f1eb8b653500912.png

    See also

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...