2016-04-30 7 views
2

Ich bin neu in R-glänzende Apps, Meine Anwendung ist sehr einfach. Es hat zwei Registerkarten, in der ersten, lade ich eine Datei wie csv, dann in der zweiten Registerkarte, wähle ich die Spalten, die geplottet werden, Meine Lösung ist über mehrere Beispiele verstreut jedes ist nicht das gleiche wie meins, ich will das hochgeladenes Dataset zu sehen und kann in allen Funktionen nicht nur beim Hochladen verwendet werden.Wie kann hochgeladenes Dataset mit Glanz dargestellt werden?

mein server.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') 
}) 
}) 

ui.R

library(shiny) 
library(datasets) 
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')) 

    ), 
      mainPanel(
      tableOutput('contents') 
      ) 
     ) 
), 
tabPanel("First Type", 
     pageWithSidebar(
      headerPanel('My First Plot'), 
      sidebarPanel(
      selectInput('xcol', 'X Variable', names(content)), 
      selectInput('ycol', 'Y Variable', names(content), 
         selected=names(content)[[2]]) 
      ), 
      mainPanel(
      plotOutput('MyPlot') 
      ) 
     ) 
) 

) 
) 
) 

Ich habe auf, dass es versucht, aber ich bin gerade erst anfangen, dass dem so ist, was soll ich tun, bitte?

+0

Um Hilfe zu erhalten, empfehle ich Ihnen, eine .csv-Datei zur Verwendung in Ihrer glänzenden App bereitzustellen. – aelwan

Antwort

4

Sie können einen reaktiven Datensatz erstellen (z. B. data), in dem Ihre App die hochgeladene Datei liest und Eingaben aktualisiert - in diesem Fall Namen eines Datenrahmens und diese an die Funktionen render* weiterleiten. Ich habe im Code einige ausführlichere Kommentare abgegeben.

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) 
+1

Eingaben, die auf numerische Variablen beschränkt sind: 'updateSelectInput (Sitzung, inputId = 'xcol', label = 'X Variable', Auswahl = Name (df), selected = Name (df) [sapply (df, is.numeric)]) ' –

+0

erstaunliche Antwort! –

Verwandte Themen