Last active
August 29, 2015 14:12
-
-
Save srvanderplas/4b2eb3ae062ab2ae96cd to your computer and use it in GitHub Desktop.
Unicode in ggplot2/shiny/markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(ggplot2) | |
library(Unicode) | |
library(Cairo) | |
TestUnicode <- function(start="25a0", end="25ff", ...) | |
{ | |
nstart <- as.hexmode(start) | |
nend <- as.hexmode(end) | |
r <- nstart:nend | |
s <- ceiling(sqrt(length(r))) | |
i <- 1:length(r) | |
data.frame(x=i%%s, y=i%/%s, shape=factor(i), shapechars=-r) | |
} | |
shinyServer(function(input, output, clientData){ | |
output$plot <- renderPlot({ | |
end <- as.hexmode(input$start)+input$n | |
data=TestUnicode(input$start, end) | |
shapes <- list(1:length(data$shapechars), data$shapechars) | |
qplot(data=data, x=x, y=y, shape=shape, size=I(5)) + | |
scale_shape_manual(values=shapes[[(input$unicode=="unicode")+1]], guide="none") | |
}) | |
# A temp file to save the output. | |
outfile <- tempfile(fileext='.png') | |
output$pictureversion <- renderImage({ | |
width <- 600#clientData$output_pictureversion_width | |
height <- 400#clientData$output_pictureversion_height | |
end <- as.hexmode(input$start)+input$n | |
data <- TestUnicode(input$start, end) | |
shapes <- list(1:length(data$shapechars), data$shapechars) | |
png(outfile, width=width, height=height) | |
qplot(data=data, x=x, y=y, shape=shape, size=I(5)) + | |
scale_shape_manual(values=shapes[[(input$unicode=="unicode")+1]], guide="none") | |
dev.off() | |
# Return a list containing the filename | |
list(src = outfile, | |
width = width, | |
height = height, | |
alt = "This is alternate text") | |
}, deleteFile=FALSE) | |
output$sessioninfo <- renderPrint(sessionInfo()) | |
output$sysinfo <- renderPrint(Sys.info()) | |
output$unicodechars <- renderPrint({ | |
if(input$unicode=="unicode"){ | |
as.u_char(as.hexmode(input$start):((as.hexmode(input$start)+input$n))) | |
} else { | |
1:input$n | |
} | |
}) | |
# output$filename <- renderText(outfile) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(Cairo) | |
seed <- runif(1, 1000, 1000000) | |
shinyUI(fluidPage( | |
titlePanel("Demonstration - Unicode Characters"), | |
fluidRow( | |
column( | |
2, | |
wellPanel( | |
radioButtons("unicode", "CharacterType", choices=c("Unicode"="unicode", "R Default"="normal")), | |
numericInput("n", "Number of points", value=64, min=1, max=225), | |
conditionalPanel(condition="input.unicode=='unicode'", | |
textInput("start", "Start value (positive)", value="25a0") | |
) | |
) | |
), | |
column(10, | |
fluidRow( | |
column(6, | |
h3("Using Graphics Device"), | |
div(align="center", plotOutput("plot", width=600, height=400)) | |
), | |
column(6, | |
h3("Using Cairo PNG"), | |
imageOutput("pictureversion")) | |
), | |
br(), | |
h3("Session Info"), | |
verbatimTextOutput("sessioninfo"), | |
br(), | |
h3("System Info"), | |
verbatimTextOutput("sysinfo"), | |
br(), | |
h3("Unicode Character Codes"), | |
verbatimTextOutput("unicodechars")#, | |
# br(), | |
# verbatimTextOutput("filename") | |
) | |
) | |
)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "UnicodeTest" | |
author: "Susan VanderPlas" | |
date: "01/06/2015" | |
output: html_document | |
--- | |
Testing whether it's a browser/rendering issue, which makes no sense to me, but it wouldn't be the strangest error I've come across: | |
```{r} | |
library(ggplot2) | |
library(Unicode) | |
library(Cairo) | |
TestUnicode <- function(start="25a0", end="25ff", ...) | |
{ | |
nstart <- as.hexmode(start) | |
nend <- as.hexmode(end) | |
r <- nstart:nend | |
s <- ceiling(sqrt(length(r))) | |
i <- 1:length(r) | |
data.frame(x=i%%s, y=i%/%s, shape=factor(i), shapechars=-r) | |
} | |
input <- list(unicode="unicode", start="25a0", n=256) | |
end <- as.hexmode(input$start)+input$n | |
data <- TestUnicode(input$start, end) | |
qplot(data=data, x=x, y=y, shape=shape, size=I(5)) + | |
scale_shape_manual(values=data$shapechars, guide="none") | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment