Rで解析:RからChatGPTを利用!!「chatgpt」パッケージ

Rの解析に役に立つ記事
スポンサーリンク

RからChatGPTを利用できるパッケージの紹介です。利用にはChatGPTサイトでアカウント登録とAPIキーの取得が必要です。RからChatGPTへ質問だけでなく、コードへコメントを記入、コードの内容から変数名を作成、コード内容の説明、コードの問題点を指摘、コードの最適化?などのコマンドが収録されています。

なお、RStudioへadd inとして追加するコマンドもヘルプなどに記載がありますが、紹介バージョンでは”could not find function”となります。

パッケージバージョンは0.1.5。実行コマンドはR version 4.2.2で確認しています。

スポンサーリンク

ChatGPTのAPIキーを取得する

下記URLにアクセスし、アカウントを作成してください。アカウント作成後にログインしてください。

OpenAI API: https://platform.openai.com/account/api-keys

ログイン後、下記画像のように右上部のアカウント名アイコンをクリックし「View API keys」をクリックします。

表示される「API keys」内の「Create new secret key」をクリックしてAPIキーをメモ帳などにコピーします。後は実行コマンドを参考にパッケージを利用してください。

パッケージのインストール

下記、コマンドを実行してください。

#パッケージのインストール
install.packages("chatgpt")

実行コマンド

詳細はコメント、パッケージのヘルプを確認してください。

#パッケージの読み込み
library("chatgpt")

#ChatGPTのAPIキーを設定
#"sk-XXXXXXXXXXX"を取得したAPIキーで置き換えます
Sys.setenv(OPENAI_API_KEY = "sk-XXXXXXXXXXX")

###RからChatGPTへ質問する:ask_chatgptコマンド#####
cat(ask_chatgpt("ggplot2パッケージで箱ひげ図を作りたい"))

#結果例
#*** ChatGPT input:
#ggplot2パッケージで箱ひげ図を作りたい
#ggplot2パッケージを使用すると、箱ひげ図を作成することが可能です。
#以下のサンプルコードを使用することで、箱ひげ図を作成することができます。
#```
# データフレームの作成
#df <- data.frame(
#  group=gl(3, 10, labels=c("A", "B", "C")), 
#  value=c(rnorm(10, 10, 2), rnorm(10, 8, 1.5), rnorm(10, 5, 2))
)
# &#31665;&#12402;&#12370;&#22259;&#12398;&#20316;&#25104;
#library(ggplot2)
#ggplot(df, aes(x=group, y=value)) +
#  geom_boxplot()
#```

#&#12371;&#12435;&#12394;&#20351;&#12356;&#26041;&#12418;&#21487;&#33021;
cat(ask_chatgpt("ChatGPT&#12392;R&#12398;&#38306;&#20418;&#24615;&#12434;&#20855;&#20307;&#30340;&#12395;&#25945;&#12360;&#12390;&#12367;&#12384;&#12373;&#12356;"))

#&#32080;&#26524;&#20363;
#*** ChatGPT input:
#ChatGPT&#12392;R&#12398;&#38306;&#20418;&#24615;&#12434;&#20855;&#20307;&#30340;&#12395;&#25945;&#12360;&#12390;&#12367;&#12384;&#12373;&#12356;
#R&#12392;ChatGPT&#12398;&#38306;&#20418;&#24615;&#12399;&#12289;R&#12392;ChatGPT&#12364;&#38750;&#24120;&#12395;&#20284;&#12390;&#12356;&#12427;&#12392;&#32771;&#12360;&#12425;&#12428;&#12390;&#12356;&#12414;&#12377;&#12290;
#R&#12399;&#38750;&#24120;&#12395;&#39640;&#24230;&#12394;&#12503;&#12525;&#12464;&#12521;&#12511;&#12531;&#12464;&#35328;&#35486;&#12391;&#12289;ChatGPT&#12399;&#33258;&#28982;&#35328;&#35486;&#20966;&#29702;&#25216;&#34899;&#12398;&#19968;&#12388;&#12391;&#12377;&#12290;
#ChatGPT&#12434;&#20351;&#29992;&#12377;&#12427;&#12392;&#12289;&#33258;&#28982;&#35328;&#35486;&#12486;&#12461;&#12473;&#12488;&#12434;&#35299;&#26512;&#12375;&#12390;&#12289;&#24847;&#21619;&#12434;&#29702;&#35299;&#12377;&#12427;&#12371;&#12392;&#12364;&#21487;&#33021;&#12395;&#12394;&#12426;&#12414;&#12377;&#12290;
#R&#12434;&#20351;&#29992;&#12377;&#12427;&#12392;&#12289;&#35079;&#38609;&#12394;&#12487;&#12540;&#12479;&#20998;&#26512;&#12434;&#34892;&#12358;&#12383;&#12417;&#12398;&#39640;&#24230;&#12394;&#12450;&#12523;&#12468;&#12522;&#12474;&#12512;&#12434;&#23455;&#35013;&#12377;&#12427;&#12371;&#12392;&#12364;&#12391;&#12365;&#12414;&#12377;&#12290;
#&#12375;&#12363;&#12375;&#12289;R&#12392;ChatGPT&#12434;&#19968;&#32210;&#12395;&#20351;&#29992;&#12377;&#12427;&#12371;&#12392;&#12399;&#12289;&#38750;&#24120;&#12395;&#22256;&#38627;&#12391;&#12377;&#12290;
########

###&#12467;&#12540;&#12489;&#12408;&#12467;&#12513;&#12531;&#12488;&#12434;&#35352;&#20837;:comment_code&#12467;&#12510;&#12531;&#12489;#####
cat(comment_code('PlotData +
                   geom_point(aes(x = carat, y = price, color = color)) +
                   facet_grid(.~color) +
                   labs(x = "carat", y = "price", colour = "Colour")'
                 ))

#&#32080;&#26524;&#20363;
#*** ChatGPT input:
#Add inline comments to the following R code: "PlotData +
#                   geom_point(aes(x = carat, y = price, color = color)) +
#                   facet_grid(.~color) +
#                   labs(x = "carat", y = "price", colour = "Colour")"

# Plot data points, each point color-coded according to the color category
#geom_point(aes(x = carat, # x-axis is the carat size of each diamond
#               y = price, # y-axis is the price of each diamond
#               color = color)) # color of each point according to the color category

# Group each plot by color category
#facet_grid(.~color)

# Define axis labels
#labs(x = "carat", # x-axis label is carat
#     y = "price", # y-axis label is price
#     colour = "Colour") # color legend label is Colour
########

###&#12467;&#12540;&#12489;&#12398;&#20869;&#23481;&#12363;&#12425;&#22793;&#25968;&#21517;&#12434;&#20316;&#25104;:create_variable_name&#12467;&#12510;&#12531;&#12489;#####
cat(create_variable_name("sample(c('&#12354;', '&#12356;'), n = 5, replace = TRUE)"))

#&#32080;&#26524;&#20363;
#*** ChatGPT input:
#Give a good variable name to the result of the following R code: 
#"sample(c('&#12354;', '&#12356;'), n = 5, replace = TRUE)"
#sampled_words
#######

###&#12467;&#12540;&#12489;&#20869;&#23481;&#12398;&#35500;&#26126;:explain_code&#12467;&#12510;&#12531;&#12489;#####
cat(explain_code("sample(c('&#12354;', '&#12356;'), n = 5, replace = TRUE)"))

#*** ChatGPT input:
#Explain the following R code: "sample(c('&#12354;', '&#12356;'), n = 5, replace = TRUE)"
#This code will generate a random sample of size 5 from the vector c('&#12354;', '&#12356;'),
#with replacement. This means that each element of the sample can appear multiple 
########

###&#12467;&#12540;&#12489;&#12398;&#21839;&#38988;&#28857;&#12434;&#25351;&#25688;:find_issues_in_code&#12467;&#12510;&#12531;&#12489;#####
#&#12354;&#12370;&#12427;&#12392;&#12377;&#12428;&#12400;set.seed&#12467;&#12510;&#12531;&#12489;&#12434;&#23455;&#34892;&#12375;&#12390;&#12356;&#12394;&#12356;&#12398;&#12391;&#12467;&#12510;&#12531;&#12489;&#20363;&#12399;&#20877;&#29694;&#24615;&#12364;&#12394;&#12356;
cat(find_issues_in_code("sample(c('&#12354;', '&#12356;'), n = 5, replace = TRUE)"))

#&#32080;&#26524;&#20363;
#*** ChatGPT input:
#Find issues or bugs in the following R code: "sample(c('&#12354;', '&#12356;'), n = 5, replace = TRUE)"
#1. The code does not specify the random number generator to use,
#so the result may vary depending on the random number generator.
#2. The code does not include an argument to set the seed,
#so the result may not be reproducible.

#&#32080;&#26524;&#20363;:&#24403;&#28982;&#12371;&#12398;&#12424;&#12358;&#12394;&#22238;&#31572;&#12418;
#*** ChatGPT input:
#Find issues or bugs in the following R code: "sample(c('&#12354;', '&#12356;'), n = 5, replace = TRUE)"
#No issues or bugs were found.
########

#&#12467;&#12510;&#12531;&#12489;&#12398;&#26368;&#36969;&#21270;&#65311;:optimize_code&#12467;&#12510;&#12531;&#12489;
#i=1&#12399;1,i=2&#12399;2,&#12381;&#12428;&#20197;&#22806;&#12399;3,4&#12364;&#34920;&#31034;&#12373;&#12428;&#12427;
#if&#12391;&#12399;&#12394;&#12367;else if&#12420;which&#12467;&#12510;&#12531;&#12489;&#12391;&#32622;&#12365;&#25563;&#12360;&#12427;?
cat(optimize_code(
  "if(i == 1){
    print(1)
  }else{
    if(i == 2){
      print(2)
    }else{
      print(3)
    }
    print(4)
  }"))

#&#32080;&#26524;&#20363;:&#20363;&#12398;&#20869;&#23481;&#12364;&#24746;&#12356;&#65311;
#*** ChatGPT input:
#Optimize the following R code: "if(i == 1){
#    print(1)
#  }else{
#    if(i == 2){
#      print(2)
#    }else{
#      print(3)
#    }
#    print(4)
#  }"

#if(i == 1){
#  print(1)
#} else if(i == 2){
#  print(2)
#} else {
#  print(3)
#  print(4)
#}
########

#RStudio&#12408;add in&#12392;&#12375;&#12390;&#36861;&#21152;:run_addin/run_addin_ask_chatgpt&#12467;&#12510;&#12531;&#12489;
#version 0.1.5&#12391;&#12399;could not find function 
#run_addin("ChatGPT")
#run_addin_ask_chatgpt()

少しでも、あなたの解析が楽になりますように!!

タイトルとURLをコピーしました