我有以下数据框,我从谷歌分析
中提取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有关。
对这里可能出现什么问题的任何想法?
答案 0 :(得分: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)})
}
)