2017-09-26 76 views
0
繪製圖形時

我有我從谷歌分析提取下列數據幀不能強迫類「」直方圖「」一個data.frame錯誤閃亮

ga_data <- google_analytics_4(viewId = my_id, 
          date_range = c(Sys.Date()-7, Sys.Date()-1), 
          metrics = c("sessions","pageviews", 
             "entrances","bounces"), 
          dimensions = c("date","deviceCategory", 
             "channelGrouping"), 
          anti_sample = TRUE) 

現在我想展示圖在Shiny應用程序中的ga_data。因此,我有以下代碼:

library(shiny) 
library(ggplot2) 

ui <- fluidPage(
    titlePanel("Shiny Text"), 
    sidebarLayout(
    sidebarPanel(
    selectInput(inputId = "dataset", 
       label = "Choose a dataset:", 
       choices = c("ga_data")), 

    numericInput(inputId = "obs", 
       label = "Number of observations to view:", 
       value = 10) 
    ), 

    mainPanel( 

    verbatimTextOutput("summary"), 
    tableOutput("view") 

) 
) 
) 

server <- function(input, output) { 

ga_data <- google_analytics_4(viewId = 156004565, 
          date_range = c(Sys.Date()-7, Sys.Date()-1), 
          metrics = c("sessions","pageviews", 
             "entrances","bounces"), 
          dimensions = c("date","deviceCategory", 
              "channelGrouping"), 
          anti_sample = TRUE) 


    datasetInput <- reactive({ 
    switch(input$dataset, 
     "ga_data" = ga_data) 
    }) 


    output$view <- renderTable({ 

    hist(ga_data$sessions) 


}) 

} 

shinyApp(ui = ui, server = server) 

然而,當我運行它,我得到以下錯誤:

cannot coerce class ""histogram"" to a data.frame 

但是,這是奇怪的原因,當我想使數據幀的正常情節,它的工作。所以這個問題可能與Shiny有關。

有什麼想法可以在這裏出錯?

+2

你爲什麼在'tableOutput'中使用'renderTable()'來繪製圖?那些是表格。你不想''plotOutput'' renderPlot()'? – MrFlick

回答

1

由於我沒有googleAnalyticsR設置,我將你的問題簡化爲他的簡單應用程序。

library(shiny) 

shinyApp(
    fluidPage(tableOutput("table")), 
    server = function(input, output, session){ 
    output$table <- renderTable({hist(mtcars$mpg)}) 
    } 
) 
## Warning: Error in as.data.frame.default: cannot coerce class ""histogram"" to a 
## data.frame 

這裏的問題是,您嘗試使用renderTable來渲染圖。如果您改用renderPlot,則一切正常。

shinyApp(
    fluidPage(plotOutput("plot")), 
    server = function(input, output, session){ 
    output$plot <- renderPlot({hist(mtcars$mpg)}) 
    } 
) 
相關問題