在我的数据框中选择一列来创建折线图

时间:2015-08-24 14:26:46

标签: r shiny dygraphs

我的数据集有三列。一列用于时间,另外两列用于温度测量(Temp1,Temp2)。

我希望能够选择Temp1或Temp2并绘制时间序列图,但我不知道如何在我的server.R代码中执行此操作。我的dygraph函数调用应该怎么做?

# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#

library(shiny)
library(dygraphs)

shinyServer(function(input, output) {

  output$TempData <- renderDygraph({
    data <- switch(input$data,
                   "Temp 1" = Data1$Temp1,
                   "Temp 2" = Data1$Temp2),
    dygraph(data, main = "Temperature Rise Data") %>%

     **I'm not sure what goes in here** 

  })

})

这是ui.R

# This is the user-interface definition of a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#

library(shiny)
library(dygraphs)

shinyUI(fluidPage(

  # Application title
  titlePanel("Temperature Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput("data", label = "Choose a Dataset",
                  choices = c("Temp 1", "Temp 1"),
                  selected = "Temp 1" ))
    ),

    # Show a plot of the generated distribution
    mainPanel(
      dygraphOutput("TempData")
    )
  )
)

1 个答案:

答案 0 :(得分:1)

据我所知,这应该能给你一些与你想要的东西相符的东西吗?

我强迫将Scan强制转换为Date以使其兼容 - 不确定这是否是执行此操作的最佳方式,但它至少会让您生成暂时使用此数据的基本dygraph - 您可能希望进一步研究格式化轴。

...
data <- switch(input$data,
               "Temp 1" = as.xts(Data1$Temp1, order.by = as.Date(Data1$Scan))
               "Temp 2" = as.xts(Data1$Temp2, order.by = as.Date(Data1$Scan))
dygraph(data, main = "Temperature Rise Data")
...