我试图通过获取 Google Analytics 数据在 Shiny 的 R 程序上运行 R 程序,但收到此错误:
Warning: Error in as.vector: cannot coerce type 'environment' to vector of type 'character'
Stack trace (innermost first):
100: as.character.default
99: as.character
98: tagList
97: as.tags.default
96: as.tags
95: inherits
94: isTag
93: rewriteTags
92: tagify
91: func
90: FUN
89: lapply
88: FUN
87: lapply
86: rewriteTags
85: tagify
84: func
83: rewriteTags
82: tagify
81: findDependencies
80: resolveDependencies
79: lapply
78: processDeps
77: origRenderFunc
76: output$viewid
1: runApp
我可以做什么来解决这个问题?这是代码:
<小时/>#server.r
library(googleAnalyticsR)
library(googleAuthR)
library(dplyr)
library(shiny)
library(shinydashboard)
server <- function(session,input,output){
output$histogram <- renderPlot({
ga_auth()
account_list <- ga_account_list()
View(account_list)
observe({
print(input$accountname)
x <- account_list$webPropertyName[account_list$accountName == input$accountname]
updateSelectInput(session, "propertyname","Select your property of the above account",choices = unique(x))
})
observe({
print(input$propertyname)
y <- c(account_list %>% filter(webPropertyName == input$propertyname) %>% select(viewName))
updateSelectInput(session, "viewname","Select your view of the above account",choices = y)
})
observe({
z <- c(account_list$viewId[account_list$viewName == input$viewname])
updateTextInput(session, "viewid", "view ID of the selected table is:",value = z)
})
output$viewid <- renderUI({
#idd <- input$viewid
#print(idd)
temp_ga_data = google_analytics_4(viewId= input$viewid,date_range = c("2017-11-06","2017-11-10"),
metrics = "sessions", dimensions = "date")
ggplot(data = temp_ga_data, mapping = aes(date,sessions)) + geom_line()
})
})
}
<小时/>
#ui.r
library(shiny)
library(shinydashboard)
library(googleAnalyticsR)
library(googleAuthR)
library(ggplot2)
library(datasets)
library(dplyr)
ui <- fluidPage(
titlePanel (title = "Insight of pageviews"),
sidebarPanel (
selectInput("accountname","Select your Analytics account",choices = c(account_list$accountName), selected = account_list$accountName[1]),
selectInput("propertyname","Select your property of the above account",choices = NULL),
selectInput("viewname","Select your view of the above account",choices = NULL),
textInput("viewid", "view ID of the selected table is:","")
# dateRangeInput("daterange3", "Date range:",
# start = "2017-01-01",
# end = "2017-12-31",
# min = "2017-01-01",
# max = "2017-12-31",
# format = "mm/dd/yy",
# separator = " - ")
),
mainPanel(
plotOutput("histogram"),
textOutput("viewid")
)
)
最佳答案
ui.R 中的 plotOutput()
需要一个绘图对象,但您却为其提供了大量 react 对象。 server.R 中 renderPlot()
函数的最终对象必须是一个绘图。
您还要求它完成许多工作,这些工作已经具有可以为您完成的功能,例如创建帐户下拉列表以选择 Google Analytics View ID。其工作版本可在 googleAnalyticsR website 上找到。复制如下:
在服务器中。R
library(googleAuthR)
library(googleAnalyticsR)
library(shiny)
library(highcharter)
function(input, output, session){
## Get auth code from return URL
token <- callModule(googleAuth, "login")
ga_accounts <- reactive({
req(token())
with_shiny(ga_account_list, shiny_access_token = token())
})
selected_id <- callModule(authDropdown, "auth_menu", ga.table = ga_accounts)
gadata <- reactive({
req(selected_id())
gaid <- selected_id()
with_shiny(google_analytics,
id = gaid,
start="2015-08-01", end="2017-08-02",
metrics = c("sessions"),
dimensions = c("date"),
shiny_access_token = token())
})
output$something <- renderHighchart({
## only trigger once authenticated
req(gadata())
gadata <- gadata()
## creates a line chart using highcharts
hchart(gadata, "line" , hcaes(x = date, y = sessions))
})
}
ui.R
library(googleAuthR)
library(googleAnalyticsR)
library(shiny)
library(highcharter)
shinyUI(
fluidPage(
googleAuthUI("login"),
authDropdownUI("auth_menu"),
highchartOutput("something")
))
关于r - 警告 : Error in as. 矢量 : cannot coerce type 'environment' to vector of type 'character' in R while running on the shiny. 我能做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48576181/