Getting started with RGoogleAnalytics - r

I've run the following code (according to http://online-behavior.com/analytics/r ) but get the error
Error in QueryBuilder() :
argument "query.params.list" is missing, with no default
All packages installed successfully, and a googling the error did not produce helpful advice. Any pointers? Thanks.
# run <sudo apt-get install libcurl4-openssl-dev> beforehand
rm(list=ls())
setwd('~')
install.packages(c("RCurl", "httr" , "rjson" , "RGoogleAnalytics", "ggplot2","plyr",
"gridExtra", "reshape" , "RColorBrewer"))
require("RCurl")
require("rjson")
require("ggplot2")
require("plyr")
require("gridExtra")
require("reshape")
require("RGoogleAnalytics")
query <- QueryBuilder()
access_token <- query$authorize()

Related

Error message when using ggcorrplot() and cor_pmat()

I have trouble with the following code:
library(ggplot2)
library(ggcorrplot)
data(USArrests)
correlation_matrix <- round(cor(USArrests),1)
corrp.mat <- cor_pmat(USArrests)
ggcorrplot(correlation_matrix, hc.order =TRUE, type ="lower",
p.mat = corrp.mat)
When I run the code, execution stops at ggcorrplot(...) and I get this error:
"Error in Math.data.frame(x = list(Assault = c(0.0695, 1.36e-07,
2.6e-12, : non-numeric-alike variable(s) in data frame: rowname"
I tried to run the code in an online R runner and it worked, but in RStudio it doesn´t.
I have no clue whats going on, has somebody an idea?
Thank you #Quinten for your suggestion. Unloading the unnecessary packages helped. The package rstatix seemed to interfere with the cor_pmat() command.

Check if package installation is required while running code via source()?

I am running several scripts in RStudio and checking syntax errors. I am using source() in a loop to perform those tasks. In some scripts, install.packages("packagename") occurs. My problem is that when i have the required packages already installed in my computer, a message pops up asking me to update the library. In these cases, I would like to be able to "ignore" install.packages("packagename") call and running code to continue without showing any message.
So, how can i check if package installation is required or not while running code via source()?
Bit of a hack, but given the location file, this will list all the packages inside the script:
require(readr)
require(stringr)
listPackages <- function(file)
{
r <- readr::read_file(file)
r = str_replace_all(r, '\\"', "") # remove all quote marks
packages <- str_extract_all(r, regex("(install.packages|library|require|p_load)\\([:alnum:]*\\)*", multiline = TRUE))[[1]]
return(unique(gsub("\\(", "", str_extract(packages, regex("\\([:alnum:]*", multiline = F)))))
}
Example
test.R:
library("ggplot2")
library(stats)
require("cowplot")
require(MASS)
add <- function(x,y) {x+y}
install.packages("cowplot")
p_load(dplyr)
p_load("dplyr")
listPackages("test.R")
# [1] "ggplot2" "stats" "cowplot" "MASS" "cowplot" "dplyr"

Error: 'set_envvar' is not an exported object from 'namespace:xfun'

I'm trying to run Data Exploration in my R Studio, version 1.3.959 on my windows10.I wrote following test code
if (!require(devtools)) install.packages("devtools")
devtools::install_github("boxuancui/DataExplorer", ref = "develop")
library(DataExplorer)
diabetes_data <- read.csv("https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.csv", header = FALSE)
names(diabetes_data) <- c("number_of_times_pregnant", "plasma_glucose_conc", "diastolic_bp", "triceps_skinfold_thickness", "two_hr_serum_insulin", "bmi", "diabetes_pedigree_function", "age", "label")
# create report
create_report(diabetes_data)
But I'm getting error message
Error: 'set_envvar' is not an exported object from 'namespace:xfun'
Can you please help me to resolve the issue?
I just had this problem and updating my packages solved the issue, especially the package xfun from which set_envvar is originated.

Error in seas(AirPassengers) : no output has been generated

I am getting this error when I run:
library(seasonal)
m <- seas(AirPassengers)
I have installed the package.
I have uninstalled R, Rstudio, reinstalled and tried again, same error.
Could someone help?
Thanks
I can't reproduce exactly the same error now but I remember following error after I installed the seasonal package:
Error in seas(AirPassengers) : could not find function "seas"
After cleaning up my workspace the error was gone.
Following code is working for me and resulting in the plot below:
library(seasonal)
head(AirPassengers)
m <- seas(AirPassengers, regression.aictest = c("td", "easter"))
plot(m)

RStudio-only error with tcl/tk?

When I compute an ROC plot with the package pROC, I get the following error in RStudio v. 99.9.9:
## Loading required package: tcltk
## Warning: couldn't connect to display ":0"
## Error: [tcl] invalid command name "toplevel".
I thought this was an issue in my OS (Debian with r-base 3.1 installed from APT depending on tcl/tk lib version 8.5), but when evaluating the same code through the terminal, I get the plot rendered without the same error.
Here is a simple example following some pROC example code that will generate the error when knitted in RStudio, but works in vanilla R:
```{r markdown.test}
library(pROC)
features <- runif(25)
labels <- as.factor(runif(25) < 0.75)
lda.model <- lda(as.matrix(features),labels)
lda.pred <- predict(lda.model,as.matrix(features))
lda.roc <- plot.roc(lda.pred$posterior[,1],
c(labels),
ci=TRUE, of="se", #specificities = seq(0,100,5),
ci.type="shape",
ci.col="#1c61b6AA")
```
The error occurs when the confidence interval parameters are added to the call.
Is there something missing from my RStudio install/config, or is this a bug?
Blast from the past! I see that same error today on an instance of RStudio installed in the IBM computing environment called CP4D. Did you ever find an answer?
I think the way to fix on your Macintosh is to install Quartz and let it serve as the X11 server to display output from programs like tcltk or maybe even hist() or quartz() devices. (my team wrote install instructions https://pj.freefaculty.org/guides/crmda_guides/47.mac_R_setup).
I'm not sure what I'll do on the CP4D thing, since it is running inside a Web browser.

Resources