我在R中有一个多列ts,我想创建一个闪亮的应用程序,该应用程序让我可以选择应在图表上显示哪些列。
我已经阅读了一些有关可见性模式的内容,但是我对JS不熟悉,我找不到在R中使用它的方法。我还试图将我的ts使用以下子集:>
`new.ts <- as.data.frame(my.ts)
new.ts <- my.ts$input$model
new.ts <- ts(new.ts, start= (...), freq=...)
dygraph(new.ts)`
`library(shiny)
library(dygraphs)
ui <- fluidPage(
# Application title
titlePanel("Title"),
# Selecionar modelo
sidebarLayout(
sidebarPanel(
selectInput(inputId = "model",
label = "Choose the model to be plotted",
choices = c("...")
selected = "x")),
mainPanel(
dygraphOutput(outputId = "dygraph")
)
)
server <- function(input, output) {
output$dygraph <- renderDygraph({
dygraph(my.ts, main =glue("Forecast vs. Realizado {nome.modelo}")) %>%
dySeries("realizado", drawPoints = TRUE, pointSize = 3,color = "red") %>%
dySeries(input$model, drawPoints = TRUE,pointSize = 3,color = "blue")%>%
dyRangeSelector()
})
}
shinyApp(ui = ui, server = server)`
上面的代码为我绘制了ts所有列的图
答案 0 :(得分:0)
我已经找到一种解决方法,尽管我不确定它是否是最有效的。我创建了一个反应表达式,将我的df子集化。完成此操作后,我再次将df强制为ts。
dataInput <- reactive({
n <- which(colnames(base)==input$modelo_escolhido)
x <- a[,c(n,13),drop=FALSE]
x <- ts(x,start=start(base),frequency = frequency(base))
})
output$dygraph <- renderDygraph({
dygraph(dataInput(),
main = "Forecast vs. Realizado") %>%
dyOptions(drawPoints = TRUE, pointSize = 3,colors = RColorBrewer::brewer.pal(3, "Set1")) %>%
dyRangeSelector()