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

问题描述 投票:0回答:1

我的数据集有三列。 一列用于时间,另外两列用于温度测量(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")
    )
  )
)
r shiny r-dygraphs
1个回答
1
投票

据我所知,这应该会给你一些与你正在寻找的东西相符的东西?

我强制将

Scan
强制转换为
Date
以便使其兼容 - 不确定这是否是执行此操作的最佳方法,但它至少允许您使用此数据生成基本的笔画此刻 - 您可能想进一步研究轴的格式。

...
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")
...
© www.soinside.com 2019 - 2024. All rights reserved.