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
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
Add Multiple Columns to R Data.Table in One Function Call
Changing Facet Label to Math Formula in Ggplot2
How to Add Legend to Ggplot Manually? - R
Sending Email in R via Outlook
Convert a Numeric Month to a Month Abbreviation
R Suppress Startupmessages from Dependency
Change Colors in Ggpairs Now That Params Is Deprecated
How to Get the Classes of All Columns in a Data Frame
Sum All Values in Every Column of a Data.Frame in R
Remove All Rows Where Length of String Is More Than N
When Should I Use the := Operator in Data.Table
How to Update R Packages in Default Library on Windows 7
How to Make Tibbles Display Significant Digits
Read.CSV Warning 'Eof Within Quoted String' Prevents Complete Reading of File