Quine with R

Quine is a self-reproducing function or a computer program that will output its source source or itself. Terms used are for these programs are also self-replicating programs, self-reproducing programs or self-copying programs (courtesy of wikipedia).

Many programming languages have been known to do this, for example in Python, a simple Quine would look like this:

a= 'a=%r;print(a%%a)';print(a%a)

Similar to Python, quine can be written in R as well. Simple example would be:

a<-"a<-0;cat(sub(0,deparse(a),a))";cat(sub(0,deparse(a),a))

Example consists of two blocks; the first block contains the function that will perform the process of replication

"a<-0;cat(sub(0,deparse(a),a))"

And the second part contains the code that will be outputted.

cat(sub(0,deparse(a),a))

When we run the command, the script will return itself, revealing the complete input command.

Word quine primarily comes from the biology, precisely from the self-replication, and it consists of two parts, first part is the code that performs the replication and the second is the data that contains all the code, script, instructions to perform the replication process.

The value of variable a can hold basically any text or any additional information, since the function in R is using string manipulation functions sub and deparse.

Deparse is used to preserve the quotations in original input command and sub is used to get the the first and the second block of code.

Happy R-coding!!!

Tagged with: , , , , ,
Posted in Uncategorized
5 comments on “Quine with R
  1. […] by data_admin [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

  2. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

  3. […] on Posted onJuly 22, 2020July 23, 2020By Bill [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You’ll be able to report subject in regards to the […]

    Like

  4. cellocgw says:

    Code Golf: what’s the *shortest* quine in R?

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...