For Loop Over Dygraph Does Not Work in R

For loop over dygraph does not work in R

Just wrap the list output from lapply() in htmltools::tagList(), e.g.

```{r}
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
res <- lapply(1:2, function(i) dygraph(lungDeaths[, i]))
htmltools::tagList(res)
```

Issue with inserting a dygraph in shiny app

Looks like filtered_xts() doesn't output any value.

Try:

  filtered_xts <- reactive({
data_ <- data %>%
filter(variable == input$variableInput,
unit == input$unitInput,
date >= input$dateInput[1],
date <= input$dateInput[2]
) %>%
select(-c(4:5)) %>%
mutate(quantity = round(quantity, 1)) %>%
spread(key = "category", value = "quantity") %>%
replace(is.na(.), 0) %>% data.table::as.data.table()
})

Following our discussion in comments, the conversion to data.table is more efficient than conversion to xts to be able to fully use dygraphs options.

How do I add spacing between dygraphs generated in apply loop in Rmarkdown?

I'm not greatly familiar with tagLists, but it would seem that you could interleave markup/tags containing your additional rules, spacing, text, etc. with the dygraph list, e.g.:

space <- list(htmltools::tags$h1("Title"), 
htmltools::tags$h2("Header text"))
out <- c(rbind(space, res))
htmltools::tagList(out)

Then each set of tags in the list will be rendered in turn.

Conditionally shade regions of dygraph time series when NA

A possible solution :

library(dygraphs)

nhtemp2 <- nhtemp
nhtemp2[10:20] <- NA
nhtemp2[30:45] <- NA

# Convert years to DyShading date format
dates <- sprintf('%d-1-1',time(nhtemp2))

# find NA intervals
start = dates[which(diff(is.na(nhtemp2))==1)]
stop = dates[which(diff(is.na(nhtemp2))==-1)+1]


g <- dygraph(nhtemp2, main = "New Haven Temperatures")

# Loop over NA intervals to add shading
for (i in 1:length(start)) {
g <- dygraphs::dyShading(
g,
from = start[i],
to = stop[i],
color = 'lightgrey')
}
g

Sample Image

Please note that this only works if the time serie doesn't start with NA, but could be improved to cope with this.



Related Topics



Leave a reply



Submit