2017-07-13 2 views
0

Ich möchte eine glänzende App erstellen, um Scatterplots zu zeichnen, indem ich die Variablen im hochgeladenen Datensatz auf die glänzende App auswähle. Ich möchte Plots mit ggplot und plotly erstellen. Der Code, den ich ausprobiert habe, ist wie folgt. Aber meine App gibt nicht die Handlung.Erstellen einer ShinyApp zum Zeichnen von Streudiagrammen mit ggplot

library(shiny) 
library(datasets) 
library(plotly) 

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')), 


           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 <- function(input, output, session) { 

    data <- reactive({ 
     req(input$file1) 

     inFile <- input$file1 

     df <- read.csv(inFile$datapath, header = input$header, sep = input$sep, 
            quote = input$quote) 


     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$trendPlot <- renderPlotly({ 

     # build graph with ggplot syntax 
     p <- ggplot(data(), aes_string(x = input$xcol, y = input$ycol)) + geom_point() 

     ggplotly(p) 

    }) 
} 

shinyApp(ui, server) 

In dem obigen Code scheint der folgende Abschnitt nicht zu funktionieren.

output$trendPlot <- renderPlotly({ 

      # build graph with ggplot syntax 
      p <- ggplot(data(), aes_string(x = input$xcol, y = input$ycol)) + geom_point() 

      ggplotly(p) 

     }) 
+0

Sie besser helfen, wenn zuerst bekommen Sie nach unten versuchen verengen, welcher Abschnitt des Codes funktioniert nicht und was das spezifische Problem ist –

Antwort

1

Eigentlich ist das Problem nicht in output$trendPlot ist aber in mainPanel(...)

1: Schauen Sie genau hin, in mainPanel, die Sie anrufen plotOutput('MyPlot') aber output$MyPlot existiert nicht, nur Plot, der output$trendPlot ist vorhanden.

2: Hinweis output$trendPlot <- renderPlotly({...}) ist Rückgabetyp trendPlotrenderPlotly aber plotOutput bei main wird genannt.

Also, fix ist

mainPanel(
    plotlyOutput('trendPlot') 
) 

Nach dieser Fixierung, wird die Ausgabe wie folgt:

snap1

Verwandte Themen