2017-09-26 75 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)}) 
    } 
) 
相关问题