how to debug R scripts - r

I´ve made a little simulator to teach the basics of population dynamics under stochasticity. This is a simple viz for the Ricker equation. It works in linux, however, I get a criptic error while running it under other environment (macos, win).
So, I wonder what my best options are to debug this error:
<simpleError in is.list(x): object of type 'closure' is not subsettable>
here is the code. I suspect on the plotly library in R... any hint?
# Ejercicio para ensenar curvas de crecimiento poblacional
# _author_ = horacio.samaniego#gmail.com
# _date_ = August 2018
# Check whether required packages are installed
list.of.packages <- c("manipulateWidget", "plotly")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
# librerias requeridas
library(manipulateWidget)
library(plotly)
# Simulador
crecLogistico <- function(t=80,r=1,n0=10,K=60,d=0){
# Simulacion de crecimiento poblacional con tasa de crecimiento (r) variable
# que simula variabilidad (estocasticidad ambiental)
# t, numero de generaciones a simular
# r, tasa intrinsica de crecimiento
# n0, abundancia inicial
# K, capacidad de carga
# d, estocasticidad
N <- matrix(n0,ncol=t,nrow=1) # vector con tamano poblacional (abundancia) inicial
R <- matrix(NA,ncol=t,nrow=1) # vector con tasas de crecimiento
for(i in 1:t) {
if(d>0) R[i] <- rnorm(1,mean=r,sd=d) else R[i] <- r
# N[i+1] <- N[i]+N[i]*R[i]*(1-N[i]/K) # Logistica
N[i+1] <- N[i]*exp(R[i]*(1 - N[i]/K)) # Ricker
}
res <- matrix(c(1:t,N[-(t+1)],R),ncol=3)
res <- as.data.frame(res)
names(res) <- c("t","Abundancia","r")
return(res)
}
## Plot de Crecimiento Logistico
if ( require(plotly) ) {
manipulateWidget({
# Modela abundancias
res = crecLogistico(n0=n0,t=t,r=r,K=K,d=d)
# Colecta informacion para construir mapa logistico
mapa = data.frame("N_1"=res$Abundancia[-t],"N_0"=res$Abundancia[-1])
combineWidgets( # crea ventanas donde plotear (proporcion 3:1). Una grande a la izq y otra con 2 lineas a la dcha (1:1)
ncol = 2,colsize=c(3,1),
# Evolucion temporal de poblacion
p = plot_ly(res[rango[1]:rango[2],],x=~t,y=~Abundancia,type="scatter",
mode="lines+markers",line=~r,color=~r), # %>%
# agrega linea para remarcar abundancia = 0
add_segments(p, x = rango[1], xend = rango[2], y = 0, yend = 0,mode="lines") %>%
# muestra escocasticidad a cada tiempo segun color
colorbar(title="Tasa de Crecimiento") %>%
layout(title="Crecimiento Logistico",showlegend = FALSE,yaxis=list(zerolinecolor=toRGB("red"))),
combineWidgets(
ncol = 1,
# histograma de estocasticidad a lo largo de todo el tiempo
plot_ly(res,x=~r,type="histogram") %>%
layout(title="Tasa de Crecimiento"),
# mapa logistico, muestra estados estacionarios en primer orden, de t-a a t
plot_ly(mapa,x=~N_1,y=~N_0,type="scatter",mode="markers") %>%
add_segments(x=0,xend=max(res$Abundancia),y=0,yend=max(res$Abundancia)) %>%
layout(title="Mapa Logistico", xaxis = list(title = "abundancia (t)"),
yaxis = list(title="abundancia (t-1)"))
)
)
},
n0 = mwNumeric(100, min = 2, step = 1 , label = "Poblacion Inicial"),
t = mwNumeric(100, min = 2, step = 1 , label = "Generaciones (t)"),
r = mwNumeric(0.9, min = -4, step = 0.05 , label = "Tasa de Crecimiento (r)"),
K = mwNumeric(60, min = 5, step = 2, label = "Capacidad de Carga"),
d = mwNumeric(0.05, min = 0, step = 0.05 ,label = "Estocasticidad"),
rango = mwSlider(0, t, c(1, t),label="Generaciones a Visualizar")
)
}
will report... thanks a bunch!!

Ok, I've got the issue... the problem is that plotly in R (or rstudio) for linux is not too picky about the 'line' flag in plot_ly, which macos strictly enforces.
I unfortunately have no training in debugging code and would not be able diagnose this using the standard tools mentioned here (will learn though!)
recasting the line to this:
plot_ly(res[rango[1]:rango[2],],x=~t,y=~Abundancia,type="scatter", mode="lines+markers",color=~r) %>%
did all the trick.
Thanks,
I'll copy my equation explorer here just in case:
# Ejercicio para ensenar curvas de crecimiento poblacional
# _author_ = horacio.samaniego#gmail.com
# _date_ = August 2018
# Check whether required packages are installed
list.of.packages <- c("manipulateWidget", "plotly")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
# librerias requeridas
library(manipulateWidget)
library(plotly)
# Simulador
crecLogistico <- function(t=80,r=1,n0=10,K=60,d=0){
# Simulacion de crecimiento poblacional con tasa de crecimiento (r) variable
# que simula variabilidad (estocasticidad ambiental)
# t, numero de generaciones a simular
# r, tasa intrinsica de crecimiento
# n0, abundancia inicial
# K, capacidad de carga
# d, estocasticidad
N <- matrix(n0,ncol=t,nrow=1) # vector con tamano poblaciona (abundancia) inicial
R <- matrix(NA,ncol=t,nrow=1) # vector con tasas de crecimiento
for(i in 1:t) {
if(d>0) R[i] <- rnorm(1,mean=r,sd=d) else R[i] <- r # adds stochasticity
# N[i+1] <- N[i]+N[i]*R[i]*(1-N[i]/K) # Logistica
N[i+1] <- N[i]*exp(R[i]*(1 - N[i]/K)) # Ricker
}
res <- matrix(c(1:t,N[-(t+1)],R),ncol=3)
res <- as.data.frame(res)
names(res) <- c("t","Abundancia","r")
return(res)
}
## Plot de Crecimiento Logistico
if ( require(plotly) ) {
manipulateWidget({
# Modela abundancias
res = crecLogistico(n0=n0,t=t,r=r,K=K,d=d)
# Colecta informacion para construir mapa logistico
mapa = data.frame("N_1"=res$Abundancia[-t],"N_0"=res$Abundancia[-1])
combineWidgets( # crea ventanas donde plotear (proporcion 3:1). Una grande a la izq y otra con 2 lineas a la dcha (1:1)
ncol = 2,colsize=c(3,1),
# Evolucion temporal de poblacion
plot_ly(res[rango[1]:rango[2],],x=~t,y=~Abundancia,type="scatter", mode="lines+markers",color=~r) %>%
# # muestra escocasticidad a cada tiempo segun color
colorbar(title="Tasa de Crecimiento") %>%
layout(title="Crecimiento Logistico",showlegend = FALSE,yaxis=list(zerolinecolor=toRGB("red"))),
combineWidgets(
ncol = 1,
# histograma de estocasticidad a lo largo de todo el tiempo
plot_ly(res,x=~r,type="histogram") %>%
layout(title="Tasa de Crecimiento"),
# mapa logistico, muestra estados estacionarios en primer orden, de t-a a t
plot_ly(mapa,x=~N_1,y=~N_0,type="scatter",mode="markers") %>%
add_segments(x=0,xend=max(res[[2]]),y=0,yend=max(res[[2]])) %>%
layout(title="Mapa Logistico", xaxis = list(title = "abundancia (t)"),
yaxis = list(title="abundancia (t-1)"),showlegend = FALSE)
)
)
},
n0 = mwNumeric(100, min = 2, step = 1 , label = "Poblacion Inicial"),
t = mwNumeric(100, min = 2, step = 1 , label = "Generaciones (t)"),
r = mwNumeric(0.9, min = -4, step = 0.05 , label = "Tasa de Crecimiento (r)"),
K = mwNumeric(60, min = 5, step = 2, label = "Capacidad de Carga"),
d = mwNumeric(0.05, min = 0, step = 0.05 ,label = "Estocasticidad"),
rango = mwSlider(0, t, c(1, t),label="Generaciones a Visualizar")
)
}

Related

Display graph with ggplot in Shiny app with a function

I'm doing right now a shiny web app, in order to plot some data that comes from csv files.
Here my code concerning the shiny app :
# install.packages("shiny")
library(shiny)
source("test.R")
# library(...) that I need
# User Interface
ui <- fluidPage(
titlePanel("Affichage de l'indice EPU"),
sidebarLayout(
sidebarPanel(
helpText("Choix des paramètres"),
selectInput("var",
label = "Choisir une variable à afficher",
choices = list("Global",
"France",
"Royaume-Uni"),
selected = "Percent White"),
dateRangeInput("dateRange",
label = "Intervalle de temps : ",
format = "mm/yyyy",
language="fr",
start = "2018-01-01",
end = Sys.Date(),
startview = "year",
separator = " - ")
),
mainPanel(plotOutput("p")
)
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
output$p <- renderPlot({
graph_epu()
})
}
shinyApp(ui = ui, server = server)
For the moment, I don't use input in User Interface function, I just want to plot the data.
My function, inside the file test.R, do some things in order to generate a graph.
Here my function :
graph_epu <- function(){
# On importe les données du csv dans les dataframes
df_lesechos <- read.csv(file = "data/df_lesechos.csv", sep=",")
df_latribune <- read.csv(file = "data/df_latribune.csv", sep=",")
# On supprime le jour pour le remplacer par 01
df_lesechos$Date <- substr(df_lesechos$Date,1,7)
df_latribune$Date <- substr(df_latribune$Date,1,7)
df_lesechos$Date <- paste(df_lesechos$Date,"-01",sep="")
df_latribune$Date <- paste(df_latribune$Date,"-01",sep="")
# Transforme la colonne Occurences au format numérique
#df_lesechos <- transform(df_lesechos, Occurences = as.numeric(Occurences))
#df_latribune <- transform(df_latribune, Occurences = as.numeric(Occurences))
df_lesechos$Occurences <- as.numeric(df_lesechos$Occurences)
df_latribune$Occurences <- as.numeric(df_latribune$Occurences)
# On convertit la colonne date au format Date
df_lesechos <- transform(df_lesechos, Date = as.Date(Date))
df_latribune <- transform(df_latribune, Date = as.Date(Date))
# On élimine les valeurs datant d'avant 2018
df_lesechos <- df_lesechos[!(df_lesechos$Date < "2018-01-01"),]
df_latribune <- df_latribune[!(df_latribune$Date < "2018-01-01"),]
# On groupe par mois et on fait la somme des occurences
df_lesechos <- df_lesechos %>% group_by(Date) %>% summarise(Occurences = sum(Occurences)) %>% arrange(desc(Date))
df_latribune <- df_latribune %>% group_by(Date) %>% summarise(Occurences = sum(Occurences)) %>% arrange(desc(Date))
# Calcul de la variance pour chaque journal
echos_var <- var(df_lesechos$Occurences)
tribune_var <- var(df_latribune$Occurences)
# Divisions des occurences par l'écart type, ce qui nous donne un écart type unitaire
df_lesechos$Occurences <- (df_lesechos$Occurences) / sqrt(echos_var)
df_latribune$Occurences <- (df_latribune$Occurences) / sqrt(tribune_var)
# La normalisation de chaque série mensuelles des différents journaux nous permets de les combiner
# Création du dataframe qui va faire la somme des deux dataframes
df_france <- bind_rows(df_lesechos,df_latribune)
df_france <- df_france %>% group_by(Date) %>% summarise(Occurences = sum(Occurences)) %>% arrange(desc(Date))
# On divise par 2 la série obtenue car on a 2 sources d'informations
df_france$Occurences <- df_france$Occurences / 2
# Calcul de la moyenne de la série
moyenne <- mean(df_france$Occurences)
# On ramène la série à une moyenne de 100 afin d'obtenir l'indicateur EPU de chaque mois
df_france$Occurences <- df_france$Occurences*(100/moyenne)
# Visualisation de la série à l'aide ggplot2
p <- df_france %>%
ggplot(aes(x=Date, y=Occurences, text = paste0("Date : ", format(Date, "%Y-%m"), "\n",
"EPU : ", round(Occurences)))) +
geom_area(fill="#5685D7", alpha=0.5, group=1) +
geom_line(color="#FF0000", size=0.2, group=1) +
ggtitle("FR Indice EPU") +
ylab("EPU") +
xlab("Années-Mois") +
geom_point(size=0.5) +
scale_x_date(breaks = df_france$Date, labels = date_format("%Y-%m")) +
theme(axis.text.x = element_text(angle = 90),
plot.title = element_text(size=14, face="italic", family="Avenir Next"),
axis.title.x = element_text(family="Avenir Next"),
axis.title.y = element_text(family="Avenir Next"))
p <- ggplotly(p, tooltip = "text")
p
}
The object p is the graph.
i really don't know how to proceed in order to display the graph. When I run the app, only the sidebar is display, and there is no graph. I have any errors in the consol...
If someone can help me, it would be really great.
Thanks a lot !!!
Reading in the csv files on the server side might help. Try this
server <- function(input, output) {
# On importe les données du csv dans les dataframes
df_lesechosi <- read.csv(file = "data/df_lesechos.csv", sep=",")
df_latribunei <- read.csv(file = "data/df_latribune.csv", sep=",")
mygraph <- reactive({
graph_epu(df_lesechos=df_lesechosi, df_latribune=df_latribunei)
})
output$p <- renderPlotly({
mygraph()
})
}
### Your function should be as follows, and no need to read in csv files within this function
graph_epu <- function(df_lesechos="", df_latribune=""){...

Syntax Error in R when adding a loop to read multiple pdf pages

Can anyone help me to find where is my mistake in this piece of code?
This is what I am getting: "Error: unexpected '}' in " }""
If I try to run only the chunk under the loop everything is fine but I need this to be process in 50 pages and then merged.
library(pdftools)
library(tidyr)
library(sqldf)
sDirectorio = "/Users/muribe/Desktop"
archivoPDF = pdftools::pdf_text(file.path(sDirectorio,"centros_votacion.pdf"))
n = 0
WHILE (n <8) {
n=n+1
vLineas = unlist( strsplit(archivoPDF[n], split = c("\r\n")) )
dfDefinicionTabla = data.frame( Orden = 1:9,
Campo = c("Codigo","Provincia","Canton","Distrito","JRVInicial","JRVFinal","TotalJRV","TipoCentro","CentroVotacion"),
Inicio = c(1,8,21,36,75,86,97,104,127),
Fin = c(7,20,35,74,85,96,103,126,180),
Tipo = c("numeric","character","character","character","numeric","numeric","numeric","character","character"),
stringsAsFactors = FALSE)
dfTabla = data.frame()
for (linea in 7:length(vLineas)) {
dfFila = data.frame(n, stringsAsFactors = FALSE)
for (campo in 1:nrow(dfDefinicionTabla)){
# campo = 2
# Extrae cada uno de los campos de la linea
dfFila =cbind(dfFila, trimws(substr( vLineas[linea],
dfDefinicionTabla[campo,]$Inicio,
dfDefinicionTabla[campo,]$Fin),
which = "both")
)
}
colnames(dfFila)[2:(nrow(dfDefinicionTabla)+1)] = dfDefinicionTabla$Campo
dfTabla <- rbind(dfTabla, dfFila)
}
}
print(dfTabla)``
Thanks, I changed it to a 'repeat' loop and fixed other syntax errors Now is working.
{library(pdftools)
library(tidyr)
library(sqldf)
sDirectorio = "~/Desktop"
archivoPDF = pdftools::pdf_text(file.path(sDirectorio,"centros_votacion.pdf"))
n = 1
repeat {
if (n <50) {
vLineas = unlist( strsplit(archivoPDF[n], split = c("\r\n")) )
dfDefinicionTabla = data.frame( Orden = 1:9,
Campo = c("Codigo","Provincia","Canton","Distrito","JRVInicial","JRVFinal","TotalJRV","TipoCentro","CentroVotacion"),
Inicio = c(1,8,21,36,75,86,97,104,127),
Fin = c(7,20,35,74,85,96,103,126,180),
Tipo = c("numeric","character","character","character","numeric","numeric","numeric","character","character"),
stringsAsFactors = FALSE)
dfTabla = data.frame()
for (linea in 7:length(vLineas)) {
dfFila = data.frame(n, stringsAsFactors = FALSE)
for (campo in 1:nrow(dfDefinicionTabla)){
# campo = 2
# Extrae cada uno de los campos de la linea
dfFila =cbind(dfFila, trimws(substr( vLineas[linea],
dfDefinicionTabla[campo,]$Inicio,
dfDefinicionTabla[campo,]$Fin),
which = "both")
)
}
colnames(dfFila)[2:(nrow(dfDefinicionTabla)+1)] = dfDefinicionTabla$Campo
dfTabla = rbind(dfTabla, dfFila)
assign(paste0("df",n),dfTabla)
}
n = n + 1 } else break }
df <- rbind(df1,df2,df3,df4,df5,df6,df7,df8,df9,df10,df11,df12,df13,df14,df15
,df16,df17,df18,df19,df20,df21,df22,df23,df24,df25,df26,df27
,df28,df29,df30,df31,df32,df33,df34,df35,df36,df37,df38,df39,df40,
df41,df42,df43,df44,df45,df46,df47,df48,df49)
head(df)}

How can i use the same code for different variables in a dataset in R?

I am working in a email classification supervised model, the emails are classified in 20 different groups, I have finished the model for the first group (G1) (a very large code) and I would like to know if there's some function which can repeat the code but with the others groups as variable, because changing G1 for (G2...G20)manually would be so tedious.
I have not idea how I could do it.
####G1####
datos_pivot1= cast(datosArg[,c('Descripción', 'Subcategoría_Servicio', 'value')], Descripción ~ Subcategoría_Servicio, mean)
datos_pivot1=datos
datos_G1=datos[datos$G1>0 & is.na(datos$G1)==F ,c('Descripción','G1')]
wordcloud(datos_G1$Descripción, max.words = 200, min.freq = 200, random.order = F, colors = brewer.pal(name = "Dark2", n = 8))
length(datos_G1$Descripción)
# casos FALSE
datos_G1_no=datos[is.na(datos$G1)==T ,c('Descripción','G1')]
# numero de casos sin O2C
length(datos_G1_no$Descripción)
#para balancear la cantidad de True vs False, se selecciona una muestra del mismo número de casos True
datos_G1_no_sample =datos_G1_no[sample(1:length(datos_G1_no$Descripción),size=length(datos_G1$Descripción)),]
datos_G1_no_sample$G1=0
cor1=head(datos_G1_no_sample)
#unir casos TRUE Y casos FALSE
datos_G1 = rbind(datos_G1, datos_G1_no_sample)
table(datos_G1$G1)
#corpus
base_corpus_G1 <- Corpus(VectorSource(datos_G1$Descripción))
#Matriz de términos
base_tdm_G1 <- TermDocumentMatrix(base_corpus_G1)
#Eliminar términos dispersos
base_tdm_G1 <- removeSparseTerms(base_tdm_G1, sparse = .95)
#Matriz por filas=Analisis, columnas = palabras
base_mat_G1 <- t(as.matrix(base_tdm_G1))
base_mat_G1= cbind(base_mat_G1, data.frame(G1=c(rep(1,table(datos_G1$G1)[2]),rep(0,table(datos_G1$G1)[2]))))
head(base_mat_G1)
##ENTRENAMIENTO Y PRUEBA
## 75% Train.
smp_size <- floor(0.75 * nrow(datos_G1))
#definimos una semilla para que cuando volvamos a ejecutar obtengamos la misma muestra
set.seed(456)
train_ind <- sample(seq_len(nrow(base_mat_G1)), size = smp_size)
#Filtro de Entrenamiento 75% (basado en el sample de la linea anterior)
train_G1 <- base_mat_G1[train_ind, ]
#Filtro de prueba 75% (los otros) (el menos indica las contratias)
test_G1 <- base_mat_G1[-train_ind, ]
table(train_G1$G1)
##MODELOS O2C
##Arbol de decision
library(rpart)
tree_G1 <- rpart(G1 ~ ., data = train_G1)
#Predict
pred.tree_G1 <- predict(tree_G1, newdata = test_G1)
pred.tree_G1=(as.data.frame(pred.tree_G1))
names(pred.tree_G1)=c('prob')
pred.tree_G1$G1.pred=0
pred.tree_G1$G1.pred[pred.tree_G1$prob>0.51] = 1
table(test_G1$G1, pred.tree_G1$G1.pred)
rpart.plot(tree_G1)
#Curva ROC
roc.curve(test_G1$G1, pred.tree_G1$G1.pred, curve=TRUE)
##GLM
#train
glm_G1 <- glm(G1 ~ ., family=binomial(logit), data=train_G1)
#predict
pred.glm_G1 = test_G1[,c('G1','G1')]
pred.glm_G1 = cbind(pred.glm_G1, data.frame(predict(glm_G1, newdata=test_G1,type='response')))
pred.glm_G1$G1 = NULL
pred.glm_G1$G1.1 = NULL
names(pred.glm_G1)=c('prob')
pred.glm_G1$G1.pred=0
pred.glm_G1$G1.pred[pred.glm_G1$prob>0.51] = 1
table(test_G1$G1, pred.glm_G1$G1.pred)
#Curva ROC:
roc.curve(test_G1$G1, pred.glm_G1$G1.pred, curve=TRUE)
##KKNN
kknn_G1 <- kknn(G1 ~ ., train_G1, test_G1, distance = 1, k=350, kernel = "optimal")
pred.kknn_G1 = test_G1[,c('G1','G1')]
pred.kknn_G1$prob<-kknn_G1$fitted.values
pred.kknn_G1$G1 = NULL
pred.kknn_G1$G1.1 = NULL
pred.kknn_G1$G1.pred=0
pred.kknn_G1$G1.pred[pred.kknn_G1$prob>0.51] = 1
table(test_G1$G1, pred.kknn_G1$G1.pred)
#Curva ROC
roc.curve(test_G1$G1, pred.kknn_G1$G1.pred, curve=TRUE)
head(pred.kknn_G1)
If you are using R studio, just press command + F or press the Find/Replace button in your R console you can easily replace all 'G1' with 'G2'.

Update the axis labels with rChart

I just discovered rChart and googleVis and i want to thank developers for their job.
My problem is simple, i want to add a variable label to my axis for nplot?
I also want to know if it's possible to add variable like sizevar and colorvar of gvisBubbleChart to nplot ?
Thank you.
library(rCharts)
VehiculeFunction <- function(data, gamme, absciss, ordinate){
# Aim: Permet de visualiser les données sous la forme de nuages de points
# croisant GMF*Coût, Ratio K * Ratio Coût ou bien GMF*Ratio en
# choisissant la gamme qu'on désire
# Input: data.frame avec notamment GAMME, PROJET, PERIMETRE, NITG, GMF.24,
# Cout.24 et libele
# Output: Graphique avec le croisement choisi ainsi que le libellé étiquetté
# sur le point qu'on voudra identifier
if(absciss == "GMF.24"){
my.data <- data[data$RANG_NITG_PROJET_K %in% c(1, 2, 3),]
} else if(absciss == "Ratio.K") {
my.data <- data[data$RANG_NITG_PROJET_C %in% c(1, 2, 3),]
}
my.data2 <- my.data[my.data$GAMME == gamme,]
X <- my.data2[[absciss]]
Y <- my.data2[[ordinate]]
SIZEVAR <- my.data2$Ratio.K
df <- data.frame(X,Y,SIZEVAR)
plot <- nPlot(x = "X", y = "Y", size = "SIZEVAR", data = df, type = "scatterChart")
plot$xAxis(axisLabel = 'X')
plot
}
VehiculeFunction(data.vehicule, gamme = "M1", "GMF.24", "Cout.24")
Usage:
gvisBubbleChart(data, idvar = "", xvar = "", yvar = "",
colorvar = "", sizevar = "",
options = list(), chartid)
E.G.
## Set color and size
bubble2 <- gvisBubbleChart(Fruits, idvar="Fruit", xvar="Sales", yvar="Expenses",
colorvar="Location", sizevar="Profit",
options=list(hAxis='{minValue:75, maxValue:125}'))
SEE http://www.inside-r.org/packages/cran/googleVis/docs/gvisBubbleChart

rChart in a function

I need help about this function, i try it with plot (R) and googleVis, it works perfectly; now i want to use rChart but when i launch the functions it returned me a simple grid with no points on it.
VehiculeFunction <- function(data, gamme, absciss, ordinate){
# Aim: Permet de visualiserles données sous la forme de nuages de points
# croisant GMF*Coût, Ratio K * Ratio Coût ou bien GMF*Ratio en
# choisissant la gamme qu'on désire
# Input: data.frame avec notamment GAMME, PROJET, PERIMETRE, NITG, GMF.24,
# Cout.24 et libele
# Output: Graphique avec le croisement choisi ainsi que le libellé étiquetté
# sur le point qu'on voudra identifier
if(absciss == "GMF.24"){
my.data <- data[data$RANG_NITG_PROJET_K %in% c(1, 2, 3),]
} else if(absciss == "Ratio.K") {
my.data <- data[data$RANG_NITG_PROJET_C %in% c(1, 2, 3),]
}
my.data2 <- my.data[my.data$GAMME == gamme,]
X <- my.data2[[absciss]]
Y <- my.data2[[ordinate]]
plot <- nPlot(Y ~ X, data = data, type = 'scatterChart')
plot
}
VehiculeFunction(data.vehicule2, gamme = "M1", "GMF.24", "Cout.24")
data.vehicule2 is a dataframe.
Thank you.
EDIT:
In the dataframe, i have 18 variables and 36 000. This code works perfectly, but my problem is the plot appearance.
library(rCharts)
VehiculeFunction <- function(data, gamme, absciss, ordinate){
# Aim: Permet de visualiser les données sous la forme de nuages de points
# croisant GMF*Coût, Ratio K * Ratio Coût ou bien GMF*Ratio en
# choisissant la gamme qu'on désire
# Input: data.frame avec notamment GAMME, PROJET, PERIMETRE, NITG, GMF.24,
# Cout.24 et libele
# Output: Graphique avec le croisement choisi ainsi que le libellé étiquetté
# sur le point qu'on voudra identifier
if(absciss == "GMF.24"){
my.data <- data[data$RANG_NITG_PROJET_K %in% c(1, 2, 3),]
} else if(absciss == "Ratio.K") {
my.data <- data[data$RANG_NITG_PROJET_C %in% c(1, 2, 3),]
}
my.data2 <- my.data[my.data$GAMME == gamme,]
X <- my.data2[[absciss]]
Y <- my.data2[[ordinate]]
SIZEVAR <- my.data2$Ratio.K
df <- data.frame(X,Y,SIZEVAR)
plot <- nPlot(x = "X", y = "Y", size = "SIZEVAR", data = df, type = "scatterChart")
plot
}
VehiculeFunction(data.vehicule, gamme = "M1", "GMF.24", "Cout.24")
Could i add a size variable because on the plot nothing appears, how could i have my variable "GMF.24" and "Cout.24" on the axis ? and the last question, is it possible to add label when i place the mouse above a point ?
Thank you in advance.

Resources