带有数据框的闪亮App时间序列图

时间:2019-06-12 21:57:34

标签: r plot shiny

我正在尝试构建一个简单的Shiny应用程序,该应用程序可以从CSV文件中获取数据输入,然后将数据与ts图一起显示,这是我编写的R代码,

ui.R文件

library(shiny)
shinyUI <- fluidPage(
  titlePanel(h2("Blood Test Result System",align = "center")),
  sidebarLayout(    
  sidebarPanel(

  selectInput(inputId = "dataset",
                         label = "Choose a blood value:",
                         choices = c("Albumin","Bilirubin","CL(KLOR)","Glukoz","Kalsiyum","Kolesterol",
                                     "Kreatinin","Potasyum","Protein","Trigliserid","Hdl Kolesterol","Kreatin Kinaz"))),
mainPanel(
  verbatimTextOutput("summary"),
  tableOutput("view"))))

server.R文件

library(shiny)
shinyServer(

  function(input,output){

datasetInput <- reactive({
  switch(input$dataset,
         "Albumin"=albumin,
         "Bilirubin"=biliribun,
         "CL(KLOR)"=clklor,
         "Glukoz"=glukoz,
         "Kalsiyum" = kalsiyum,
         "Kolesterol" = kolesterol,
         "Kreatinin" = kreatinin,
         "Kreatin Kinaz" = kreatinkinaz,
         "Potasyum" = potasyum,
         "Protein" = protein,
         "Trigliserid"=trigliserid,
         "Hdl Kolesterol"=hdlkolesterol,
         "Kreatin Kinaz"=kreatinkinaz)

})


# Generate a summary of the dataset ----
output$summary <- renderPrint({
  dataset <- datasetInput()
  summary(dataset)
})})

我如何用验血值及其日期来实现ps图?我有13个血液值作为数据框(日期,值名称,结果,单位,间隔) 像(2019-01-28,钾,4.00,mmol / L,3.5-5.2)

1 个答案:

答案 0 :(得分:0)

您似乎是Shiny的新手,所以我为您编写了工作示例,以便您继续研究。

我还根据您的描述创建了虚拟数据集,并将它们堆叠在一起成为一个data.frame。

有关绘制时间序列以及任何其他类型的图表的信息,请查看ggplot2。要通过一些变量对系列进行子集化,您将需要dplyr。两者都是由RStudio团队创建的。

欢迎来到闪亮的世界!

library(shiny)
library(dplyr)
library(ggplot2)

potasyum_data <- data.frame(date=seq(as.Date("2019/6/1"), by = "day", length.out = 30),
                          value_name = rep("Potasyum", 30),
                          result=sample(1:100, 30))

protein_data <- data.frame(date=seq(as.Date("2019/6/1"), by = "day", length.out = 30),
                            value_name = rep("Protein", 30),
                            result=sample(1:100, 30))     

stack_data <- rbind(potasyum_data, protein_data)

ui <- fluidPage(
  titlePanel(h2("Blood Test Result System",align = "center")),
  sidebarLayout(    
    sidebarPanel(

      selectInput(inputId = "dataset",
                  label = "Choose a blood value:",
                  choices = c("Potasyum", "Protein"),
                  selected = "Protein")),
    mainPanel(
      plotOutput("ts_plot"),
      verbatimTextOutput("summary"))))

server <- shinyServer(

  function(input,output){

    datasetInput <- reactive({
      stack_data %>% filter(value_name == input$dataset)
    })


    # Generate a summary of the dataset ----
    output$summary <- renderPrint({
      dataset <- datasetInput()
      summary(dataset$result)
    })

    # plot time series
    output$ts_plot <- renderPlot({

      dataset <- datasetInput()
      ggplot(dataset, aes(x = date, y=result)) + geom_line()

    })
  })



shiny::shinyApp(ui = ui, server = server)