Reputation: 31
I am trying to run a R program on shiny by fetching Google Analytics data, but I am getting this error:
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
What can I do to solve it? Here is the code:
#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")
)
)
Upvotes: 3
Views: 8405
Reputation: 2631
The plotOutput()
in the ui.R expects a plot object, but you are providing it lots of reactive objects instead. The end object of the renderPlot()
function in server.R must be a plot.
You are also asking it do to a lot of work which already has functions that can do it for you, such as creating an account dropdown to choose the Google Analytics view ID. A working version of this is available on the googleAnalyticsR website which is replicated below:
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))
})
}
library(googleAuthR)
library(googleAnalyticsR)
library(shiny)
library(highcharter)
shinyUI(
fluidPage(
googleAuthUI("login"),
authDropdownUI("auth_menu"),
highchartOutput("something")
))
Upvotes: 4