Skip to main content

Have you used AI for R coding and has it been successful?

Created
Active
Viewed 4k times
20 replies
12

Recently I have mixed success with using AI to help with some programming tasks. I am defining success here as: the code output being correct and coding faster than me.

Here is an example of AI giving the wrong output.

My prompt:

I want to create the same result with less function calls in R. I am going to provide my current code: as.vector(unlist(lapply(mtcars,is.numeric)))

The answer:

as.vector(unlist(mtcars, use.names = FALSE))

This is just a silly example but it does show the point that you have to be careful and check the output.

I have also used it successfully to help me pull publications from the website pubmed which did save me a tremendous amount of time (the script is long to share here).

Discussion questions:

What are your guidelines on using AI to help with programming tasks? What is your general take on using AI in general? Do you have success or horror stories?

  • 4.4k
  • 1
  • 14
  • 35

20 replies

Sorted by:
77100958
7

My first R experiment with ChatGPT was to ask for code and references for an analytical solution to the sum of three triangular probability densities. The initial code that it gave me made no sense so I asked several follow up questions and found that the code in the replies became worse, i.e. not related to the problem and with obvious syntax errors. The titles of the references that it provided looked very promising until I attempted to find them - none were real and their DOIs either went nowhere or to unrelated papers. More recently I have gotten some useful replies to more specific questions. For example, asking how to specify a spatial auto-regressive term in a brms model resulted in a suggestion that it might be easier to fit such a model using the INLA package, plus a code snippet to get started.

77101481
3

Yes. Converting from Python code to R code. Not great results but okay results to get close. Good to get started since was shut down getting started asking questions on StackOverflow. But, was also lied to/hallucinated with by the ChatGPT for some conversion and it totally made up a non-existent package, so your mileage may vary. Good luck.

77102402
3

Bard seems to do better job that chatGPT and Co-pilot. I have only used free version of chatGPT so can't say much about premium. But I would take Bard as a baseline for R. Co-pilot works fine for few lines of code. (Short term memory I think).

77103463
11

I have tried it a couple of times, mainly when I got stuck with a coding problem. It never really gave the perfect answer but it did help me to get new ideas of how to solve the coding problem, which was really useful.

77382802
3

My sense too having tried both development and debugging I found that ChatGPT is better at explaining errors and several strategies to solve them but its R code development is subpar.

77105938
16
  • 17.2k
  • 7
  • 56
  • 82

I use the Copilot as plugin in my editor, and I absolutely love it. Not for the complicated stuff or complete solutions and definitely not for statistics, but computers are here for us to do very boring things very quickly. Like prototyping functions, writing documentation, finishing the closing brackets, quickly adding necessary mundane options. I absolutely love it and Copilot does a great job doing that. Things like:

  • I have a data frame, want to move it to wide format, type pivot_wider and Copilot correctly guesses all the options and arguments
  • I need a variation of a graphics I did before in the same script for another data set with small changes; Copilot fills out all the 10+ lines of ggplot2 code for me, I do my little modifications and presto.
  • example from today: I needed a function to read and process a yaml file, but I don't remember the library name for yaml or how to do it. I write a comment '## function that reads parameters out of the yaml file' and I get a working prototype a fraction of a second later.

All in all, it speeds up my work by a significant factor. What does not work is expecting the code to run out of the box or taking over the code without understanding what it does.

With AI, it is like in that old Zen of Unix anecdote, about an acolyte who tries to restart the server by pushing a button and it does not work. The master comes and pushes the same button, and lo, it works. Why did it not work for me? asks the student. The master smiles and says: because you need to understand what you're doing.

So yeah, this is how I see the AI generated code. If you understand the code it produces, straightening it is usually way less work than writing the code yourself. If you do not, then no, AI will not help you.

77106890
3
  • 4.4k
  • 1
  • 14
  • 35
Author

This is great thank you for this! I will try out copilot (especially for documentation). Good to understand the limitations as well.

77222418
2

It's interesting to note that the github copilot is available in the daily versions of Rstudio

77223095
2
  • 4.4k
  • 1
  • 14
  • 35
Author

I tried it out and have been appreciating it so far. It has some limitations but I think if you are a sound R programmer this can be a helpful tool for you.

78556208
0

I'm using it and it ranges from handy to infuriating. When doing EDA, it's nice to be able to type ggplot( and get a decent chart. But it keeps trying to mess up my data processing pipelines in subtle ways.

77448561
1

Which AI are you using? ChatGPT? CoPilot?

77461087
1
  • 4.4k
  • 1
  • 14
  • 35
Author

at this point I used both, ChatGPT helped me get some skeleton code that I had to verify if it worked and produced the output I desired. I prompted for a few things and it definitely gave the wrong answers so be careful. I use CoPilot more but not religiously. They have their uses but I haven't found it overwhelmingly beneficial yet. Most of my issues I could have googled and found a solution. I might start using copilot to help document code but even that has to be checked.

77639254
2

Ive been using Chat GPT for a while now.

its fantastic at working out a function or name when you cant remember it. it is pritty good at giving you ideas for how to fix bugy code. but if your trying to do somthing new that isnt withing your own kowlege base it can somtimes give some long winded or miss guided solutions. but tbh that can happen to me when im using stack overflow or solving it myself.

tldr its just like a calculator speeds up stuff you have a bit of knowledge about and is confusing if you don't know anything about the functions its trying to offer up

77668745
2

I mostly used it to interpret other's code and it worked fine. When it comes to plotting I noticed it prefers ggplot2 library over the standard graphics. Also, if you pass some data formatted in a table is able to recognize is a dataset.

77673601
2
  • 1.4k
  • 1
  • 12
  • 27

Even though this question is several months old, the question is entirely relevant and pertains to Base R coding.

It seems AI is increasing its Base R code related query response capabilities. AI is expanding its knowledge base quickly. Even though AI claims its training was completed in 2021 or 2022, the models are receiving near constant "minor retraining" updates with new data.

The Bing co-pilot / VS Code co-pilot / GitHub co-pilot / RStudio co-pilots are awesome.

I have no way to prove or disprove this assertion, but from my own personal experiences I can say that I am receiving fewer and fewer unsatisfactory AI generated responses to my Base R queries.

I have enough Base R coding experience to usually make a query concise enough that I receive a response with the code or Base R answer I was seeking. Much more often than being wrong, the issue with AI involves a simple translation misunderstanding.

When I receive an AI response to one of my queries that is way off base, then I will closely review the wording of that query. Normally, I find I could have worded the query better. Usually, those responses I receive are not wrong - they are just answers to the wrong question. Those wayward responses to one of my queries are usually just responses to my poorly worded query.

I would suggest that querying AI for R Base coding questions might not be a satisfactory experience for those who do have enough coding experience to understand whether there was a problem with the question or a problem with the response.

I am more likely to frame a Base R query to AI as a statement rather than a question. And often these types of queries get a response that begins with "Yes, you're correct ...." . But in learning to work with AI, there will be a time when seeing many responses saying ""Apologies for the confusion earlier ...". But the confusion was from me, not from AI.

78141694
2

The only time I use AI to "code" for me is when I want to input a tiny data frame of test data generated with excel into R (when I don't want to save the test data to my computer). I essentially tell chatGPT "write R code to create a data frame for the following data" and then paste my table from excel. I wouldn't trust chatGPT to do anything more complicated, or to write a bigger data frame since you need to read through the entire output to check that it is accurate.

I will admit that it was super cool when chatGPT generated its own (very accurate) column names after I forgot to paste my column names along with the data. For example, it recognized that the contents of my first column were cell lines and named that column "Cell_Line".

78180620
2

Yes.

I often use it for simple but time-consuming tasks that I know I'd be able to do on my own if I spent 10 minutes trying to remember exactly how to do something. I use it to build up complex uses of dplyr::mutate() with long case_when() statements fairly often.

ChatGPT can usually interpret and explain an error message that I'd have to spend half an hour working out by myself.

I find that it cannot do the computational thinking I need to solve complex problems which need to be broken down into many steps, but that I often come across the solution while frustratedly attempting to explain to CGTP why its code didn't work.

I'm an intermediate user of R. I don't have much instant recall for R syntax, but I'm able to recognise when I'm being led down a dead end, so it saves me a lot of time.

78240890
1
  • 1.9k
  • 3
  • 16
  • 26

I've found AI to be very useful for coding in R. In general, it is just acts as a better search engine that can normally provide results tailored to my specific use case. When coding I often know what I want to do but sometimes forget the specific syntax I need to use. I know there's some stackoverflow question that had a good example of what I needed to do so I used to just search for that. Now I ask AI and it pretty much just returns the same stack overflow response I was looking for but the advantage is that if I give it my data.frames name it will give me code more or less ready to go with my data.frames name. Saves me time in the search and also in compiling code. Sometimes it can provide me generally useful responses that I was not aware of previously. I've been learning python recently and AI has been even more useful there. I've found in both R and python if you want to do something truly unique that has not been asked on stackoverflow or some other form before you still need to code it the old fashion way and just figure it out yourself because if its not searchable on the internet AI doesn't have an answer for you. Still in some causes it can remind you have good coding practices and give you hints at the right direction to go.

So to answer you question my guidelines for AI is to use it like you would a search engine which is to verify the code works before blindly throwing it into what your working on. In general, I've found AI to be very useful in general causes but less useful for truly unique questions and I have mostly success stories.

78243956
1
  • 4.4k
  • 1
  • 14
  • 35
Author

great response thank you! I do appreciate the analogy of using it like a search engine.

78368061
1

I am mostly working with Python and when I needed to code in R, using AI has been very helpful IMO. However, I need to do recheck and careful about prompting and the AI codes' suggestions (due to hallucination. Still there with the newest GPT4).
In addition, using Copilot in RStudio has been life changing!