2017-02-20 33 views
0

我有点遗失,无法在R markdown文档的闪亮输出中添加反应测试(te)。基于R studio例子的一个简单例子是粘贴如下。在R markdown文件中包含反应文本

非常感谢提前! 让 - 皮埃尔·

--- 
title: "Untitled" 
runtime: shiny 
output: html_document 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE) 
``` 

```{r eruptions, echo=FALSE} 
inputPanel(
    selectInput("n_breaks", label = "Number of bins:", 
       choices = c(10, 20, 35, 50), selected = 20), 

    sliderInput("bw_adjust", label = "Bandwidth adjustment:", 
       min = 0.2, max = 2, value = 1, step = 0.2) 
) 

renderText({te}) 

renderPlot({ 
startTime <- Sys.time() 
    # additional code goes here 
endTime <- Sys.time() +1 
te <- reactive(startTime - endTime) 
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks), 
     xlab = "Duration (minutes)", main = "Geyser eruption duration") 

    dens <- density(faithful$eruptions, adjust = input$bw_adjust) 
    lines(dens, col = "blue") 
}) 
``` 

回答

1

我认为你应该使用te <<- reactive(startTime - endTime)定义terenderPlot外,使用renderText({te()})而不是renderText({te}),因为它是一种反应性的表达,最后把renderText({te()})给它的定义后结束。

+0

太棒了!我浪费了很多时间试图弄清楚它是如何工作的。你已经救了我的一天! – gattuso

相关问题