R 如何使用shiny绘制上传的数据集?

R 如何使用shiny绘制上传的数据集?,r,ggplot2,shiny,rstudio,R,Ggplot2,Shiny,Rstudio,我是R-Shining应用程序的新手,我的应用程序非常简单。它有两个选项卡,在第一个选项卡中,我上传一个文件,如csv,然后在第二个选项卡中,我选择要绘制的列,我的解决方案分散在几个示例中,每个示例与我的不同,我希望上传的数据集能够被看到,并且可以在所有功能中使用,而不仅仅是在上传时 我的服务器.R library(shiny) shinyServer(function(input, output) { output$contents <- renderTable({ inFile <

我是R-Shining应用程序的新手,我的应用程序非常简单。它有两个选项卡,在第一个选项卡中,我上传一个文件,如csv,然后在第二个选项卡中,我选择要绘制的列,我的解决方案分散在几个示例中,每个示例与我的不同,我希望上传的数据集能够被看到,并且可以在所有功能中使用,而不仅仅是在上传时

我的服务器.R

library(shiny)
shinyServer(function(input, output) {
output$contents <- renderTable({
inFile <- input$file1

if (is.null(inFile))
  return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep, 
         quote=input$quote)

})
output$MyPlot <- renderPlot({
x    <- contents()$contents[, c(input$xcol, input$ycol)]
bins <- nrow(contents())
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})

我已经尝试过了,但我刚刚开始,那么,我应该怎么做呢?

您可以创建一个反应式数据集(例如
数据
),在该数据集中,您的应用程序读取上传的文件并更新输入-在本例中,数据帧的名称并将其传递给
渲染*
函数。我在代码中做了一些更详细的注释

library(shiny)
library(datasets)

ui <- shinyUI(fluidPage(
  titlePanel("Column Plot"),
  tabsetPanel(
    tabPanel("Upload File",
             titlePanel("Uploading Files"),
             sidebarLayout(
               sidebarPanel(
                 fileInput('file1', 'Choose CSV File',
                           accept=c('text/csv', 
                                    'text/comma-separated-values,text/plain', 
                                    '.csv')),

                 # added interface for uploading data from
                 # http://shiny.rstudio.com/gallery/file-upload.html
                 tags$br(),
                 checkboxInput('header', 'Header', TRUE),
                 radioButtons('sep', 'Separator',
                              c(Comma=',',
                                Semicolon=';',
                                Tab='\t'),
                              ','),
                 radioButtons('quote', 'Quote',
                              c(None='',
                                'Double Quote'='"',
                                'Single Quote'="'"),
                              '"')

               ),
               mainPanel(
                 tableOutput('contents')
               )
             )
    ),
    tabPanel("First Type",
             pageWithSidebar(
               headerPanel('My First Plot'),
               sidebarPanel(

                 # "Empty inputs" - they will be updated after the data is uploaded
                 selectInput('xcol', 'X Variable', ""),
                 selectInput('ycol', 'Y Variable', "", selected = "")

               ),
               mainPanel(
                 plotOutput('MyPlot')
               )
             )
    )

  )
)
)

server <- shinyServer(function(input, output, session) {
    # added "session" because updateSelectInput requires it


  data <- reactive({ 
    req(input$file1) ## ?req #  require that the input is available

    inFile <- input$file1 

    # tested with a following dataset: write.csv(mtcars, "mtcars.csv")
    # and                              write.csv(iris, "iris.csv")
    df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
             quote = input$quote)


    # Update inputs (you could create an observer with both updateSel...)
    # You can also constraint your choices. If you wanted select only numeric
    # variables you could set "choices = sapply(df, is.numeric)"
    # It depends on what do you want to do later on.

    updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                      choices = names(df), selected = names(df))
    updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                      choices = names(df), selected = names(df)[2])

    return(df)
  })

  output$contents <- renderTable({
      data()
  })

  output$MyPlot <- renderPlot({
    # for a histogram: remove the second variable (it has to be numeric as well):
    # x    <- data()[, c(input$xcol, input$ycol)]
    # bins <- nrow(data())
    # hist(x, breaks = bins, col = 'darkgray', border = 'white')

    # Correct way:
    # x    <- data()[, input$xcol]
    # bins <- nrow(data())
    # hist(x, breaks = bins, col = 'darkgray', border = 'white')


    # I Since you have two inputs I decided to make a scatterplot
    x <- data()[, c(input$xcol, input$ycol)]
    plot(x)

  })
})

shinyApp(ui, server)
库(闪亮)
图书馆(数据集)

ui为了获得帮助,我建议您提供一个.csv文件,用于您闪亮的应用程序。输入限制为数值变量:
updateSelectInput(会话,输入ID='xcol',标签='X Variable',choices=names(df),selected=names(df)[sapply(df,is.numeric)]
惊人的答案!绝对精彩的回答
library(shiny)
library(datasets)

ui <- shinyUI(fluidPage(
  titlePanel("Column Plot"),
  tabsetPanel(
    tabPanel("Upload File",
             titlePanel("Uploading Files"),
             sidebarLayout(
               sidebarPanel(
                 fileInput('file1', 'Choose CSV File',
                           accept=c('text/csv', 
                                    'text/comma-separated-values,text/plain', 
                                    '.csv')),

                 # added interface for uploading data from
                 # http://shiny.rstudio.com/gallery/file-upload.html
                 tags$br(),
                 checkboxInput('header', 'Header', TRUE),
                 radioButtons('sep', 'Separator',
                              c(Comma=',',
                                Semicolon=';',
                                Tab='\t'),
                              ','),
                 radioButtons('quote', 'Quote',
                              c(None='',
                                'Double Quote'='"',
                                'Single Quote'="'"),
                              '"')

               ),
               mainPanel(
                 tableOutput('contents')
               )
             )
    ),
    tabPanel("First Type",
             pageWithSidebar(
               headerPanel('My First Plot'),
               sidebarPanel(

                 # "Empty inputs" - they will be updated after the data is uploaded
                 selectInput('xcol', 'X Variable', ""),
                 selectInput('ycol', 'Y Variable', "", selected = "")

               ),
               mainPanel(
                 plotOutput('MyPlot')
               )
             )
    )

  )
)
)

server <- shinyServer(function(input, output, session) {
    # added "session" because updateSelectInput requires it


  data <- reactive({ 
    req(input$file1) ## ?req #  require that the input is available

    inFile <- input$file1 

    # tested with a following dataset: write.csv(mtcars, "mtcars.csv")
    # and                              write.csv(iris, "iris.csv")
    df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
             quote = input$quote)


    # Update inputs (you could create an observer with both updateSel...)
    # You can also constraint your choices. If you wanted select only numeric
    # variables you could set "choices = sapply(df, is.numeric)"
    # It depends on what do you want to do later on.

    updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                      choices = names(df), selected = names(df))
    updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                      choices = names(df), selected = names(df)[2])

    return(df)
  })

  output$contents <- renderTable({
      data()
  })

  output$MyPlot <- renderPlot({
    # for a histogram: remove the second variable (it has to be numeric as well):
    # x    <- data()[, c(input$xcol, input$ycol)]
    # bins <- nrow(data())
    # hist(x, breaks = bins, col = 'darkgray', border = 'white')

    # Correct way:
    # x    <- data()[, input$xcol]
    # bins <- nrow(data())
    # hist(x, breaks = bins, col = 'darkgray', border = 'white')


    # I Since you have two inputs I decided to make a scatterplot
    x <- data()[, c(input$xcol, input$ycol)]
    plot(x)

  })
})

shinyApp(ui, server)