ashb commented on a change in pull request #3115: [AIRFLOW-2193] Add ROperator 
for using R
URL: https://github.com/apache/airflow/pull/3115#discussion_r259303210
 
 

 ##########
 File path: airflow/contrib/operators/r_operator.py
 ##########
 @@ -0,0 +1,79 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from builtins import bytes
+import os
+from tempfile import NamedTemporaryFile
+
+from airflow.models import BaseOperator
+from airflow.utils.decorators import apply_defaults
+from airflow.utils.file import TemporaryDirectory
+
+import rpy2.robjects as robjects
+from rpy2.rinterface import RRuntimeError
+
+
+class ROperator(BaseOperator):
+    """
+    Execute an R script or command
+
+    :param r_command: The command or a reference to an R script (must have
+        '.r' extension) to be executed (templated)
+    :type r_command: string
+    :param output_encoding: encoding output from R (default: 'utf-8')
+    :type output_encoding: string
+
+    """
+
+    template_fields = ('r_command',)
+    template_ext = ('.r', '.R')
+    ui_color = '#C8D5E6'
+
+    @apply_defaults
+    def __init__(
+            self,
+            r_command,
+            output_encoding='utf-8',
+            *args, **kwargs):
+
+        super(ROperator, self).__init__(*args, **kwargs)
+        self.r_command = r_command
+        self.output_encoding = output_encoding
+
+    def execute(self, context):
+        """
+        Execute the R command or script in a temporary directory
+        """
+
+        with TemporaryDirectory(prefix='airflowtmp') as tmp_dir:
+            with NamedTemporaryFile(dir=tmp_dir, prefix=self.task_id) as f:
+
+                f.write(bytes(self.r_command, 'utf_8'))
+                f.flush()
+                fname = f.name
+                script_location = os.path.abspath(fname)
+
+                self.log.info("Temporary script location: %s", script_location)
+                self.log.info("Running command(s):\n%s", self.r_command)
+
+                try:
+                    res = robjects.r.source(fname, echo=False)
 
 Review comment:
   That should be okay if you are based off the latest master:
   
   
https://github.com/apache/airflow/blob/d46a137ec32eb81a6a2874f62a9623d0bee12c57/airflow/models/__init__.py#L1388-L1392
   
   If _collecting_ the result is expensive then it sometimes makes sense to 
check `self.do_xcom_push` in the operator (i.e. the docker operator does this 
as it doesn't make sense to collect the logs and then just throw them away)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to