Max Power
Max Power

Reputation: 8996

Displaying Output of Expression Using Shiny Reactive Input in RMarkdown

My rmd file is below. It should be easily reproducible as it just uses Sys.Date() and the slider input.

---
output: html_document 
runtime: shiny
---

```{r, echo=FALSE, error=FALSE, message=FALSE}
sliderInput("daysback", "Days back from today: ", min=1, max=45, value=30, step=1)

renderText({input$daysback})

display_date = reactive({Sys.Date() - input$daysback})
observe(print(display_date()))
```

renderText({input$daysback}) correctly displays the input value, and updates it as the user moves the slider.

However observe(print(display_date)) isn't displaying anything. I took the observe(print()) approach from the example below, from the SO answer using values from a reactive input to directly input into a custom function

num_square = reactive({input$num^2}) 
observe(print(num_square()))

Also, although no dates are displaying in the viewer pane when I click "Run Document" in RStudio, the RStudio console (RMarkdown-tab) is showing dates as I move the slider in the viewer pane. A sample of the output (a row is added each time I move the slider): Output created:

C:/Users/vuser/AppData/Local/Temp/RtmpSiQ52I/file125c17cd231.html [1] "2015-04-28" [1] "2015-05-08" [1] "2015-04-27" [1] "2015-05-03"

So I can't figure out why it won't display in the launched document.

Upvotes: 2

Views: 1119

Answers (1)

Max Power
Max Power

Reputation: 8996

OP here.

renderPrint(displayDate()) instead of observe(print(display_date())) did the trick for me. I actually mainly needed to be able to display str(displayDate()) to help me debug something else in my code.

Anyway, the code chunk below uses sliderInput and and a date to create a workaround date slider, since there's not a shiny dateSlider input function. That was what I was trying to get to with this question.

---
runtime: shiny
output: html_document
---

```{r}
sliderInput("sliderDays", "Days forward from 1/1/2015: ", min=1, max=45, value=1, step=1)

display_date = reactive({as.Date("2015-01-01") + input$sliderDays})
renderPrint(display_date())
```

Upvotes: 2

Related Questions