Can someone tell me how can I convert python code into R code in Codex?

I had been trying to follow the examples outlined by OpenAI documentation for converting Python Code into R Language. However I’m unable to convert (It seems OpenAI changed some stuff from the first release) Can someone help me in how to convert Python Code into R in Codex?

That’ll be real help! :slight_smile:

Which model are you using?
I would try the older Davinci to see if that works

1 Like

Thanks for the heads up! But, do you know if they had published the updates documentation. Could help a lot if you can tell!

Well i tried asking GPT-3 about it. Hope it helps.

There is no one-to-one conversion between Python and R, but there are some tools that can help with the task. The RPy package can help you convert R code to Python, and the reticulate package can help you run Python code from R.

Documentation highlighs that I can convert code. Even the OpenAI Live Demo

What I would do is make a new R (I assume Ruby) file.
Then use R’s comment system and write out.

=begin
Use Ruby

Take "section A" and convert it to Ruby

section A = [Paste your code Here]
=end

With that it tells the AI to use Ruby (comments help it understand language and allows you to speak to the AI.)

This is not tested but I have written a codedesc.py link to a bat file to allow me to load any code file into with comments and it seems to try its best.

codedesc.py (written by OpenAI, and me - 60/20)

import os
import json
import wget
import argparse
import random

parser = argparse.ArgumentParser(description='OpenAI Codex Code Description')
parser.add_argument('--file', action="store", dest='file', default="")
parser.add_argument('--text', action="store", dest='text', default="")
parser.add_argument('--output', action="store", dest='output', default="")

args = parser.parse_args()

def overwrite_file(file_path, string):
    with open(file_path, 'w') as f:
        f.write(string)

"""
2.create a function to append the string in a the file path sent to it
"""
def append_file(file_path, string):
    with open(file_path, 'a') as f:
        f.write(string)

"""
3.create a function to read the string in a the file path sent to it
"""
def read_file(file_path):
    with open(file_path, 'r') as f:
        return f.read()



import openai
openai.api_key = "[Your-Key]"

if args.file != "":
	with open(args.file, "r") as text_file:
		inputCode = text_file.read()
		
		
		response = openai.Completion.create(
		  engine="davinci-codex",
		  prompt=inputCode,
		  temperature=0.2,
		  max_tokens=3090,
		  top_p=1,
		  frequency_penalty=0.3,
		  presence_penalty=0.1,
		)

		append_file(args.file, response["choices"][0]["text"])
elif args.text != "" and args.output != "":

	response = openai.Completion.create(
	  engine="davinci-codex",
	  prompt=args.text,
	  temperature=0.2,
	  max_tokens=3090,
	  top_p=1.0,
	  frequency_penalty=0.3,
	  presence_penalty=0.1,
	)

	overwrite_file(args.output, response["choices"][0]["text"])
else:
	print("use '--file [filepath]' to get description of code file.", "use --text [test] and --output [filepath] to convert text into a code file.")

Good luck

(On other thing, it might get confused and just dump code that keeps going, if so change the prompt a little more clearer, and it helps a lot.)

1 Like