2015-04-30 6 views
6

Sto lavorando alla mia prima app lucida e sto riscontrando un problema in cui i dati utilizzati per il rendering della mia tabella di dati non vengono rilevati da shinyapps.io.Oggetto dati non trovato quando si distribuisce l'app lucida

L'app funziona correttamente nella mia console, ma quando la schiero l'app si apre in un browser con l'errore: oggetto "Pitchers" non trovato, dove "Pitchers" è uno dei miei oggetti dati.

Ho trovato one suggestion che diceva di inserire i dati in una cartella all'interno della cartella dell'app, ma che ancora non funziona.

Ecco il mio codice server.R corrente:

shinyServer(function(input, output) { 

    Pitchers <- read.csv("data/Pitchers_Edge.csv", header=TRUE, check.names = FALSE) 
    Batters <- read.csv("data/Batters_Edge.csv", header=TRUE, check.names = FALSE) 

    output$table1 <- renderDataTable({ 

    if (input$Year != "All"){ 
     Pitchers <- Pitchers[Pitchers$Year == input$Year,] 
    } 
    Pitchers 
    }) 

    output$table2 <- renderDataTable({ 

    if (input$Year != "All"){ 
     Batters <- Batters[Batters$Year == input$Year,] 
    } 
    Batters 
    }) 

}) 

Ed ecco il codice ui.R:

shinyUI(fluidPage(
    titlePanel('Edge%: 2010-Present'), 
    fluidRow(
     column(12, 

        p("Provides current and historical data on the percentage of pitches thrown to different parts of the strike zone by pitchers and to batters"), 
        p("Created and maintained by Bill Petti", 
         a("(@BillPetti)", href = "https://twitter.com/billpetti")), 
        p("Data last updated",Sys.time())) 
    ), 
    fluidRow(
    column(5, 
      selectInput("Year", 
         "Year:", 
         c("All", 
         unique(as.character(Pitchers$Year))))) 
    ), 
    mainPanel(
     tabsetPanel(
     tabPanel("Pitchers", dataTableOutput(outputId = 'table1')), 
     tabPanel("Batters", dataTableOutput(outputId = 'table2')), 
     tabPanel("About", 
      br(), h1("About Edge%"), 
      br(), p("A few years ago, Jeff Zimmerman and I created a metric to represent how often a pitcher threw to the edges of the strike zone compared to the heart of the strike zone. The result was Edge%. The metric has evolved to include separate metrics for different edges (upper, lower, etc.). In the image below, the brown shaded areas represent the horizontal edges of the strike zone, the blue the top, and the green the bottom edges. You will notice the horizontal edges differ by batter handedness, given how umpires actually call balls and strikes."), 
      br(), img(src = "edge_image.png", height = 350, width = 700)), 
      br(), p("Edge% is useful in a number of contexts. For example, we know that as pitchers age they lose velocity and therefore need to avoid throwing to the heart of the plate to be successful. Edge% provides a quick look at who is adjusting to lower velocity and who isn't. It can also be used to see how pitchers are adjusting to hitters as they age (i.e. as hitters improve, pitchers may avoid the heart of the plate more, or as hitters decline they may begin challenge them more."), 
      br(), p("For more information on Edge%, check out these articles:", 
      br(), br(), a("Introduction to Edge%", href = "http://www.fangraphs.com/blogs/the-difference-pitching-on-the-edge-makes/"), 
      br(), br(), a("Collection of Articles Using and Expanding on Edge%", href = "http://www.fangraphs.com/blogs/category/edge/"), 
      br(), br(), a("Most Recent Version", href = "http://www.hardballtimes.com/expanding-the-edges-of-the-strike-zone/") 
                                                                                                                                                                            ) 
       ) 
) 
) 
) 

sto schierando da un PC Windows.

Apprezzerei qualsiasi suggerimento!

Aggiornamento

ho preso il consiglio di Colin e aggiornato il mio codice, ma sono ancora in esecuzione nello stesso problema.

Ecco cosa la mia app dovrebbe essere simile se implementata:

http://i.stack.imgur.com/idmvC.jpg

Ed ecco cosa succede quando pubblico e la vista, sia a livello locale o sul web:

http://i.stack.imgur.com/ikBrD.jpg

E ' non è solo che l'oggetto è mancante, ma posso vedere il resto dell'app. L'intera app scompare.

Ecco il mio codice aggiornato:

library(shiny) 

shinyServer(function(input, output) { 

    Pitchers <- reactive({read.csv("data/Pitchers_Edge.csv", header=TRUE, check.names = FALSE)}) 
    Batters <- reactive({read.csv("data/Batters_Edge.csv", header=TRUE, check.names = FALSE)}) 

    output$table1 <- renderDataTable({ 
    Pitchers <- Pitchers() 
    if (input$Year != "All"){ 
     Pitchers <- Pitchers[Pitchers$Year == input$Year,] 
    } 
    subset(Pitchers, Pitchers$'# of total pitches'>= input$pitch_total) 
    }) 

    output$table2 <- renderDataTable({ 
    Batters <- Batters() 
    if (input$Year != "All"){ 
     Batters <- Batters[Batters$Year == input$Year,] 
    } 
    subset(Batters, Batters$'# of total pitches'>= input$pitch_total) 
    }) 

}) 

shinyUI(fluidPage(
    titlePanel('Edge%: 2010-Present'), 
    fluidRow(
    column(12, 
      p("Provides current and historical data on the percentage of pitches thrown to different parts of the strike zone by pitchers and to batters"), 
      p("Created and maintained by Bill Petti", 
      a("(@BillPetti)", href = "https://twitter.com/billpetti")), 
      p("Data last updated",Sys.time())) 
), 
    sidebarLayout(
    sidebarPanel(selectInput("Year", 
            "Year:", 
            c("All", 
             unique(as.character(Pitchers$Year)))), 
       numericInput("pitch_total", 
          label = "Minimum # of Pitches:", 
          value = 300) 
) 
    , 
    mainPanel(
    tabsetPanel(
     tabPanel("Pitchers", dataTableOutput(outputId = 'table1')), 
     tabPanel("Batters", dataTableOutput(outputId = 'table2')), 
     tabPanel("About", 
       br(), h1("About Edge%"), 
       br(), p("A few years ago, Jeff Zimmerman and I created a metric to represent how often a pitcher threw to the edges of the strike zone compared to the heart of the strike zone. The result was Edge%. The metric has evolved to include separate metrics for different edges (upper, lower, etc.). In the image below, the brown shaded areas represent the horizontal edges of the strike zone, the blue the top, and the green the bottom edges. You will notice the horizontal edges differ by batter handedness, given how umpires actually call balls and strikes."), 
       br(), img(src = "edge_image.png", height = 350, width = 700), 
     br(), p("Edge% is useful in a number of contexts. For example, we know that as pitchers age they lose velocity and therefore need to avoid throwing to the heart of the plate to be successful. Edge% provides a quick look at who is adjusting to lower velocity and who isn't. It can also be used to see how pitchers are adjusting to hitters as they age (i.e. as hitters improve, pitchers may avoid the heart of the plate more, or as hitters decline they may begin challenge them more."), 
     br(), p("For more information on Edge%, check out these articles:"), 
       br(), a("Introduction to Edge%", href = "http://www.fangraphs.com/blogs/the-difference-pitching-on-the-edge-makes/"), 
       br(), br(), a("Collection of Articles Using and Expanding on Edge%", href = "http://www.fangraphs.com/blogs/category/edge/"), 
       br(), br(), a("Most Recent Version", href = "http://www.hardballtimes.com/expanding-the-edges-of-the-strike-zone/") 
    ) 
    ))))) 

risposta

6

Oh! Penso che tu non stia caricando il file dei lanciatori come parte dello script ui.R. Quindi quando stai definendo l'input dell'anno non è possibile trovare $ Pitchers. Puoi provare a leggere il file di pitcher nello script ui sopra la chiamata shinyUI()?

Alcune informazioni sui dati danneggiati in termini di lucentezza: se i dati non cambieranno regolarmente, non è necessario che i dati in uso siano reattivi. Basta inserirlo all'inizio del file del server prima della chiamata shinyServer(). Quella parte del codice viene eseguita una sola volta, quando si distribuisce l'app, quindi è un buon posto per fare qualsiasi chiamata library(), dati ingiustificati o pre-elaborazione che non dipendono in alcun modo dall'utente. Puoi semplicemente usare read.csv() nello stesso modo in cui lo faresti nella normale R, ma a volte è anche utile salvare un binay del dataframe perché read.csv può essere un po 'lento.

Se sta per cambiare (come un csv aggiornato verrà inserito nel file regolarmente), quindi inserire la chiamata read.csv() sotto la chiamata shinyServer() ma di nuovo non dovrebbe essere necessario essere reattivi.

Spero che questo aiuti!

+1

Certo fatto! Grazie, sembrava risolvere il problema. Il mio codice finale è [qui] (https://github.com/BillPetti/Scheduling-Shiny-App), se a qualcuno importa. – BillPetti

1

Una volta dentro la funzione renderDataTable, non riconoscere ciò che Brocche è perché è stata definita al di fuori della funzione. Tuttavia, se si imposta Pitchers come elemento reattivo, può essere richiamato all'interno della funzione. Quindi impostare la variabile e quindi chiamarla come una funzione all'interno della funzione di rendering.

Pitchers<- reactive({read.csv("Your Stuff")}) 

output$table1 <- renderDataTable(
Pitch<-Pitchers() 

"Then call all your if statements and such on the function using Pitch" 
) 

Spero che ti aiuti!

+0

Grazie, Colin! Sto ancora avendo il problema - ho aggiornato il mio codice e fornito collegamenti alle immagini di ciò che sta accadendo. Sembra che potrebbe essere un problema più grande dal momento che l'app sembra letteralmente scomparire quando mi schiero sul web. – BillPetti

0

Portare in dono una risposta precedente:

Sono d'accordo che il problema è che l'oggetto "Brocche" deve essere definito sia shinyServer e shinyUI. La creazione di un terzo file chiamato "global.R" potrebbe essere il modo migliore per risolvere questo problema, poiché in questo modo è sufficiente leggere il database una sola volta (che è più veloce e più bello per il server shinyapps.io gratuito). Vedere https://shiny.rstudio.com/articles/scoping.html per ulteriori informazioni sulla creazione di variabili con un ambito globale.

Problemi correlati