{"id":16571690,"url":"https://github.com/coolbutuseless/callme","last_synced_at":"2025-03-16T20:31:15.319Z","repository":{"id":182299057,"uuid":"668247694","full_name":"coolbutuseless/callme","owner":"coolbutuseless","description":"Easily compile inline C code for R","archived":false,"fork":false,"pushed_at":"2025-02-17T07:18:34.000Z","size":837,"stargazers_count":24,"open_issues_count":1,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-16T05:31:42.974Z","etag":null,"topics":["c","r","r-package"],"latest_commit_sha":null,"homepage":"https://coolbutuseless.github.io/package/callme/index.html","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coolbutuseless.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-19T11:12:10.000Z","updated_at":"2025-02-28T22:23:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"850000b0-98e9-4231-a33f-88396d670131","html_url":"https://github.com/coolbutuseless/callme","commit_stats":null,"previous_names":["coolbutuseless/callme"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fcallme","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fcallme/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fcallme/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fcallme/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coolbutuseless","download_url":"https://codeload.github.com/coolbutuseless/callme/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243928325,"owners_count":20370271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","r","r-package"],"created_at":"2024-10-11T21:24:49.477Z","updated_at":"2025-03-16T20:31:15.313Z","avatar_url":"https://github.com/coolbutuseless.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n# callme \u003cimg src=\"man/figures/logo.png\" align=\"right\" width=\"30%\" /\u003e\n\n\u003c!-- badges: start --\u003e\n\n![](https://img.shields.io/badge/cool-useless-green.svg) [![CRAN\nstatus](https://www.r-pkg.org/badges/version/callme.png)](https://CRAN.R-project.org/package=callme)\n[![R-CMD-check](https://github.com/coolbutuseless/callme/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/coolbutuseless/callme/actions/workflows/R-CMD-check.yaml)\n\u003c!-- badges: end --\u003e\n\n`{callme}` compiles inline C code and generates wrappers so that the C\ncode can be easily called from R.\n\nFeatures:\n\n- Compile inline C code (or code from a file) and makes it immediately\n  (and easily!) available to R.\n- Accepts complete C code - including function declaration and header\n  `#include` directives.\n- Explicit handling for `CFLAGS`, `PKG_CPPFLAGS` and `PKG_LIBS` for\n  setting compiler flags, C pre-processor flags, and library linking\n  flags, respectively.\n- Generates R functions to call the compiled C functions.\n- Multiple function definitions allowed in a single code block.\n\n### What’s in the box\n\n- `compile(code, CFLAGS, PKG_CPPFLAGS, PKG_LIBS, env, verbosity)`\n  compile the C `code` and assign R functions into the nominated `env`\n  in R. C code could be as a string or in a file.\n\n## Installation\n\nThis package can be installed from CRAN\n\n``` r\ninstall.packages('callme')\n```\n\nYou can install the latest development version from\n[GitHub](https://github.com/coolbutuseless/callme) with:\n\n``` r\n# install.package('remotes')\nremotes::install_github('coolbutuseless/callme')\n```\n\nPre-built source/binary versions can also be installed from\n[R-universe](https://r-universe.dev)\n\n``` r\ninstall.packages('callme', repos = c('https://coolbutuseless.r-universe.dev', 'https://cloud.r-project.org'))\n```\n\n## Example\n\nThe following example compiles a code snippet into a C library and\ncreates a wrapper function in R (of the same name) which can be used to\ncall the compiled code.\n\n``` r\nlibrary(callme)\n\ncode \u003c- \"\n#include \u003cR.h\u003e\n#include \u003cRinternals.h\u003e\n\n// Add 2 numbers\nSEXP add(SEXP val1, SEXP val2) {\n  return ScalarReal(asReal(val1) + asReal(val2));\n}\n\n// Multiply 2 numbers\nSEXP mul(SEXP val1, SEXP val2) {\n  return ScalarReal(asReal(val1) * asReal(val2));\n}\n\n// sqrt elements in a vector\nSEXP new_sqrt(SEXP vec) {\n  SEXP res = PROTECT(allocVector(REALSXP, length(vec)));\n  double *res_ptr = REAL(res);\n  double *vec_ptr = REAL(vec);\n  for (int i = 0; i \u003c length(vec); i++) {\n    res_ptr[i] = sqrt(vec_ptr[i]);\n  }\n  \n  UNPROTECT(1);\n  return res;\n}\n\"\n\n# compile the code\ncompile(code)\n\n# Call the functions\nadd(99.5, 0.5)\n```\n\n    #\u003e [1] 100\n\n``` r\nmul(99.5, 0.5)\n```\n\n    #\u003e [1] 49.75\n\n``` r\nnew_sqrt(c(1, 4, 25, 999))\n```\n\n    #\u003e [1]  1.00000  2.00000  5.00000 31.60696\n\n## Linking against an installed library\n\nIn this example we want to get the version of the `zstd` library (which\nhas already been installed on the computer), and return it as a\ncharacter string.\n\nWe need to tell R when compiling the code:\n\n- to look in the `/opt/homebrew/include` directory for `zstd.h`.\n- to look for the actual `zstd` library in `/opt/homebrew/lib`.\n- to link to the `zstd` library (`-lzstd`)\n\nNote: This code works for `zstd` installed via `homebrew` on macOS.\nPaths will be different for other operating systems.\n\n``` r\ncode \u003c- r\"(\n#include \u003cR.h\u003e\n#include \u003cRinternals.h\u003e\n#include \"zstd.h\"\n  \nSEXP zstd_version() {\n  return mkString(ZSTD_versionString());\n}\n)\"\n\n# Compile the code \ncompile(code, \n       PKG_CPPFLAGS = \"-I/opt/homebrew/include\", \n       PKG_LIBS     = \"-L/opt/homebrew/lib -lzstd\")\n\n# Call the function\nzstd_version()\n```\n\n    #\u003e [1] \"1.5.6\"\n\n# References\n\n- Hadley’s [R internals](https://github.com/hadley/r-internals).\n- [Advanced R Book](http://adv-r.had.co.nz/C-interface.html) has a\n  specfic chapter or R’s interface to C.\n- Ella Kay’s\n  [UserR2024](https://userconf2024.sched.com/event/1c8zS/c-for-r-users-ella-kaye-university-of-warwick)\n  conference presentation: [“C for R\n  users”](https://static.sched.com/hosted_files/userconf2024/84/c-for-r-users.pdf)\n- Book: [Deep R\n  Programming](https://deepr.gagolewski.com/chapter/310-compiled.html)\n- Davis Vaughan’s [Now you C\n  me](https://blog.davisvaughan.com/posts/2019-03-02-now-you-c-me/)\n- [c3po](https://github.com/ramiromagno/c3po)\n- [R Native API](https://github.com/HenrikBengtsson/RNativeAPI)\n\n### R project official documentation\n\n- Writing R extensions Section 5 [System and foreign language\n  interfaces](https://cran.r-project.org/doc/manuals/R-exts.html#System-and-foreign-language-interfaces)\n- [R\n  internals](https://cran.stat.auckland.ac.nz/doc/manuals/r-devel/R-ints.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolbutuseless%2Fcallme","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoolbutuseless%2Fcallme","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolbutuseless%2Fcallme/lists"}