具有上载CSV的多线图

时间:2019-07-17 11:01:44

标签: r ggplot2 shiny

我希望能够显示带有导入的csv的多线图。 CSV文件包含时间序列。导入时,我希望能够选择,知道字段名称可以根据CSV进行更改,该字段表示X和Y之一,并定义包含ID的字段,该ID将创建各行。像这样的东西: enter image description here

现在,我有了这个,但是完全错了

# ui.R

library(shiny)
library(shinydashboard)
library(ggplot2)

shinyUI(
  dashboardPage(
    dashboardHeader(title ="Sen2extract"),
    sidebar <- dashboardSidebar(
      sidebarMenu(
        menuItem("Chart", tabName = "chart")
      )
    ),
    dashboardBody(
        tabItem(tabName = "chart",
                box(
                    width = 12, collapsible=FALSE,
                    fileInput(inputId = "csv_chart", label = "Upload your CSV", multiple = FALSE, 
                              accept = c(".csv", "text/csv", "text/comma-separated-values,text/plan"), width = "300px"),
                    selectInput("X", label = "Field X :", choices = list("Choice 1" = "")),
                    selectInput("Y", label = "Field Y :", choices = list("Choice 1" = "")),
                    selectInput("group", label = "Group by :", choices = list("Choice 1" = ""))
                    ),
                box(plotOutput("plot"), width = 12)
        )
    )
  )
)

    # server.R

    library(shiny)
    library(shinydashboard)
    library(ggplot2)

    shinyServer(function(input, output, session){

      output$plot = renderPlot({
        data <- read.csv(file = input$csv_chart)
        ggplot(data) +
          geom_line(mapping = aes(x = input$X, y = input$Y)) +
          labs (x = "Years", y = "", title = "Index Values")
      })
    })

1 个答案:

答案 0 :(得分:1)

您的代码有几个问题,下面有一个有效的版本。 主要问题是必须读取reactive()中的数据,然后更新选择。另外,要在图形中包含多条线,必须在aes或本例中的aes_string中定义映射时,在ggplot中添加要分组的内容。我选择了color,因为这样可以根据所选列中的不同组为多行着色。

library(shiny)
library(shinydashboard)
library(tidyverse)

ui <- dashboardPage(
        dashboardHeader(title ="Sen2extract"),
        sidebar <- dashboardSidebar(
            sidebarMenu(
                menuItem("Chart", tabName = "chart")
            )
        ),
        dashboardBody(
            tabItem(tabName = "chart",
                    box(
                        width = 12, collapsible=FALSE,
                        fileInput(inputId = "csv_chart", label = "Upload your CSV",
                                  multiple = FALSE, 
                                  accept = c(".csv",
                                             "text/csv",
                                             "text/comma-separated-values,text/plan"),
                                  width = "300px"),
                        selectInput("X", label = "Field X:", choices = "Pending Upload"),
                        selectInput("Y", label = "Field Y:", choices = "Pending Upload"),
                        selectInput("group", label = "Group by:", choices = "Pending Upload")
                    ),
                    box(plotOutput("plot"), width = 12)
            )
        )
    )

server <- function(input, output, session){
    data <- reactive({
        req(input$csv_chart)
        infile <- input$csv_chart
        if (is.null(infile))
            return(NULL)
        df <- read_csv(infile$datapath)
        updateSelectInput(session, inputId = 'X', label = 'Field X:',
                          choices = names(df), selected = names(df)[1])
        updateSelectInput(session, inputId = 'Y', label = 'Field Y:',
                          choices = names(df), selected = names(df)[2])
        updateSelectInput(session, inputId = 'group', label = 'Group by:',
                          choices = names(df), selected = names(df)[3])
        return(df)
    })

    output$plot <- renderPlot({
        ggplot(data()) +
            geom_line(mapping = aes_string(x = input$X, y = input$Y, color=input$group)) +
            labs(x = "Years", y = "", title = "Index Values")
    })
}

shinyApp(ui = ui, server = server)