Skip to content

Commit

Permalink
chunk options for knitr and sweave
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Mar 19, 2012
1 parent 3d7dba7 commit 649c77b
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 1 deletion.
64 changes: 64 additions & 0 deletions src/cpp/session/modules/SessionAuthoring.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#
# SessionAuthoring.R
#
# Copyright (C) 2009-11 by RStudio, Inc.
#
# This program is licensed to you under the terms of version 3 of the
# GNU Affero General Public License. This program is distributed WITHOUT
# ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
# AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
#
#

.rs.addFunction( "knitrChunkOptions", function()
{
knitrOptions <- knitr:::opts_chunk$get()
knitrOptions <- as.list(sapply(knitrOptions, class))
knitrOptions[knitrOptions == "NULL"] <- "character"
knitrOptions$results <- list("markup", "asis", "hide")
knitrOptions$fig.show <- list("asis", "hold", "animate")
knitrOptions$fig.keep <- list("high", "none", "all", "first", "last")
knitrOptions$fig.align <- list("left", "right", "center")
if (packageVersion("knitr") >= "0.4")
knitrOptions$dev <- as.list(names(knitr:::auto_exts))

return (knitrOptions)
})

.rs.addFunction( "sweaveChunkOptions", function()
{
sweaveOptions <- list()

sweaveOptions$label <- "character"
sweaveOptions$engine <- list("R", "S")
sweaveOptions$echo <- "logical"
sweaveOptions$keep.source <- "logical"
sweaveOptions$eval <- "logical"
sweaveOptions$results <- list("verbatim", "tex", "hide")
sweaveOptions$print <- "logical"
sweaveOptions$term <- "logical"
sweaveOptions$split <- "logical"
sweaveOptions$strip.white <- list("true", "all", "false")
sweaveOptions$prefix <- "logical"
sweaveOptions$prefix.string <- "character"
sweaveOptions$include <- "logical"
sweaveOptions$fig <- "logical"
sweaveOptions$eps <- "logical"
sweaveOptions$pdf <- "logical"
sweaveOptions$pdf.version <- "character"
sweaveOptions$pdf.encoding <- "character"
sweaveOptions$pdf.compress <-"logical"
if (getRversion() >= "2.13.0") {
sweaveOptions$png <- "logical"
sweaveOptions$jpeg <- "logical"
sweaveOptions$grdevice <- "character"
}
sweaveOptions$width <- "numeric"
sweaveOptions$height <- "numeric"
sweaveOptions$resolution <- "numeric"
sweaveOptions$figs.only <- "logical"

return (sweaveOptions)
})

14 changes: 14 additions & 0 deletions src/cpp/session/modules/SessionAuthoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ Error getTexCapabilities(const core::json::JsonRpcRequest& request,
return Success();
}

Error getChunkOptions(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
std::string weaveType;
Error error = json::readParams(request.params, &weaveType);
if (error)
return error;

pResponse->setResult(tex::rnw_weave::chunkOptions(weaveType));
return Success();
}

Error isTexInstalled(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
Expand Down Expand Up @@ -220,12 +232,14 @@ Error initialize()
using namespace module_context;
ExecBlock initBlock ;
initBlock.addFunctions()
(bind(sourceModuleRFile, "SessionAuthoring.R"))
(tex::compile_pdf::initialize)
(tex::compile_pdf_supervisor::initialize)
(tex::synctex::initialize)
(tex::view_pdf::initialize)
(bind(registerRpcMethod, "is_tex_installed", isTexInstalled))
(bind(registerRpcMethod, "get_tex_capabilities", getTexCapabilities))
(bind(registerRpcMethod, "get_chunk_options", getChunkOptions))
(bind(registerRpcMethod, "compile_pdf", compilePdf))
(bind(registerRpcMethod, "is_compile_pdf_running", isCompilePdfRunning))
(bind(registerRpcMethod, "terminate_compile_pdf", terminateCompilePdf))
Expand Down
51 changes: 50 additions & 1 deletion src/cpp/session/modules/tex/SessionRnwWeave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <r/RExec.hpp>
#include <r/RRoutines.hpp>
#include <r/RJson.hpp>
#include <r/session/RSessionUtils.hpp>

#include <session/SessionUserSettings.hpp>
Expand Down Expand Up @@ -81,6 +82,8 @@ class RnwWeave : boost::noncopyable

virtual bool isInstalled() const = 0;

virtual core::json::Value chunkOptions() const = 0;

virtual std::vector<std::string> commandArgs(
const std::string& file) const = 0;

Expand Down Expand Up @@ -135,6 +138,27 @@ class RnwWeave : boost::noncopyable
return Success();
}

protected:
core::json::Value chunkOptions(const std::string& chunkFunction) const
{
SEXP optionsSEXP;
r::sexp::Protect rProtect;
r::exec::RFunction optionsFunc(chunkFunction);
Error error = optionsFunc.call(&optionsSEXP, &rProtect);
if (error)
{
LOG_ERROR(error);
return json::Value();
}

core::json::Value optionsJson;
error = r::json::jsonValueFromList(optionsSEXP, &optionsJson);
if (error)
LOG_ERROR(error);

return optionsJson;
}

private:
std::string name_;
std::string packageName_;
Expand All @@ -152,6 +176,11 @@ class RnwSweave : public RnwWeave

virtual bool injectConcordance() const { return true; }

virtual core::json::Value chunkOptions() const
{
return RnwWeave::chunkOptions(".rs.sweaveChunkOptions");
}

#ifdef _WIN32
virtual std::vector<std::string> commandArgs(const std::string& file) const
{
Expand Down Expand Up @@ -220,6 +249,11 @@ class RnwPgfSweave : public RnwExternalWeave
}

virtual bool injectConcordance() const { return false; }

virtual core::json::Value chunkOptions() const
{
return core::json::Value();
}
};

class RnwKnitr : public RnwExternalWeave
Expand Down Expand Up @@ -268,6 +302,11 @@ class RnwKnitr : public RnwExternalWeave
return Success();
}

virtual core::json::Value chunkOptions() const
{
return RnwWeave::chunkOptions(".rs.knitrChunkOptions");
}

private:
static std::string cmdFormat()
{
Expand Down Expand Up @@ -454,7 +493,17 @@ void runWeave(const core::FilePath& rnwPath,
}
}

json::Array supportedTypes()
core::json::Value chunkOptions(const std::string& weaveType)
{
boost::shared_ptr<RnwWeave> pRnwWeave = weaveRegistry()
.findTypeIgnoreCase(weaveType);
if (pRnwWeave)
return pRnwWeave->chunkOptions();
else
return core::json::Value();
}

core::json::Array supportedTypes()
{
// query for list of supported types
json::Array array;
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/session/modules/tex/SessionRnwWeave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace rnw_weave {
core::json::Array supportedTypes();
void getTypesInstalledStatus(core::json::Object* pObj);

core::json::Value chunkOptions(const std::string& weaveType);

struct Result
{
static Result error(const std::string& errorMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.rstudio.studio.client.workbench.views.source.editors.text.IconvListResult;
import org.rstudio.studio.client.workbench.views.source.model.CheckForExternalEditResult;
import org.rstudio.studio.client.workbench.views.source.model.PublishPdfResult;
import org.rstudio.studio.client.workbench.views.source.model.RnwChunkOptions;
import org.rstudio.studio.client.workbench.views.source.model.SourceDocument;
import org.rstudio.studio.client.workbench.views.vcs.dialog.CommitCount;
import org.rstudio.studio.client.workbench.views.vcs.dialog.CommitInfo;
Expand Down Expand Up @@ -1094,6 +1095,13 @@ public void getTexCapabilities(
requestCallback);
}

public void getChunkOptions(
String weaveType,
ServerRequestCallback<RnwChunkOptions> requestCallback)
{
sendRequest(RPC_SCOPE, GET_CHUNK_OPTIONS, weaveType, requestCallback);
}

public String getProgressUrl(String message)
{
String url = getApplicationURL(SOURCE_SCOPE + "/" + "progress");
Expand Down Expand Up @@ -2624,6 +2632,7 @@ public void clearFindResults(ServerRequestCallback<Void> requestCallback)
private static final String ICONVLIST = "iconvlist";
private static final String PUBLISH_PDF = "publish_pdf";
private static final String GET_TEX_CAPABILITIES = "get_tex_capabilities";
private static final String GET_CHUNK_OPTIONS = "get_chunk_options";

private static final String GET_RECENT_HISTORY = "get_recent_history";
private static final String GET_HISTORY_ITEMS = "get_history_items";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* RnwChunkOptions.java
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
package org.rstudio.studio.client.workbench.views.source.model;

import java.util.ArrayList;
import java.util.Collections;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;

public class RnwChunkOptions extends JavaScriptObject
{
protected RnwChunkOptions()
{
}

public final ArrayList<String> getOptions()
{
ArrayList<String> options = new ArrayList<String>(
new JSONObject(this).keySet());
Collections.sort(options);
return options;
}

// types: "numeric", "character", "logical", "list"
public final String getOptionType(String name)
{
JSONArray arr = new JSONArray(getOptionTypeNative(name));;
if (arr.size() == 1)
return arr.get(0).isString().stringValue();
else
return "list";
}

public final ArrayList<String> getOptionValues(String name)
{
JSONArray arr = new JSONArray(getOptionTypeNative(name));
ArrayList<String> values = new ArrayList<String>();
for (int i=0; i<arr.size(); i++)
values.add(arr.get(i).isArray().get(0).isString().stringValue());
return values;
}

private native final JavaScriptObject getOptionTypeNative(String name) /*-{
return this[name];
}-*/;


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ public interface TexServerOperations
{
void getTexCapabilities(
ServerRequestCallback<TexCapabilities> requestCallback);

void getChunkOptions(String weaveType,
ServerRequestCallback<RnwChunkOptions> requestCallback);
}

0 comments on commit 649c77b

Please sign in to comment.