Как получить значения 2 указанных c столбцов из функции selectInput () в качестве заголовка столбца и построить эти столбцы? - PullRequest
0 голосов
/ 04 февраля 2020

Вот код, который у меня есть:

library(shiny)
library(ggplot2)

ui <- fluidPage(
    fluidRow(
        headerPanel("CSV Viewer & Visualiser"),
        sidebarPanel(
            fileInput('fileData', 'Choose CSV File',
                     accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
            width = 3,
            selectInput("x", "X-Axis", list()),
            selectInput("y", "Y-Axis:", list()),
        ),
        mainPanel(
            tableOutput('contents'),
        ),
    )
)

Реальная проблема, с которой я столкнулся, в разделе сервера, я не мог понять, как получить весь столбец CSV из updateSelectInput как мои оси X и Y, чтобы построить.


server <- function(input, output, session){

    output$contents <- renderTable({
        inFile <- input$fileData

        if (is.null(inFile))
            return(NULL)

        df <- read.csv(inFile$datapath)
        updateSelectInput(session,"x",choices=colnames(df))
        updateSelectInput(session,"y",choices=colnames(df))

        p1 <- ggplot() +
            geom_area(aes(y = , x = ), data = df)

        p1 + labs(title = "", x = "", y = "")

        return(df)
    })    
}
shinyApp(ui, server)

Я новичок в R, я был бы признателен за помощь, спасибо.

1 Ответ

0 голосов
/ 04 февраля 2020

Вам нужно добавить вывод сюжета:

library(shiny)
library(ggplot2)

ui <- fluidPage(
  fluidRow(
    headerPanel("CSV Viewer & Visualiser"),
    sidebarPanel(
      fileInput('fileData', 'Choose CSV File',
                accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
      width = 3,
      selectInput("x", "X-Axis", list()),
      selectInput("y", "Y-Axis:", list()),
    ),
    mainPanel(
      plotOutput("plot"),
      tableOutput('contents'),
    ),
  )
)

server <- function(input, output, session){

  val <- reactiveValues()

  observeEvent(input$fileData,{
    val$data <- read.csv(input$fileData$datapath)


    updateSelectInput(session,"x",choices=colnames(val$data))
    updateSelectInput(session,"y",choices=colnames(val$data))
  })

  output$contents <- renderTable({
   validate(need(val$data,''))


    val$data
  })    

  output$plot <- renderPlot({
    validate(need(val$data,''),
             need(input$y,''),
             need(input$x,''))

    p1 <- ggplot() +
      geom_area(aes(x = !!sym(input$x), y = !!sym(input$y)), data = val$data)
    p1 + labs(title = "", x = "", y = "")

  })
}
shinyApp(ui, server)
...