Skip to content

Instantly share code, notes, and snippets.

@sergiolucero
Created August 1, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiolucero/b9d4b181e43aefaa3f0fd030d48f6f00 to your computer and use it in GitHub Desktop.
Save sergiolucero/b9d4b181e43aefaa3f0fd030d48f6f00 to your computer and use it in GitHub Desktop.
shiny excel gg(repel)plotter
library(shiny)
library(openxlsx)
library(lattice)
library(readxl)
library(ggplot2)
library(ggrepel)
setwd('C:/Users/Sergio/Sandbox/shiny_servers/excel_plotter')
#############################################################
read_excel_allsheets <- function(filename) {
sheets <- readxl::excel_sheets(filename)
x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
names(x) <- sheets
x
}
##################################################################
runApp(
list(
ui = fluidPage(
titlePanel("AZERTA Plotter 1.0"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose input file',
accept = c(".xlsx")),
tags$hr(),
downloadButton('down',"download plot")
),
mainPanel(
tableOutput('contents'),
plotOutput('plot'))
)
),
server = function(input, output){
df <- reactive({
inFile <- input$file1
req(inFile) # require that inFile is available (is not NULL)
# (a user has uploaded data)
})
output$contents <- renderTable({
df()
})
plotInput <- reactive({
df <- df()
# xyplot(df[,2]~df[,1], df ,xlim=c(0,10),ylim=c(0,100),type = "b")
fullname <- paste(getwd(), df$name, sep='/')
print('LOADING:')
print(fullname)
xldata <- read_excel_allsheets(fullname)
### esto debiera venir en la primera hoja de un Excel:
print(xldata$config)
cfg <- xldata$config
titlename = cfg[cfg$variable=='Titulo 1',]
print('TITLE:');print(titlename$valor)
MAX_ITER = 200
legdata = xldata$datos_legislativos
p <- ggplot(legdata, aes(Urgencia, Criticidad,label=Tema,colour=Favorabilidad, shape=Impacto),
width = 24, height = 12, dpi = 120)
p <- p + theme_light() + geom_point(size=10) +
scale_x_continuous(breaks = seq(0, 10, 1)) +
scale_y_continuous(breaks = seq(0, 10, 1)) +
geom_hline(yintercept=5, colour='red', size=2) +
geom_vline(xintercept=5, colour='red', size=2) +
xlim(c(0,10)) + ylim(c(0,10))
p <- p + labs(title = titlename) +
theme(plot.title = element_text(hjust = 0.5)) +
geom_text_repel(aes(Urgencia, Criticidad), size=5, max.iter=MAX_ITER, force=30)
# ggsave('legislativo.png', width = 24, height = 12, dpi = 120)
p
})
output$plot <- renderPlot({
plotInput()
})
output$down <- downloadHandler(
filename = function(){paste("plot",".pdf",sep=".") },
content = function(file) {
pdf(file)
#xyplot(df[,2]~df[,1],df(),xlim=c(0,10),ylim=c(0,100),type = "b")
# you have to print the plot so that you can open pdf file
print(plotInput())
dev.off()
}
)
}
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment