您可以毫无问题地编译 cpp 文件,请查看您的代码为 shinyapp.io 采用和部署:
应用程序.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
*/
输出: