Skip to content

Instantly share code, notes, and snippets.

@micstr
Created July 25, 2017 09:35
Show Gist options
  • Save micstr/1b00531035fed7d5455655d57db48dcf to your computer and use it in GitHub Desktop.
Save micstr/1b00531035fed7d5455655d57db48dcf to your computer and use it in GitHub Desktop.
missing chart labels MCVE example
# Test app chartlabels missing fixes
# (mcve Smaller subset version with no source)
suppressPackageStartupMessages(library(data.table))
suppressPackageStartupMessages(library(googleVis)) # else get startup msg
library(shiny)
# FUNCTIONS
# Simplifying output
drawGraphAndTable <- function(title, name)
{
return (wellPanel(
titlePanel(title),
tabsetPanel(
# googleVis needs htmlOutput not usual PlotOutput
tabPanel("Plot", htmlOutput(paste0(name,"plot"))),
tabPanel("Data", htmlOutput(paste0(name,"data")))
# tabPanel("Plot", plotOutput(paste0(name,"Plot"))),
# tabPanel("Data", plotOutput(paste0(name,"Data")))
)
))
}
PlotFVStandardChart <- function(plotdt, px, py,
chart.title = "Chart at Report Date",
ylabel = "",
xlabel = "Score Percentages",
chart.type = "bar",
op.width = "automatic",
op.height = "automatic",
op.legend = "none",
op.showeditor = NULL) {
require(googleVis)
require(data.table)
# Error handling
if (!is.data.table(plotdt) || !is.data.frame(plotdt)) {
stop("Argument must be data table/frame.")
}
# check fields exist in plotdt
if (class(px) != "character" || class(py) != "character") {
stop("Argument fieldnames must be strings.")
}
if (!px %in% names(plotdt) || !py %in% names(plotdt)) {
stop("Argument fieldnames not found in data table.")
}
# set options
# examples
# http://cran.r-project.org/web/packages/googleVis/vignettes/googleVis_examples.html
# https://developers.google.com/chart/interactive/docs/gallery/columnchart#Configuration_Options
op <- list(title = chart.title,
legend = op.legend,
gvis.editor = op.showeditor,
hAxis = paste0("{title:'", xlabel, "'}"),
vAxis = paste0("{title:'", ylabel, "', ",
"showTextEvery:1}"),
#vAxis = paste0("{title:'", ylabel, "', ",
# "showTextEvery:1, viewWindow:{max:35}}"),
width = op.width,
height = op.height
)
# Create chart specified by chart.type
# (choose lower case three characters to specify)
tmp.chart.type <- tolower(substr(chart.type, 1, 3))
if (tmp.chart.type == "col") {
plotfv <- gvisColumnChart(plotdt,
xvar = px,
yvar = c(py),
options = op)
} else if (tmp.chart.type == "bar") {
plotfv <- gvisBarChart(plotdt,
xvar = px,
yvar = py,
options = op)
} else if (tmp.chart.type == "lin") {
plotfv <- gvisLineChart(plotdt,
xvar = px,
yvar = c(py),
options = op)
# !!! no colouring for now yvar = c(py, paste0(py, ".style")),
} else {
stop("Unknown chart type")
}
return(plotfv)
}
# TEST DATA
test.n <- 15
k.chart.default.limit <- 15
plotdata <- data.table("ticker" = LETTERS[1:test.n],
"score" = c(test.n:1) / 100,
"other" = c(1:test.n))
# TEST APP
server <- function(input, output) {
output$dashboard <- renderUI({
tabPanel("Dashboard",
fluidRow(
column(12, drawGraphAndTable("Missing labels demo", "mychart"))
)
)
})
output$mychartdata <- renderGvis(gvisTable(plotdata))
output$mychartplot <- renderGvis({
#case 1
#k.op.width <- "automatic" # fixed 900 does not scale though
#k.op.height <- "automatic"
# case 2 works but does not scale with page
#k.op.width <- "400" # fixed 900 does not scale though
#k.op.height <- "400"
# case 3 %?
k.op.width <- "100%"
k.op.height <- "100%"
PlotFVStandardChart(plotdata, "ticker", "score",
chart.title = "Scores at Report Date",
ylabel = "",
xlabel = "Score Percentages",
chart.type = "bar",
op.width = k.op.width,
op.height = k.op.height,
op.legend = "none",
op.showeditor = NULL)
})
}
ui <- fluidPage(
tabPanel("Test Dash", uiOutput("dashboard"))
)
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment