2017-08-24 1 views
1

Je prépare une présentation de RMarkdown reveal.js. Je souhaite que les sections du code R de la diapositive soient pliables dans la nature.RMarkdown reveal.js code de présentation pliage

Mon tête de YAML ressemble à ceci, mais le pliage de code n'est pas visible dans la présentation finale.

--- 
title: "Untitled" 
output: 
    revealjs::revealjs_presentation: 
    code_folding: hide 

--- 


## R Markdown 

This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. 

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. 


## Slide with R Code and Output 

```{r} 
summary(cars) 
``` 

## Slide with Plot 

```{r, echo=FALSE} 
plot(cars) 
``` 

L'ajout de code_folding: hide fonctionne pour le fichier RMarkdown standard.

est-code_folding pas disponible pour la présentation? Existe-t-il un moyen alternatif que je devrais essayer?

+0

Je suppose que ce n'est pas. Mais vous pouvez toujours créer smth similaire avec un peu de JavaScript. Peut-être que cela aide: https://stackoverflow.com/a/37839683/1777111 –

+0

Merci Martin, j'ai essayé ce code, mais il semble fonctionner uniquement sur les fichiers HTML, pas sur les diapositives. – Vasim

+0

Cest vrai. La raison en est que le DOM (modèle d'objet document) des présentations diffère de celui d'un document HTML commun de RMarkdown. Vous devrez adapter le code un peu pour le faire fonctionner. –

Répondre

1

Je tripoté quelque chose. Supposons que cela fonctionne uniquement pour les fragments de code source, mais pourrait être étendu à d'autres éléments. La plus grande partie du code provient de ma réponse mentionnée dans mon commentaire ci-dessus.

PEPAM complète:

--- 
title: "Untitled" 
output: 
    revealjs::revealjs_presentation: 
    self_contained: true 
--- 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script> 
$(document).ready(function() { 
    $chunks = $('div.sourceCode'); // get all divs containing source code... 
    // add the button and a wrapping container to each of them... 
    $chunks.each(function() { 
    $(this).prepend('<div class=\"but_con\"><div class=\"showopt\">Show Source</div></div>'); // add the button and a wrapping container to each of them... 
    $(this).find('code').toggle(); // hide them right away... 
    }); 

    // definition of the function to toggle visibility 
    // we select all buttons, and add a click function 
    $('.showopt').click(function() { 
    var label = $(this).html(); 
    if (label.indexOf("Show") >= 0) { 
     $(this).html(label.replace("Show", "Hide")); 
    } else { 
     $(this).html(label.replace("Hide", "Show")); 
    } 
    $(this).parent().siblings('pre').children('code').slideToggle('fast', 'swing'); 
    }); 

}); 
</script> 


<style> 
div.but_con { 
    margin: auto; 
    width: 90%; 
    padding-bottom: 10px; 
} 

div.showopt { 
    font-size: 35%; 
    background-color: #004c93; 
    color: #FFFFFF; 
    width: 100px; 
    height: 20px; 
    text-align: center; 
    vertical-align: middle !important; 
    float: right; 
    font-family: sans-serif; 
    border-radius: 8px; 
    margin-bottom: 10px; 
} 

.showopt:hover { 
    background-color: #dfe4f2; 
    color: #004c93; 
} 
</style> 

## R Markdown 

This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. 

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. 


## Slide with R Code and Output 

```{r} 
summary(cars) 
``` 

## Slide with Plot 

```{r, echo=FALSE} 
plot(cars) 
``` 
+0

Merci; ça fonctionne parfaitement. Je vais essayer de comprendre la différence entre les deux et apprendre la raison. – Vasim