README.md

swiplr

Use SWI-Prolog from R

Full documentation at PDF or HTML

What for

To call prolog from Rmd files and generate prolog based reports from RStudio.

Install

First of all, install SWI-Prolog. Make sure swipl binary is in your path.

I tested it once on windows. I manly use it from Linux. 64bit Windows seemed to miss swipl.exe, but 32bit worked fine. In case windows version looks broken and you need it, let me know!

Then you can install the R package:

# you need devtools, did you try install.packages(devtools) ?
devtools::install_github("https://github.com/battmanux/swiplr.git")

Basic usage

Load R library

# this is an R chunk
library(swiplr)

Add a prolog chunk

% this is some prolog code
foo(bar).
foo(other).

% define queries by starting lines with ?-
?- foo(X).

|FOO | |:-----| |bar | |other |

This returns a nice table foo_list that you can use from R

# This is some R code calling prolog output 
# R variable is named after prolog chunk label

str(foo_list)
'data.frame':   2 obs. of  1 variable:
 $ FOO: chr  "bar" "other"

Use from python

path <- system.file("swiplPy.py", package = "swiplr")
cat("Add this path for python libs: ", path)
sys.path.append("/usr/local/lib/R/site-library/swiplr/")
import swiplPy

swipl = swiplPy.swiplPy()

swipl.query(code="foo(bar).", query="foo(X)")

del swipl


battmanux/swiplr documentation built on July 29, 2021, 12:58 a.m.