############################################
# Ciclo de charlas - Los vieRnes de maRzo  #
# De Excel a R: Un viaje de ida y vuelta   #
# Ángela Villaverde Ramiro		   #
############################################ 

## Introduce tu directorio de trabajo en la función setwd()
setwd()

library(openxlsx)

## De Excel a R
df <- read.xlsx (xlsxFile = "./datos.xlsx", sheet = "Curso", startRow = 2, colNames = TRUE, rowNames =FALSE,
skipEmptyRows =TRUE, skipEmptyCols =TRUE, cols = c(1:3,5:10), rows = 1:34, detectDates = TRUE)

## Pasos en R
df$Asistencia <- as.logical(df$Asistencia)
df$Grupo <- as.factor(df$Grupo)

## De R a Excel
wb <- createWorkbook()
addWorksheet(wb, "CursoR")

# Introducir datos
writeData(wb, "CursoR", x = df, startCol = 1, startRow = 3 )

# Introducir fórmulas
Total_parcial <- sprintf("D%s*0.6 + E%s*0.4",4:33,4:33)
writeData(wb, "CursoR", x = "Total_parcial", startCol = 10, startRow = 3 )
writeFormula(wb, "CursoR", x = Total_parcial, startCol = 10, startRow = 4)

# Combinar celdas
mergeCells(wb, "CursoR", cols = 1:10, rows = 1)
writeData(wb, "CursoR", x = " Curso de análisis de datos con R", startCol = 1, startRow = 1)
mergeCells(wb, "CursoR", cols = 1:3, rows = 2)
mergeCells(wb, "CursoR", cols = 4:7, rows = 2)
mergeCells(wb, "CursoR", cols = 9:10, rows = 2)
writeData(wb, "CursoR", x ="Datos_personales", startCol = 1, startRow = 2)
writeData(wb, "CursoR", x ="Desarrollo_curso", startCol = 4, startRow = 2)
writeData(wb, "CursoR", x ="Grupo", startCol = 8, startRow = 2) 
writeData(wb, "CursoR", x ="Final_curso", startCol = 9, startRow = 2)

# Estilo del encabezado 
headerStyle <- createStyle(
      		fontSize = 11, fontColour = "skyblue4", textDecoration = "bold",
		halign = "center", fgFill = "cadetblue3", 
		border = "TopBottom", borderColour = "black")

addStyle(wb, sheet = "CursoR", headerStyle, rows = 1, cols = 1:10, gridExpand = TRUE)

# Ancho de las columnas
setColWidths(wb, sheet = "CursoR", cols = 1:10, widths = 16)

# Mas estilos de encabezados
titleGroupStyle <- createStyle(fontSize = 10, fontColour = "brown", textDecoration = "italic",
		halign = "center", fgFill = "sandybrown", borderStyle = "double", 
		border = "TopBottomLeftRight", borderColour = "black")

addStyle(wb, sheet = "CursoR", style = titleGroupStyle, rows = 2, cols = 1:10, gridExpand = TRUE)

titleStyle <- createStyle(fontSize = 10, fontColour = "darkseagreen4",  
		halign = "center", fgFill = "darkseagreen2", 
		border = "TopBottomLeftRight", borderColour = "black")

addStyle(wb, sheet = "CursoR", style = titleStyle, rows = 3, cols = 1:10, gridExpand = TRUE)

# Estilo datos con formato
fill <- gray.colors(n=4,start = 1, end = 0)[1:2]
format <- c("text","text","text","number","number","text","number","text","date","number")

for(i in 1:length(levels(df$Grupo))){
	mergeCells(wb, "CursoR", cols = 8, rows = (which(df$Grupo == levels(df$Grupo)[i])+3))
	for(j in 1:10){
		dataStyle <- createStyle(fontSize = 10, halign = "center", valign = "center",
		fgFill = fill[i%%2 + 1],border = "TopBottomLeftRight", borderColour = "black",
		numFmt = format[j])

		addStyle(wb, sheet = "CursoR", style = dataStyle, rows = (which(df$Grupo == levels(df$Grupo)[i])+3), 
		cols = j, gridExpand = TRUE)
	}
}

saveWorkbook(wb, "datos2.xlsx", overwrite = T)

