colorful text using printf in C - c

I was wondering how can I print colorful text in the console? I use eclipse win64 OS. Does it have to do with the compiler? Can anyone give a simple example in C with just a hello world text in red or whatever?

I know that this is incredibly easy to do in C++, but I found this for you to look at in C:
#include <stdio.h>
#include <windows.h> // WinApi header
int main()
{
HANDLE hConsole;
int k;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
SetConsoleTextAttribute(hConsole, k);
printf("%3d %s\n", k, "I want to be nice today!");
}
getchar(); // wait
return 0;
}
All of the comments will help you to find your way through the code - hope it helps!

If you want to print colored text in Windows console, you will have to use Windows API. ANSI.sys support is no longer present in Windows.
In Linux you can still use ANSI escape sequences to color text.

You can use ANSI escape sequence on Windows as well with the following modification:
reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f
example:
int main()
{
printf("\033[33mThis is yellow\033[0m");
return 0;
}
source:
https://www.codeproject.com/Tips/5255355/How-to-Put-Color-on-Windows-Console

If you are constrained to using just printf(), this requires knowledge of the terminal to which you're writing. The chances are it is an ANSI-style terminal, so it can be done. The Unix curses (Linux ncurses) library handles such information in a terminal-independent way. Basically, you will need to define or manufacture control strings to turn the terminal into red mode and then reset it back again (but how do you know what state it was in before you changed it to writing red text?). The libraries mentioned keep track of the state information, amongst many other details.
However, if you get the strings organized, then code like this will do the trick (more or less):
static const char to_red[] = "\033...";
static const char to_black[] = "\033...";
printf("%s%s%s\n", to_red, "hello world", to_black);
The hard part is determining what goes in the constant strings (which need not actually be constant).
All this means there is probably a Windows-specific interface that can be used to do the job, but that does not really involve printf() for controlling the colours; you call the Windows API to set the colour, then write with printf(), then call the API again to reinstate the colour. There is probably a query function to allow you to find the current setting, which you use before changing it.

The console in Java uses stdout which is whatever OS you are running on. For Windows, you would need to access the Console API to change the colours. For Linux or Mac, the console might support ANSI escape sequences which can change the console colours via stdout.

Here's a further example for you. It is in C++, but I'm sure you can handle that; but I do also have the exact same code in this example in python. It's a small demo I wrote to end up drawing some lines in colors.
Anyway the series of demos is at:
https://github.com/goblinhack/c-plus-plus-python-tutorial
With the full source for the below example at:
https://github.com/goblinhack/c-plus-plus-python-tutorial/blob/master/lesson1/lesson1.cpp
The C++ code for the above picture is using the Ansi color class I define in the lesson1.cpp. But once you use that, it's very simple to use. hth.
int main (int argc, char *argv[])
{
Ansi ansi;
std::cout << ansi.get_code(ansi.FOREGROUND_RED);
std::cout << "hello ";
std::cout << ansi.get_code(ansi.FOREGROUND_GREEN);
std::cout << "beautiful";
std::cout << ansi.get_code(ansi.RESET);
std::cout << ansi.get_code(ansi.FOREGROUND_CYAN);
std::cout << " colorful";
std::cout << ansi.get_code(ansi.RESET);
std::cout << ansi.get_code(ansi.FOREGROUND_BLUE);
std::cout << " world";
std::cout << ansi.get_code(ansi.RESET);
std::cout << std::endl;
return (0);
}
With similar ability in python
def lesson1():
""" hello beautiful world """
ansi = Ansi()
for bg_col in range(ansi.Code.BACKGROUND_BLACK,
ansi.Code.BACKGROUND_WHITE):
for fg_col in range(ansi.Code.FOREGROUND_BLACK,
ansi.Code.FOREGROUND_WHITE):
sys.stdout.write("{0: <20} {1: <20}".format(\
ansi.get_code_name(bg_col),
ansi.get_code_name(fg_col)))
sys.stdout.write(ansi.get_bgfg_code(bg_col, fg_col))
sys.stdout.write("colorful")
sys.stdout.write(ansi.get_code(ansi.Code.RESET))
print()
sys.stdout.write(ansi.get_code(ansi.Code.FOREGROUND_RED))
sys.stdout.write("hello")
sys.stdout.write(ansi.get_code(ansi.Code.FOREGROUND_GREEN))
sys.stdout.write(" beautiful")
sys.stdout.write(ansi.get_code(ansi.Code.FOREGROUND_CYAN))
sys.stdout.write(" colorful")
sys.stdout.write(ansi.get_code(ansi.Code.FOREGROUND_BLUE))
sys.stdout.write(" world")
sys.stdout.write(ansi.get_code(ansi.Code.RESET))
print("from Python")
lesson1()

Related

Utilizing the <filesystem> library; visualStudios

I'll cut the the short of it. I'm attempting to understand the filesystem library but there's very little information I've been able to find. I managed to get it to compile and understand the filesystem::path type variable really well but don't seem to understand how to get filesystem::directory_iterator to work properly. I'm not sure if I'm using it for a purpose it wasn't design for. So here is what I'm attempting to do:
I wanted to create a program that opens every text file within a specified folder. For this I need to obtain the folder name and path but I want the program to be able to obtain this information on itself and dynamically so if I add or remove textFiles it'll have the logic to function.
I'm able to create a directory_iterator variable that it manages to hold the first file with me just giving it the directory like this:
const char address[]{ "C:\\Users\\c-jr8\\ProgramBranch\\PersonalPlatform\\log extruder\\logs" };
fs::directory_iterator myIterator(address);
When testing the code in the folder I have four textFiles called "attempt 1" to "attempt 4". When reading the information on:
https://learn.microsoft.com/en-us/cpp/standard-library/directory-iterator-class?view=vs-2019#op_star
It mentions two functions to move the path object within the iterator to the next file. The first is increment(), which is the intendent method for iterating through the files, and operation++().
Now increment() hasn't been able to work for me cause it takes in a erro_code type variable and I haven't been able to find much information about how to implement this with the filesystem_errorcode variable or however it's meant to be used. The operation++() works beautifully and provides me with the path to every file but I was having issues with managing the code to detect when the operate++() functions leads to no other files. Once it iterates through every file it sorts of crashes when it keeps moving to the next. Here's that piece of code:
string tempString;
for (int i = 0; i < 5; i++) { //Here the limiting is 5 so it'll iterate onces more than the numbers of files unpurpose to see how it responses.
tempString = myIterator.operator*().path().generic_string();
ifstream tempFile(tempString);
if (!tempFile.is_open()) {
cout << "Looking at file: " << i + 1 << "; failed to open." << endl << endl;
cin.get();
return 0;
}
{
//do things with file...
}
tempFile.close();
myIterator.operator++();
}
What I want if to find a way to stop the for loop once it the iterator goes off the final file.
whichever information about how the filesystem library works it would be very much appreciated.
std::directory_iterator is a classic iterator that allows for iterating over ranges, and those are usually designated by a pair of iterators, one indicating the beginning of a sequence and another representing the past-the-end iterator.
Some iterator types, like those providing access to streams, don't have an actual end location in memory. A similar situation applies to a directory iterator. In such a case, the idiomatic approach is to use a default-constructed iterator object that will serve as an end indicator.
Having said that, you could rewrite your loop to:
for (fs::directory_iterator myIterator(address), end{}; myIterator != end; ++myIterator) {
Alternatively, you can utilize a range-based for loop:
for (auto& p : fs::directory_iterator(address)) {
tempString = p.path().generic_string();
// ...
Also, note that iterators' interface is supposed to look/behave like a pointer, hence it uses operator overloading to allow for concise syntax. So instead of:
myIterator.operator++();
you should be using:
++myIterator;
Similarly, instead of:
myIterator.operator*().path().generic_string();
juse use:
(*myIterator).path().generic_string();
or:
myIterator->path().generic_string();
You should compare myIterator with a default constructed directory_iterator to check if the last file has been processed. You can also use a much simpler form to access the operators (shown in the code below):
string tempString;
// loop until myIterator == fs::directory_iterator{}
for(size_t i = 1; myIterator != fs::directory_iterator{}; ++i) {
// access path() through the iterators operator->
tempString = myIterator->path().generic_string();
ifstream tempFile(tempString);
if(!tempFile.is_open()) {
cout << "Looking at file: " << i << "; failed to open." << endl << endl;
cin.get();
return 0;
}
{
std::cout << tempString << " opened\n";
}
// tempFile.close(); // closes automatically when it goes out of scope
// simpler form to use myIterator.operator++():
++myIterator;
}
An even simpler approach would be to use a range-based for-loop:
for(const fs::directory_entry& dirent : fs::directory_iterator(address)) {
const fs::path& path = dirent.path();
ifstream tempFile(path);
if(!tempFile) {
cout << "Looking at file: " << path << "; failed to open.\n\n";
cin.get();
return 0;
}
std::cout << path << " opened\n";
}

export C data structures to a Grafana-supported database: your advice?

I have a C program which fills and calculates the data structures like
struct Users
{
unsigned char login[LOGIN_SIZE+1];
unsigned int hours, minut, works;
struct Range {
unsigned int hours_a, minut_a, time_a;
unsigned int hours_b, minut_b, time_b;
unsigned int dur_hours, dur_minut, dur_time;
struct WorkIDs {
unsigned int work_id;
unsigned int filled;
} work;
unsigned int filled;
} range[RANGE_SIZE];
unsigned int verbosity;
unsigned int filled;
} users[USERS_SIZE];
Now I need to export the filled data structures from C program to any of the Grafana-supported databases, i.e.: InfluxDB, MySQL or PostgreSQL (full list is available at https://grafana.com/docs/grafana/latest/features/datasources/ and may be expanded with plugins)
However, I couldn't find any good example of exporting a C filled data structure to a database, and struggle establishing a connection between these technologies. Please could you advise a right approach?
My working approach (feel free to suggest the alternatives!)
1) use a PostgreSQL : unlike MySQL which gave me a lot of various errors until I gave up, the installation of PostgreSQL was really quick and straightforward, just follow an article like https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-18-04 to quickly get started with your first example database.
2) take a libpqxx library : they give a great C++ example at documentation http://pqxx.org/development/libpqxx/ , which could be built with a command like c++ -o post post.cxx -lpqxx -lpq. Your libpqxx library needs to be above a certain version for this example to work! On a Linux Mint 19.3, I had to download libpqxx-6.2_6.2.5-1_amd64.deb and libpqxx-dev_6.2.5-1_amd64.deb of "The Eoan Ermine" from https://launchpad.net/ubuntu/+source/libpqxx : because building a fresh libpqxx from source isn't that easy, while its' version at Linux Mint distro's official repositories is way too old and not sufficient.
Connection string at this C++ example could be replaced with something like
pqxx::connection c("dbname=anonymous host=localhost user=anonymous password=123"); and then it works. Whole modified source code, to match a tutorial from 1 - paint all the playgrounds to a color of your choice:
#include <iostream>
#include <pqxx/pqxx>
int main(int, char *argv[])
{
pqxx::connection c("dbname=anonymous host=localhost user=anonymous password=123");
pqxx::work txn(c);
int equip_id = 1;
std::cout << "Updating equip_id #" << equip_id << " and others" << std::endl;
for (equip_id = 1; equip_id < 3; equip_id++) {
txn.exec0(
"UPDATE playground "
"SET color = " + txn.quote(argv[1]) +
"WHERE equip_id = " + txn.quote(equip_id));
}
// Make our change definite.
txn.commit();
}
Then, it's just a matter of time before you could export the C structure data fields into this PostgreSQL database using lots of SQL queries in a cycle; although, you'd also need to come up with a good database structure and add it to PostgreSQL using a psql interactive command line before doing this.
3) Install Grafana and configure PostgreSQL data source - using the tutorials like
https://grafana.com/docs/grafana/latest/installation/debian/
https://grafana.com/docs/grafana/latest/getting-started/getting-started/
https://grafana.com/docs/grafana/latest/features/datasources/postgres/
I had to disable a SSL mode at Data Sources / PostgreSQL configuration to successfully connect to a database using the same username/password as your C++ program (didn't need to specify a port).

How To Make Dev C++ on wine (Ubuntu) automatically launches after compile

I want to try some C libraries that are only available in windows so i installed wine and install dev C++.
Unlike on windows, after i compile and run, it successfuly generate/compile into "exe" but the cmd is not showing up .
I found a way on how to run the exe by launching the terminal and putting
$wine cmd
$myc.exe
It works but it takes time to manually launch the "exe".
How can i make dev c++ to automatically find cmd in wine and execute the compiled code?
Thank you in advance.
UPDATE :
The time I posted this question, I'm new to Linux/Ubuntu that's why I'm looking/expecting for that functionality in wine. But now after 2 years amany months i figured out that codeblock running on wine which is for windows and cannot call ubuntu terminal once compiled.
Dev-C++, by default, relies on a simple file called ConsolePauser.exe. This file calls the compiled .exe file, and gives the familiar Process exited after 0.xxxxx seconds with return value x. notice after it exits.
However, ConsolePauser.exe is a native Windows binary, it cannot be executed in Ubuntu, unless called by Wine. Also, the ConsolePauser calls the bare name of the executable, instead of a call to Wine, which is required.
Therefore, what you need to do to make Dev-C++ to run .exe files automatically after you press F9 is to build your OWN ConsolePauser. This is quite simple, actually:
#include <chrono>
#include <iostream>
#include <string>
int main(int agrc, char ** argv)
{
using namespace std;
string s = argv[1];
string s1;
for (const auto & ss : s)
{
if ((ss == ' ') || (ss == '\\')) s1.push_back('\\');
s1.push_back(ss);
}
s = "wine " + s1;
auto begin = chrono::high_resolution_clock::now();
auto retVal = system(s.c_str());
auto end = chrono::high_resolution_clock::now();
cout << "-------------------------------------" << endl;
cout << "Process completed after " << chrono::duration_cast<chrono::milliseconds>(end - begin).count();
cout << " milliseconds with return value " << retVal << "." << endl;
cout << "Press any key to continue. . ." << endl;
cin.get();
return 0;
}
What it simply does is parsing the argument, escaping required characters, and pass it to Wine. It is a quick and dirty version, you start improving it by checking if argc == 1.
Compile it as ConsolePauser.exe with Ubuntu's compiler, put it anywhere in your computer's PATH and it should work.
Another problem exists, however. For unknown reasons, Ubuntu's executables don't get executed in a separate window, if called by an app like Dev-C++, unlike Windows. Therefore, you will have to find a way to bring the ConsolePauser.exe to a new window.
A simple approach is rename your file to ConsolePauser1.exe, and use this code for ConsolePauser.exe:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char ** argv)
{
string s = argv[1];
//Opens new window through gnome-terminal:
string command = "gnome-terminal -e ";
command += string("\"") + "bash -c ";
command += string("\\\"") + "ConsolePauser1.exe ";
command += string("\\\\\\\"") + s;
command += string("\\\\\\\"");
command += string("\\\"");
command += string("\"");
system(command.c_str());
cerr << command << endl;
//Make sure that window lingers...
system("exec bash");
return 0;
}
Put these two files in the same folder in your PATH, and the familiar old Console Pauser will work like a charm.
I'd like to submit my approach, although it is not specific to Ubuntu. It can work with any distro with xterm installed.
Rename ConsolePauser.exe to ConsolePauserEngine.exe and create a new ConsolePauser.exe text file in the same dir with the following lines:
#! /bin/sh
CMD=$(printf "%q" $#) #https://stackoverflow.com/questions/2854655/command-to-escape-a-string-in-bash#answer-2856010
CONSOLE_PAUSER_PATH=$(printf "%q" "C:\Program Files (x86)\Dev-Cpp\ConsolePauserEngine.exe")
xterm -e "wine $CONSOLE_PAUSER_PATH $CMD"
EDIT: Don't forget to make the new ConsolePauser.exe executable.

c doesn't print "┌──┐" character correctly

Good afternoon, I'm facing a problem on my c code and I don't know what is causing it.
Every time I try to print characters like these: "┌──┐" my program simply prints some strange characters, like on this screenshot:
I'm using Qt Creator on Windows, with Qt version 5.5.0 MSVC 64 bits. The compiler is the Microsoft Visual C++ Compiler 12.0 (amd64).
I tried changing the locale but with no success. The only way I found to print these characters was to define them as int variables with the ASCII code and printing them, but it led to some really extensive and ugly coding, like this:
int cSupEsq = 218; //'┌'
int cSupDir = 191; //'┐'
int cInfEsq = 192; //'└'
int cInfDir = 217; //'┘'
int mVert = 179; //'│'
int mHor = 196; //'─'
int espaco = 255; //' '
int letraO = 111; //'o'
//Inicia limpando a tela da aplicação
clrscr();
//Linha 1
printf("%c", cSupEsq);
for (i = 1; i < 79; i++) { printf("%c", mHor); }
printf("%c", cSupDir);
Is there any way I can make the program treat these characters correctly? What could be causing this problem?
Your solution to use the OEM code points is the right way to go, codepage 850/437 is the default code page for the console and therefore should work. You could also use SetConsoleOutputCP to ensure the correct code page is used for the console.
Having said that, what is happening when you do not use your workaround is that the source file is being saved using a different codepage ie. not codepage 850/437. The in memory representation of the source code is Unicode (probably UTF-8), when you save the file the in memory representation of the characters are mapped to the target code page for the file.
What you can do is to save the file using the 850/437 codepage as the target, I don't know how you do this in Qt Creator (If you can at all), in Visual Studio for example you can select the down arrow on the Save button and select "Save with encoding", you can then proceed to select the target codepage, in your case code page 850. This will ensure that the in memory code points are mapped correctly to the file to be compiled.
I hope that helps explain the issue.
It shouldn't be necessary to print the characters one at a time. Instead, you can use an escape sequence:
printf("\xDA\xBF\xC0\xD9\xB3\xC4\xFF");

Using R random number generators in C [duplicate]

I would like to, within my own compiled C++ code, check to see if a library package is loaded in R (if not, load it), call a function from that library and get the results back to in my C++ code.
Could someone point me in the right direction? There seems to be a plethora of info on R and different ways of calling R from C++ and vis versa, but I have not come across exactly what I am wanting to do.
Thanks.
Dirk's probably right that RInside makes life easier. But for the die-hards... The essence comes from Writing R Extensions sections 8.1 and 8.2, and from the examples distributed with R. The material below covers constructing and evaluating the call; dealing with the return value is a different (and in some sense easier) topic.
Setup
Let's suppose a Linux / Mac platform. The first thing is that R must have been compiled to allow linking, either to a shared or static R library. I work with an svn copy of R's source, in the directory ~/src/R-devel. I switch to some other directory, call it ~/bin/R-devel, and then
~/src/R-devel/configure --enable-R-shlib
make -j
this generates ~/bin/R-devel/lib/libR.so; perhaps whatever distribution you're using already has this? The -j flag runs make in parallel, which greatly speeds the build.
Examples for embedding are in ~/src/R-devel/tests/Embedding, and they can be made with cd ~/bin/R-devel/tests/Embedding && make. Obviously, the source code for these examples is extremely instructive.
Code
To illustrate, create a file embed.cpp. Start by including the header that defines R data structures, and the R embedding interface; these are located in bin/R-devel/include, and serve as the primary documentation. We also have a prototype for the function that will do all the work
#include <Rembedded.h>
#include <Rdefines.h>
static void doSplinesExample();
The work flow is to start R, do the work, and end R:
int
main(int argc, char *argv[])
{
Rf_initEmbeddedR(argc, argv);
doSplinesExample();
Rf_endEmbeddedR(0);
return 0;
}
The examples under Embedding include one that calls library(splines), sets a named option, then runs a function example("ns"). Here's the routine that does this
static void
doSplinesExample()
{
SEXP e, result;
int errorOccurred;
// create and evaluate 'library(splines)'
PROTECT(e = lang2(install("library"), mkString("splines")));
R_tryEval(e, R_GlobalEnv, &errorOccurred);
if (errorOccurred) {
// handle error
}
UNPROTECT(1);
// 'options(FALSE)' ...
PROTECT(e = lang2(install("options"), ScalarLogical(0)));
// ... modified to 'options(example.ask=FALSE)' (this is obscure)
SET_TAG(CDR(e), install("example.ask"));
R_tryEval(e, R_GlobalEnv, NULL);
UNPROTECT(1);
// 'example("ns")'
PROTECT(e = lang2(install("example"), mkString("ns")));
R_tryEval(e, R_GlobalEnv, &errorOccurred);
UNPROTECT(1);
}
Compile and run
We're now ready to put everything together. The compiler needs to know where the headers and libraries are
g++ -I/home/user/bin/R-devel/include -L/home/user/bin/R-devel/lib -lR embed.cpp
The compiled application needs to be run in the correct environment, e.g., with R_HOME set correctly; this can be arranged easily (obviously a deployed app would want to take a more extensive approach) with
R CMD ./a.out
Depending on your ambitions, some parts of section 8 of Writing R Extensions are not relevant, e.g., callbacks are needed to implement a GUI on top of R, but not to evaluate simple code chunks.
Some detail
Running through that in a bit of detail... An SEXP (S-expression) is a data structure fundamental to R's representation of basic types (integer, logical, language calls, etc.). The line
PROTECT(e = lang2(install("library"), mkString("splines")));
makes a symbol library and a string "splines", and places them into a language construct consisting of two elements. This constructs an unevaluated language object, approximately equivalent to quote(library("splines")) in R. lang2 returns an SEXP that has been allocated from R's memory pool, and it needs to be PROTECTed from garbage collection. PROTECT adds the address pointed to by e to a protection stack, when the memory no longer needs to be protected, the address is popped from the stack (with UNPROTECT(1), a few lines down). The line
R_tryEval(e, R_GlobalEnv, &errorOccurred);
tries to evaluate e in R's global environment. errorOccurred is set to non-0 if an error occurs. R_tryEval returns an SEXP representing the result of the function, but we ignore it here. Because we no longer need the memory allocated to store library("splines"), we tell R that it is no longer PROTECT'ed.
The next chunk of code is similar, evaluating options(example.ask=FALSE), but the construction of the call is more complicated. The S-expression created by lang2 is a pair list, conceptually with a node, a left pointer (CAR) and a right pointer (CDR). The left pointer of e points to the symbol options. The right pointer of e points to another node in the pair list, whose left pointer is FALSE (the right pointer is R_NilValue, indicating the end of the language expression). Each node of a pair list can have a TAG, the meaning of which depends on the role played by the node. Here we attach an argument name.
SET_TAG(CDR(e), install("example.ask"));
The next line evaluates the expression that we have constructed (options(example.ask=FALSE)), using NULL to indicate that we'll ignore the success or failure of the function's evaluation. A different way of constructing and evaluating this call is illustrated in R-devel/tests/Embedding/RParseEval.c, adapted here as
PROTECT(tmp = mkString("options(example.ask=FALSE)"));
PROTECT(e = R_ParseVector(tmp, 1, &status, R_NilValue));
R_tryEval(VECTOR_ELT(e, 0), R_GlobalEnv, NULL);
UNPROTECT(2);
but this doesn't seem like a good strategy in general, as it mixes R and C code and does not allow computed arguments to be used in R functions. Instead write and manage R code in R (e.g., creating a package with functions that perform complicated series of R manipulations) that your C code uses.
The final block of code above constructs and evaluates example("ns"). Rf_tryEval returns the result of the function call, so
SEXP result;
PROTECT(result = Rf_tryEval(e, R_GlobalEnv, &errorOccurred));
// ...
UNPROTECT(1);
would capture that for subsequent processing.
There is Rcpp which allows you to easily extend R with C++ code, and also have that C++ code call back to R. There are examples included in the package which show that.
But maybe what you really want is to keep your C++ program (i.e. you own main()) and call out to R? That can be done most easily with
RInside which allows you to very easily embed R inside your C++ application---and the test for library, load if needed and function call are then extremely easy to do, and the (more than a dozen) included examples show you how to. And Rcpp still helps you to get results back and forth.
Edit: As Martin was kind enough to show things the official way I cannot help and contrast it with one of the examples shipping with RInside. It is something I once wrote quickly to help someone who had asked on r-help about how to load (a portfolio optimisation) library and use it. It meets your requirements: load a library, accesses some data in pass a weights vector down from C++ to R, deploy R and get the result back.
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
//
// Simple example for the repeated r-devel mails by Abhijit Bera
//
// Copyright (C) 2009 Dirk Eddelbuettel
// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
try {
RInside R(argc, argv); // create an embedded R instance
std::string txt = "suppressMessages(library(fPortfolio))";
R.parseEvalQ(txt); // load library, no return value
txt = "M <- as.matrix(SWX.RET); print(head(M)); M";
// assign mat. M to NumericMatrix
Rcpp::NumericMatrix M = R.parseEval(txt);
std::cout << "M has "
<< M.nrow() << " rows and "
<< M.ncol() << " cols" << std::endl;
txt = "colnames(M)"; // assign columns names of M to ans and
// into string vector cnames
Rcpp::CharacterVector cnames = R.parseEval(txt);
for (int i=0; i<M.ncol(); i++) {
std::cout << "Column " << cnames[i]
<< " in row 42 has " << M(42,i) << std::endl;
}
} catch(std::exception& ex) {
std::cerr << "Exception caught: " << ex.what() << std::endl;
} catch(...) {
std::cerr << "Unknown exception caught" << std::endl;
}
exit(0);
}
This rinside_sample2.cpp, and there are lots more examples in the package. To build it, you just say 'make rinside_sample2' as the supplied Makefile is set up to find R, Rcpp and RInside.

Resources