-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathR Runner.step
1 lines (1 loc) · 23.6 KB
/
R Runner.step
1
{"creationTimeStamp":"2023-08-18T03:21:53.402Z","modifiedTimeStamp":"2023-08-22T02:55:06.785Z","createdBy":"viya_admin","modifiedBy":"viya_admin","name":"R Runner.step","displayName":"R Runner.step","localDisplayName":"R Runner.step","properties":{},"links":[{"method":"GET","rel":"self","href":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","uri":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","type":"application/vnd.sas.data.flow.step"},{"method":"GET","rel":"alternate","href":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","uri":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","type":"application/vnd.sas.data.flow.step.summary"},{"method":"GET","rel":"up","href":"/dataFlows/steps","uri":"/dataFlows/steps","type":"application/vnd.sas.collection","itemType":"application/vnd.sas.data.flow.step.summary"},{"method":"PUT","rel":"update","href":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","uri":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","type":"application/vnd.sas.data.flow.step","responseType":"application/vnd.sas.data.flow.step"},{"method":"DELETE","rel":"delete","href":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","uri":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6"},{"method":"GET","rel":"transferExport","href":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","uri":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","responseType":"application/vnd.sas.transfer.object"},{"method":"PUT","rel":"transferImportUpdate","href":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","uri":"/dataFlows/steps/8184bc08-d196-4a7a-9fd6-882052e401d6","type":"application/vnd.sas.transfer.object","responseType":"application/vnd.sas.summary"}],"metadataVersion":0.0,"version":2,"type":"code","flowMetadata":{"inputPorts":[{"name":"inputtable","displayName":"inputtable","localDisplayName":"inputtable","minEntries":0,"maxEntries":1,"defaultEntries":0,"type":"table"}],"outputPorts":[{"name":"envData","displayName":"envData","localDisplayName":"envData","minEntries":0,"maxEntries":1,"defaultEntries":0,"type":"table","supportsView":false,"requiresStructure":false},{"name":"outputtable","displayName":"outputtable","localDisplayName":"outputtable","minEntries":0,"maxEntries":1,"defaultEntries":0,"type":"table","supportsView":false,"requiresStructure":false}]},"ui":"{\n\t\"showPageContentOnly\": true,\n\t\"pages\": [\n\t\t{\n\t\t\t\"id\": \"parameters\",\n\t\t\t\"type\": \"page\",\n\t\t\t\"label\": \"Parameters\",\n\t\t\t\"children\": [\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"text1\",\n\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\"text\": \"\",\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"inputtable\",\n\t\t\t\t\t\"type\": \"inputtable\",\n\t\t\t\t\t\"label\": \"Provide an input dataset:\",\n\t\t\t\t\t\"required\": false,\n\t\t\t\t\t\"placeholder\": \"\",\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"text2\",\n\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\"text\": \"Note: If you have attached input data to the \\\"inputtable\\\" input port of this custom step, ensure you refer to the same as r_input_table within your R script.\",\n\t\t\t\t\t\"visible\": \"$inputtable\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"inputRScript\",\n\t\t\t\t\t\"type\": \"path\",\n\t\t\t\t\t\"label\": \"Provide your R script location:\",\n\t\t\t\t\t\"pathtype\": \"file\",\n\t\t\t\t\t\"placeholder\": \"/path/to/your/R/Script\",\n\t\t\t\t\t\"required\": false,\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"rrrarea\",\n\t\t\t\t\t\"type\": \"textarea\",\n\t\t\t\t\t\"label\": \"Run an R snippet:\",\n\t\t\t\t\t\"placeholder\": \"\",\n\t\t\t\t\t\"required\": false,\n\t\t\t\t\t\"visible\": \"!$inputRScript\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"output_rdf_name\",\n\t\t\t\t\t\"type\": \"textfield\",\n\t\t\t\t\t\"label\": \"Provide name of R data frame you wish to output:\",\n\t\t\t\t\t\"placeholder\": \"\",\n\t\t\t\t\t\"required\": false,\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"text3\",\n\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\"text\": \"Note: If you are using this step, ensure that the R dataframe you refer does exist in the R session.\",\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"envData\",\n\t\t\t\t\t\"type\": \"outputtable\",\n\t\t\t\t\t\"label\": \"Provide output dataset for R environment variables:\",\n\t\t\t\t\t\"required\": false,\n\t\t\t\t\t\"placeholder\": \"WORK.__TEMP_R_ENV_VARIABLES\",\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"outputtable\",\n\t\t\t\t\t\"type\": \"outputtable\",\n\t\t\t\t\t\"label\": \"Provide desired output table:\",\n\t\t\t\t\t\"required\": false,\n\t\t\t\t\t\"placeholder\": \"\",\n\t\t\t\t\t\"visible\": \"$output_rdf_name\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"id\": \"config_tab\",\n\t\t\t\"type\": \"page\",\n\t\t\t\"label\": \"Configuration\",\n\t\t\t\"children\": [\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"rHomePath\",\n\t\t\t\t\t\"type\": \"textfield\",\n\t\t\t\t\t\"label\": \"Enter path to R_HOME:\",\n\t\t\t\t\t\"placeholder\": \"/opt/sas/viya/home/sas-pyconfig/default_r/lib64/R\",\n\t\t\t\t\t\"required\": false,\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"id\": \"about_tab\",\n\t\t\t\"type\": \"page\",\n\t\t\t\"label\": \"About\",\n\t\t\t\"children\": [\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"about_text\",\n\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\"text\": \"R Runner\\n=======\\n\\nR Runner enables you to submit R scripts from within SAS Studio and develop integrated analytics pipelines.\\n\\nYou can submit either:\\n\\n1. Short R snippets in the text area provided\\n\\n2. R scripts (programs) through a file selector\\n\\nVariables created within the R script (also known as R environment variables) can also be written to an output SAS dataset for reference and reuse in downstream code. \\n\\nIn this early version, you'll also be able to attach an input table (SAS dataset) to the custom step, which will be converted to an R data frame (via Python) for analysis in the R script.\\n\\n\",\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"early_release_notes\",\n\t\t\t\t\t\"type\": \"section\",\n\t\t\t\t\t\"label\": \"IMPORTANT NOTE for this early version (please expand!)\",\n\t\t\t\t\t\"open\": false,\n\t\t\t\t\t\"visible\": \"\",\n\t\t\t\t\t\"children\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"id\": \"early_release_text\",\n\t\t\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\t\t\"text\": \"This is the first release of R Runner and facilitates the basic task of executing R scripts within SAS Studio. We'll consider additional improvements in future.\\n\\nTo set correct expectations,\\n\\n- R Runner should NOT be considered an R editor / IDE for SAS Studio (though it's tempting for us, the creators, to label it so :)). \\n\\n- Given the recency of this step, there are bound to be teething troubles. We appreciate your patience (and even more so, suggestions and contributions). \\n\\n- While this may seem obvious, do not expect syntax checks, highlighting or other convenience you are used to with R interfaces.\",\n\t\t\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"about_parameters\",\n\t\t\t\t\t\"type\": \"section\",\n\t\t\t\t\t\"label\": \"Parameters\",\n\t\t\t\t\t\"open\": false,\n\t\t\t\t\t\"visible\": \"\",\n\t\t\t\t\t\"children\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"id\": \"parameters_text\",\n\t\t\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\t\t\"text\": \"This custom step accesses an R interpreter through a Python package (rpy2). Therefore, ensure:\\n\\n1. SAS Viya has access to an active Python (version >=3.7) and R ( >= 4.0 ) environment. Proc Python makes use of this Python environment.\\n\\n2. The rpy2 Python package is installed and configured. Refer documentation for details on rpy2. Recommended rpy2 details (on which this step is based): 3.5.13\\n\\n3. A path to R is available through the R_HOME environment variable.\\n\\n4. Preferable / recommended: Administrators could make use of the SAS Configurator for Open Source (also commonly known as sas-pyconfig) to install and configure Python and R access from SAS Viya. Refer SAS Viya Deployment Guide (monthly stable 2023.08 onwards) for instructions on the same. Documentation provided below.\\n\\n5. If you want this step to interact with input / output data, the pandas Python package along with the SAS callback method is used under the covers to convert the table to a pandas dataframe (and then to R). Pandas should be installed.\",\n\t\t\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"id\": \"parameters_input\",\n\t\t\t\t\t\t\t\"type\": \"section\",\n\t\t\t\t\t\t\t\"label\": \"Input Parameters\",\n\t\t\t\t\t\t\t\"open\": true,\n\t\t\t\t\t\t\t\"visible\": \"\",\n\t\t\t\t\t\t\t\"children\": [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\"id\": \"input_parameters_text\",\n\t\t\t\t\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\t\t\t\t\"text\": \"1. Input Data (input port, optional): attach an input table (SAS dataset) to this port to facilitate analysis in R. You should refer this input dataset as r_input_table inside the R script. \\n\\n2. Input R Script (file selector, optional): attach an R script which you wish to execute.\\n\\n3. R Snippet (text area, optional): use this for short (less than 32768 characters) snippets of R code you wish to execute.\\n\\nEnsure your R script is styled and indented as per R conventions to avoid failures.\\n\\n4. R dataframe to output (text field, optional): refer an R dataframe (which exists in the R session) which you desire to output to a SAS dataset for downstream analysis.\\n\\n5. Output table (output port, optional): attach a SAS dataset to this port which will contain data from a R dataframe you wish to output.\\n\\n6. R_HOME path (Configuration tab, verify/change defaults): rpy2 needs to know where to find R on the system. Change this default value after consulting your administrator. The default is based on an environment where R and Python are installed using the SAS Configurator for Open Source (also known as sas-pyconfig) and sas-open-source-config utilities.\",\n\t\t\t\t\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"id\": \"parameters_output_specs\",\n\t\t\t\t\t\t\t\"type\": \"section\",\n\t\t\t\t\t\t\t\"label\": \"Output Specifications\",\n\t\t\t\t\t\t\t\"open\": true,\n\t\t\t\t\t\t\t\"visible\": \"\",\n\t\t\t\t\t\t\t\"children\": [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\"id\": \"output_parameters_text\",\n\t\t\t\t\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\t\t\t\t\"text\": \"- Reference dataset for R environment variables (output port, optional) : attach a SAS dataset to the envData output port of this custom step to refer variables created during R execution. These are known as R environment variables and can be accessed through the globalenvs attribute of the robjects object in rpy2 (documentation below).\",\n\t\t\t\t\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"about_documentation\",\n\t\t\t\t\t\"type\": \"section\",\n\t\t\t\t\t\"label\": \"Documentation\",\n\t\t\t\t\t\"open\": false,\n\t\t\t\t\t\"visible\": \"\",\n\t\t\t\t\t\"children\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"id\": \"documentation_text\",\n\t\t\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\t\t\"text\": \"1. Documentation on the rpy2 package: https://rpy2.github.io/doc/latest/html/introduction.html\\n\\n2. Access to R from inside a SAS Viya environment is governed by some environment variables, including R_HOME and DM_RHOME, used within this custom step to configure the rpy2 package. Refer this link (and embedded references) for instructions on configuring open source environments with SAS Viya: https://go.documentation.sas.com/doc/en/sasadmincdc/default/dplyml0phy0dkr/n08u2yg8tdkb4jn18u8zsi6yfv3d.htm#n0mq2y83d72jr8n1va9uuv04vx76\\n\\n3. The following SAS Communities article on R integration is also useful: https://communities.sas.com/t5/SAS-Communities-Library/Configuring-SAS-Viya-for-R-Integration/ta-p/848186\\n\\n4. The SAS Viya Platform Deployment Guide (refer to SAS Configurator for Open Source within): https://go.documentation.sas.com/doc/en/itopscdc/default/itopssr/p1n66p7u2cm8fjn13yeggzbxcqqg.htm?fromDefault=#p19cpvrrjw3lurn135ih46tjm7oi \",\n\t\t\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"known_issues\",\n\t\t\t\t\t\"type\": \"section\",\n\t\t\t\t\t\"label\": \"Some known issues & behaviour\",\n\t\t\t\t\t\"open\": false,\n\t\t\t\t\t\"visible\": \"\",\n\t\t\t\t\t\"children\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"id\": \"known_issues_text\",\n\t\t\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\t\t\"text\": \"This is not a comprehensive list of all known issues with rpy2. However, it's likely you may encounter some of these (given that you are working with THREE languages !) issues and the following links may be helpful.\\n\\n1. Pandas may not transfer cleanly to R dataframe due to a mismatch in types: https://stackoverflow.com/questions/60197294/error-when-using-pandas-dataframe-in-r-cell-in-rpy2-jupyter-notebook\\n\\n2. A table may be created but you are not able to access / export it. This might be due to global envs vs. local sessions: https://stackoverflow.com/questions/15227926/rpy2-object-not-found-error\",\n\t\t\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"change_log_text\",\n\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\"text\": \"Version: 1.0 (18AUG2023)\",\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\"id\": \"contact_text\",\n\t\t\t\t\t\"type\": \"text\",\n\t\t\t\t\t\"text\": \"Created/contact: \\n\\n- Samiul Haque (samiul.haque@sas.com)\\n\\n- Sundaresh Sankaran (sundaresh.sankaran@sas.com)\",\n\t\t\t\t\t\"visible\": \"\"\n\t\t\t\t}\n\t\t\t]\n\t\t}\n\t],\n\t\"syntaxversion\": \"1.3.0\",\n\t\"values\": {\n\t\t\"inputtable\": {\n\t\t\t\"library\": \"\",\n\t\t\t\"table\": \"\"\n\t\t},\n\t\t\"inputRScript\": \"\",\n\t\t\"rrrarea\": \"cat(\\\"R Runner. The coyote is after you...\\\")\",\n\t\t\"output_rdf_name\": \"\",\n\t\t\"envData\": {\n\t\t\t\"library\": \"\",\n\t\t\t\"table\": \"\"\n\t\t},\n\t\t\"outputtable\": {\n\t\t\t\"library\": \"\",\n\t\t\t\"table\": \"\"\n\t\t},\n\t\t\"rHomePath\": \"/opt/sas/viya/home/sas-pyconfig/default_r/lib64/R\"\n\t}\n}","templates":{"SAS":"/* SAS templated code goes here */\n\n/*-----------------------------------------------------------------------------------------*\n START MACRO DEFINITIONS.\n*------------------------------------------------------------------------------------------*/\n\n/* -----------------------------------------------------------------------------------------* \n Error flag for capture during code execution.\n*------------------------------------------------------------------------------------------ */\n\n%global _rr_error_flag;\n%let _rr_error_flag=0;\n\n/* -----------------------------------------------------------------------------------------* \n Create a global variable to contain the path of the R filename (either temp or specified)\n*------------------------------------------------------------------------------------------ */\n%global _rr_rfile_location;\n\n/* -----------------------------------------------------------------------------------------* \n Create a global variable to mention whether the text area count exists or not (indicating)\n whether single or multi-line.\n*------------------------------------------------------------------------------------------ */\n%global _rr_rarea_multiline_exist;\n%let _rr_rarea_multiline_exist=%symexist(rrrarea_count);\n\n\n%put NOTE: Multiple Lines exist in snippet indicator - &_rr_rarea_multiline_exist.;\n\n/* -----------------------------------------------------------------------------------------* \n This macro creates a temporary file to hold the R script from the snippet. \n*------------------------------------------------------------------------------------------ */\n\n%macro createTempRFile;\nfilename rfile temp;\n\n%if &_rr_rarea_multiline_exist.=0 %then %do;\n\n data _null_;\n file rfile;\n put \"&rrrarea.\";\n run;\n\n%end;\n\n%else %do;\n\n data _null_;\n length text $32767.;\n file rfile;\n do i = 1 to &rrrarea_COUNT. ;\n if symget(\"rrrarea_\" || strip(put(i,12.))) eq '' then do ;\n text=\"\";\n end ;\n else do ;\n\t\t text=cats(symget(\"rrrarea_\" || strip(put(i,12.)))) ;\n put text;\n\t\t end ;\n\t end ;\n run;\n\n%end;\n\n\n%mend createTempRFile;\n\n/* -----------------------------------------------------------------------------------------* \n This macro checks if the file provided is on the filesystem and not SAS Content, or if a \n file is provided at all. Currently, only files on the filesystem or snippets through the\n UI are supported. \n*------------------------------------------------------------------------------------------ */\n\n%macro treatRFileInput(fileName);\n\n %let fileType = %sysfunc(scan(\"&fileName.\",1,\":\"));\n %let filePath = %sysfunc(scan(\"&fileName.\",2,\":\"));\n\n %put NOTE: The file path is &filePath.;\n\n %if %sysfunc(upcase(\"&fileType.\"))=\"SASCONTENT\" %then %do;\n %put ERROR: Provide a R program located on the filesystem.;\n %let _rr_error_flag=1;\n %end;\n\n %else %if %sysfunc(upcase(\"&fileType.\"))=\"SASSERVER\" %then %do;\n %put NOTE: Filesystem file provided;\n filename rfile \"&filePath.\";\n %let _rr_rfile_location=%sysfunc(pathname(rfile));\n %end;\n\n %else %do;\n %put NOTE: Empty file location. Use of R snippet area assumed.;\n %createTempRFile;\n %let _rr_rfile_location=%sysfunc(pathname(rfile));\n %end;\n\n%mend treatRFileInput;\n\n\n\n/*-----------------------------------------------------------------------------------------*\n EXECUTION CODE MACRO \n Driven by user choice from UI. \n NOTE: Execution code needs (proc python) submit blocks to be written to a file prior to\n running. This shall be undertaken in future.\n*------------------------------------------------------------------------------------------*/\n%macro main_execution_code;\n%mend main_execution_code;\n\n\n/*-----------------------------------------------------------------------------------------*\n EXECUTION CODE \n Note: Python code blocks follow different indentation logic and are currently not\n indented within the SAS proc python blocks below. Workaround (lower priority) in progress.\n*------------------------------------------------------------------------------------------*/\n\n/*-----------------------------------------------------------------------------------------*\n Check the type of R file input provided and treat accordingly.\n*------------------------------------------------------------------------------------------*/\n\n%treatRFileInput(\"&inputRScript.\");\n\n%if &_rr_error_flag.=0 %then %do;\n\n/*-----------------------------------------------------------------------------------------*\n Initial imports which set up the R HOME connection.\n*------------------------------------------------------------------------------------------*/\n\n proc python;\n\n submit;\n\nimport os\nimport gc\n\nr_home_path=SAS.symget(\"rHomePath\")\n\nos.environ['R_HOME']=r_home_path\nimport rpy2.robjects as robjects\nfrom rpy2.robjects import r\n\n endsubmit;\n\n quit;\n\n/*-----------------------------------------------------------------------------------------*\n Create a Pandas dataframe from any input table / dataset provided. For ease of reference,\n this dataframe will be called input_table within Pandas (Python).\n\n Correspondingly, the R data frame can be referred to as \"r_input_table\" within R script.\n Notice the controlled manner in which the r_input_table data frame is passed to a global\n environment.\n*------------------------------------------------------------------------------------------*/\n\n\n\n proc python;\n submit;\n\ninputtable=SAS.symget(\"inputtable\")\n\nif len(inputtable) > 1 : \n\n import pandas as pd\n input_table = pd.DataFrame\n input_table = SAS.sd2df(inputtable)\n\n import rpy2.robjects as ro\n from rpy2.robjects.packages import importr\n from rpy2.robjects import pandas2ri\n from rpy2.robjects import globalenv\n\n with (ro.default_converter + pandas2ri.converter).context():\n r_input_table = ro.conversion.get_conversion().py2rpy(input_table)\n\n globalenv['r_input_table'] = r_input_table\n SAS.logMessage(\"Input dataset has been transferred to an R dataframe.\")\n \n del input_table\n gc.collect()\n\n endsubmit;\n quit;\n\n/*-----------------------------------------------------------------------------------------*\n Obtain input R file and execute with rpy2's robjects.r object. Write env variables and \n values to a Pandas dataframe and subsequently to a SAS dataset. \n*------------------------------------------------------------------------------------------*/\n\n/*-----------------------------------------------------------------------------------------*\n NOTE: In this ** early ** release, the values of the R environment variables (globalenv)\n are written as-is, i.e. they are considered rpy2 objects of different class types\n in Python. The values are only meant for informational purposes. Their presence \n within a Pandas dataframe should not make you assume that you can treat them \n natively as Python objects (further manipulation as guided by rPy2 documentation\n is needed).\n*------------------------------------------------------------------------------------------*/\n\n\n proc python;\n\n submit;\n\n# Obtain values from the UI\nrScriptPath=SAS.symget(\"_rr_rfile_location\")\nenvData=SAS.symget(\"envData\")\nprint(envData)\n\nwith open(rScriptPath,\"r\",encoding=\"utf-8\") as f:\n rsnippet=f.read()\n r(rsnippet) \n\n\nif len(envData) > 1:\n outputDict=[]\n for eachObject in robjects.globalenv:\n outputDict.append({\"R_Variable\":eachObject})\n outputDataFrame = pd.DataFrame.from_dict(outputDict)\n SAS.df2sd(outputDataFrame, dataset=envData)\n SAS.logMessage(\"Output variable information table.\")\n \n del outputDataFrame\n gc.collect()\n\n endsubmit;\n\n quit;\n\n\n\n\n%put NOTE: RR FILE LOCATION is &_rr_rfile_location.;\n\n\n\n/*-----------------------------------------------------------------------------------------*\n Create a Pandas dataframe from an existing R data frame and output to a SAS dataset. \n Ensure that this R data frame does exist.\n*------------------------------------------------------------------------------------------*/\n\n\n proc python;\n submit;\noutputtable = SAS.symget(\"outputtable\")\n\nif len(outputtable) > 1:\n\n import pandas as pd\n output_r_df = SAS.symget(\"output_rdf_name\")\n output_pdf = pd.DataFrame\n\n\n import rpy2.robjects as ro\n from rpy2.robjects import pandas2ri\n from rpy2.robjects import globalenv\n\n print(output_r_df)\n\n r_table = globalenv[output_r_df]\n\n with (ro.default_converter + pandas2ri.converter).context():\n output_pdf = ro.conversion.get_conversion().rpy2py(r_table)\n\n SAS.df2sd(output_pdf, dataset=outputtable)\n SAS.logMessage(\"Transferred dataframe to SAS\")\n\n del output_pdf\n gc.collect()\n\n\n endsubmit;\n quit;\n\n\n%end;\n/*-----------------------------------------------------------------------------------------*\n Remove execution-time macro variables.\n*------------------------------------------------------------------------------------------*/\n\n%symdel _rr_rfile_location;\n%symdel _rr_rarea_multiline_exist;\n%symdel _rr_error_flag;\n\n"}}