Example 2: Working with R script - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-04-03
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

In this example, the R script mapper.R reads in a line of text input ("Old Macdonald Had A Farm") from csv and splits the line into individual words, emitting a new row for each word.

  • Create teradataml DataFrame object.
    >>> barrierdf = DataFrame.from_table("barrier")
  • Create remote user environment.
    >>> testenv = create_env('test_env_for_r', 'r_4.1', 'Demo environment')
    User environment test_env_for_r created.
    >>> import os, teradataml
  • Install file in remote user environment.
    >>> testenv.install_file(file_path=os.path.join(os.path.dirname(teradataml.__file__), "data", "scripts", "mapper.R"))
    File 'mapper.R' installed successfully in the remote user environment 'test_env_for_r'.
  • Create an APPLY object that allows user to run script.
    >>> apply_obj = Apply(data=barrierdf,
                          apply_command='Rscript --vanilla mapper.R',
                          data_order_column="Id",
                          is_local_order=False,
                          nulls_first=False,
                          sort_ascending=False,
                          returns={"word": VARCHAR(15), "count_input": VARCHAR(10)},
                          env_name=testenv,
                          delimiter='\t')
  • Run the user script in Open Analytics Framework
    >>> apply_obj.execute_script()
            word count_input
    0  Macdonald           1
    1          A           1
    2       Farm           1
    3        Had           1
    4        Old           1
    5          1           1
  • Remove the installed file from remote user environment.
    >>> apply_obj.remove_file(file_name='mapper.R')
    File 'mapper.R' removed successfully from the remote user environment 'test_env_for_r'.