{"id":4456,"date":"2024-09-21T09:47:19","date_gmt":"2024-09-21T17:47:19","guid":{"rendered":"https:\/\/crm270s.abramjmeister.com\/?p=4456"},"modified":"2024-09-21T09:47:19","modified_gmt":"2024-09-21T17:47:19","slug":"r-monthly-from-oct-2023-to-aug-2024","status":"publish","type":"post","link":"https:\/\/crm270s.mycpamytax.com\/?p=4456","title":{"rendered":"R &#8211; monthly from Oct 2023 to Aug 2024"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>library(RSQLite)\nlibrary(DBI)\nlibrary(ggplot2)\n\n# Define the path to your SQLite database\ndb_path &lt;- \"\/home\/jbyungrokim\/CSV\/CSV.db\"\n\n# Connect to the SQLite database\nconn &lt;- dbConnect(RSQLite::SQLite(), dbname = db_path)\n\n# Define the range of months\nmonths &lt;- c('2023-10', '2023-11', '2023-12', '2024-01', '2024-02', '2024-03', '2024-04', '2024-05', '2024-06', '2024-07', '2024-08')\n\n# Loop through each month and run the query\nfor (month in months) {\n  query &lt;- paste0(\"\n  SELECT \n      Item, \n      SUM(Pay_Amount) AS Total_Spent\n  FROM \n      '\uc9c0\ucd9c-\uc561\uc218_2024_09_18'\n  WHERE \n      strftime('%Y-%m', '20' || substr(Date_Transaction, 7, 2) || '-' || substr(Date_Transaction, 1, 2) || '-' || substr(Date_Transaction, 4, 2)) = '\", month, \"'\n  GROUP BY \n      Item\n  ORDER BY \n      Total_Spent DESC;\n  \")\n  \n  # Execute the query and store the result in a data frame\n  monthly_sum &lt;- dbGetQuery(conn, query)\n  \n  # Print the result\n  print(paste(\"Sum of Pay_Amount by Item for\", month, \":\"))\n  print(monthly_sum)\n  \n  # Save the result as a CSV file\n  csv_file_path &lt;- paste0(\"\/home\/jbyungrokim\/Downloads\/\uc9c0\ucd9c-\", month, \"_sum.csv\")\n  write.csv(monthly_sum, file = csv_file_path, row.names = FALSE)\n  print(paste(\"Data has been written to\", csv_file_path))\n  \n  # Create a bar graph using ggplot2\n  p &lt;- ggplot(monthly_sum, aes(x = reorder(Item, -Total_Spent), y = Total_Spent)) +\n    geom_bar(stat = \"identity\", fill = \"steelblue\") +\n    theme_minimal() +\n    labs(title = paste(\"Total Spending by Item for\", month),\n         x = \"Item\",\n         y = \"Total Spent ($)\") +\n    theme(axis.text.x = element_text(angle = 45, hjust = 1))\n  \n  # Print the plot to ensure it displays\n  print(p)\n}\n\n# Close the connection to the SQLite database\ndbDisconnect(conn)\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[26],"tags":[],"class_list":["post-4456","post","type-post","status-publish","format-standard","hentry","category-codes"],"_links":{"self":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/4456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4456"}],"version-history":[{"count":0,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/4456\/revisions"}],"wp:attachment":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}