-1

I have a C++ code (hello world code that prints "hello world!" in a text file).

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, const char * argv[]) {
// insert code here...
ofstream myfile;
myfile.open ("Hello_World.txt");
myfile << "Hello World! This is a test.\n";
myfile.close();
return 0;
}

I want to see if it's possible to compile my C++ code on shinyapp.io and then execute the compiled file on the server and get the "Hello_World.txt" file? If not, how should I compile my code on my local machine so it can be executed in shinyapp.io once I transfer the compiled code on server?

I am more concerned about the approach (compiling a Fortran or C++ code on local machine and running on shinyapp.io). My goal is to extend the approach for more complex code in future.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Ali
  • 41
  • 6
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Are you using Rcpp? Can you put your code in a package? – MrFlick Jul 27 '18 at 18:05
  • Thank you MrFlick for the response. No, I have not used Rcpp. In fact my actual code is a fortran code, but I first just wanted to start with a simple code as above to see the approach works or not. How can I put my code in a package? Sorry for the silly question. – Ali Jul 27 '18 at 18:26
  • 1
    http://r-pkgs.had.co.nz/ is a good start for package creation. – r2evans Jul 27 '18 at 19:34

2 Answers2

1

You can compile cpp-files without problems, see your code adopted & deployed for shinyapp.io:

app.R:

library(Rcpp)
library(shiny)

Rcpp::sourceCpp("test_rcpp.cpp")


shiny::shinyApp(

  ui = fluidPage(
    titlePanel(hello()),
    sidebarLayout(
      sidebarPanel("content of Hello_world.txt"),
      mainPanel(readChar("Hello_World.txt", file.info("Hello_World.txt")$size))
    )
  ),
  server = function(input, output, session) {
  }
)

test_rcpp.cpp:

#include <Rcpp.h>
#include <iostream>
#include <fstream>
using namespace Rcpp;
using namespace std;

// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp 
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
//   http://www.rcpp.org/
//   http://adv-r.had.co.nz/Rcpp.html
//   http://gallery.rcpp.org/
//

// [[Rcpp::export]]
CharacterVector hello() {
  ofstream myfile;
  myfile.open ("Hello_World.txt");
  myfile << "Hello World! This is a test.\n";
  myfile.close();
  return "Hello, World is saved";
;
}


// You can include R code blocks in C++ files processed with sourceCpp
// (useful for testing and development). The R code will be automatically 
// run after the compilation.
//

/*** R
*/

Output: enter image description here

Artem
  • 3,304
  • 3
  • 18
  • 41
0

An update:

I was finally able to execute the compiled code on shinyapp.io. There was a mistake about the file permission, and I used to get the permission denied error on shinyapp.io log. Until I found out that I need to set the permission for the compiled code on the server.R (with system() command) during the execution on shinyapp.io, not on my local machine. Thank you all for your comments.

Ali
  • 41
  • 6