警告:as.vector 中的错误:在 Shiny 上运行时,无法将类型 'environment' 强制转换为 R 中 'character' 类型的向量。我能做什么?



我试图通过获取Google Analytics数据在shiny上运行R程序,但是我收到此错误:

警告:as.vector 中的错误:无法将类型"环境"强制转换为类型为"字符"的向量

堆栈跟踪(最里面的第一个):100:作为字符.
默认值 99:作为字符 98:标记列表 97:作为标签.默认 96:作为.标签 95:继承 94:isTag 93:重写标签 92:标记 91:功能90:乐趣 89:l申请 88:乐趣 87:l申请 86:重写标签












85: 标记 84:函数



83:重写标签
82:标记
81:查找依赖项
80:解析依赖关系
79:l应用
78:进程Deps
77:原始渲染函数
76:输出$viewid
1:运行应用

我能做些什么来解决它?这是代码:


#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 中的plotOutput()。R 需要一个绘图对象,但你却为它提供了大量反应性对象。 服务器中renderPlot()函数的最终对象。R 必须是绘图。

您还要求它做很多工作,这些工作已经具有可以为您完成的功能,例如创建一个帐户下拉列表以选择Google Analytics视图ID。 谷歌分析R网站上提供了其工作版本,复制如下:

在服务器中。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))
})
}

用户界面。R

library(googleAuthR)
library(googleAnalyticsR)
library(shiny)
library(highcharter)
shinyUI(
fluidPage(
googleAuthUI("login"),
authDropdownUI("auth_menu"),
highchartOutput("something")
))

相关内容

最新更新