Creating wordcloud with greek symbols - r

Is it possible to create a wordcloud with greek symbols? Since expression is not working within a data frame, is there a workaround?
Here is a small example what I'm trying to do, but haven't figured out how the cloud could show the symbols instead.
library(highcharter)
hchart(data.frame(greek_symbols = c("beta","mu","gamma")), "wordcloud", hcaes(name = greek_symbols, weight = rep(1,1,1)))

If youir input is given by the written names of the symbols you could make a translation vector as in your cloud you want to provide the unicode value of the symbol. symbols_conversion should include all (I just added yours plus one). So symbols_to_show contains the names you want to display. Subset like symbols_conversion[symbols_to_show] to get the symbols shown.
library(highcharter)
symbols_conversion <- c("beta" = "\u03b2", "gamma" = "\u03b3", "delta" = "\u03b4", "mu" = "\u03bc")
symbols_to_show <- c("beta","mu","gamma")
hchart(data.frame(greek_symbols = symbols_conversion[symbols_to_show]), "wordcloud", hcaes(name = greek_symbols, weight = rep(1,1,1)))

Related

How to set-up R crosstalk filter_select "group" argument to match unlisted vector of individual character strings from shared dataframe

I am new to using the crosstalk package and I already have a unique case for which I would like to use it. I want to filter point coordinates based on any matching keyword contained in a list of keywords belonging to an individual point. I have generalized my problem to a minimal reproducible example described below.
I have a dataframe where each row is an item with one xy coordinate. Each point has a unique id. Each point also has a column called "tag" that is populated with a list of strings representing keywords associated with that point I would like to be able to search and filter on. Some points may have only one keyword, some may have multiple, and some may share keywords. I want to use the crosstalk::filter_select() function to ultimately be able to search for one keyword and see which points contain that keyword in the rendered html. For example, a search for the keyword "keyword2" should filter points that contain these example lists in the "tag" column:
filtered point 1: "keyword3" "keyword8" "keyword5" "keyword2"
filtered point 2: "keyword2"
filtered point 3: "keyword7" "keyword2"
The search should hide points that contain these example lists in the "tag" column:
hidden point 1: "keyword3" "keyword8" "keyword5" "keyword10"
hidden point 2: "keyword5"
hidden point 3: "keyword7" "keyword4"
I would like to be able to also supply "TRUE" the "multiple" argument to be able to enter more than one keyword and see what points have these in the list in the tag column of my dataframe. My example code renders but you can test this out by entering one of the keywords in the "Tag" search box. You will see the way I have the filter_select() set-up is not not appropriately filtering the points the way I am intending. Entering one keyword in the search box is filtering out points that do indeed have that keyword in its list. I believe the problem is in the "group" argument where I am supplying the unlisted "tag" column of keywords. I think I need some sort of function but I have not been able to figure out how to format it. Any help with this would be greatly appreciated. Thanks.
The code I provided below will produce this html:
This is the rendered html with no filtering. All dummy points are displayed:
This is the (undesired) result of entering a search term, one of the many keywords that may appear in the tag column of my dataframe. Only one point is displayed but there are several other points that contain that keyword in the list of keywords within the tag column.
This is the desired result. When I enter a keyword in the search box, I get all points that have that keyword in the list of associated keywords for that point. I had to manually select these rows in the table to achieve this result but I want the select_filter() function to be able to do this automatically for me.
title: "Min reproducible Ex"
author: "Me"
output:
flexdashboard::flex_dashboard:
theme: paper
#Import libraries
library(dplyr)
library(leaflet)
library(DT)
library(crosstalk)
#Create some dummy data for demonstration
keytags <- 1:10
df <- data.frame(
"id"=1:10,
"x"=seq(-90, -85.5, by=.5),
"y"= seq(30, 34.5, by=.5))
for(i in 1:nrow(df)){
tags <- as.vector(paste0("keyword", sample(keytags,i)))
df[i, "tag"][[1]] <- list(tags)
}
#Create shared df
sdf <- crosstalk::SharedData$new(df,key =~id, group="shareddata")
Interactives {data-icon="ion-stats-bars"}
Column {data-width=400}
Filters
filter_select(
id="tag",
label="Tag",
sharedData= sdf,
allLevels = TRUE,
group= ~unlist(tag)
)
Datatable
sdf %>%
DT::datatable(
filter = "top", # allows filtering on each column
extensions = c(
"Buttons", # add download buttons, etc
"Scroller" # for scrolling down the rows rather than pagination
),
rownames = FALSE, # remove rownames
style = "bootstrap",
class = "compact",
width = "100%",
options = list(
dom = "Blrtip", # specify content (search box, etc)
deferRender = TRUE,
scrollY = 300,
scroller = TRUE,
buttons = list(
I("colvis"), # turn columns on and off
"csv", # download as .csv
"excel" # download as .xlsx
)
)
)
Column
Interactive map
#Add map
#Make basemap
map <- sdf %>%
leaflet() %>%
#Add base layers
addTiles() %>%
#Add Markers
addMarkers(~ x, ~ y, popup = ~as.character(id))
map

How to use R character vector element as string and variable inside function?

I am trying to apply SPSS style category labels to my dataset in R. I think my question arises as I do not know how to parse variables correctly, so is not necessarily related to just these types of data.
To begin with, doing this manually as per the expss library documentation works fine:
library(expss)
#Load in the data
data(mtcars)
#Apply Variable Labels and Value Labels (and Numeric Coding) to each Variable.
mtcars = apply_labels(mtcars,
vs = "Engine",
vs = c("V-engine" = 1,
"Straight engine" = 2,
"Other engine" = 3)
)
Now my problem arises if I have my "Variable Names", "Variable Labels", "Value Labels" and corresponding "Value Numeric Codes" stored in some R data type and I try to use them in the apply_labels function. For example, if I have these stored in character vectors like so:
#Load in the data
data(mtcars)
#Value Labels
value_lab<-c("V-engine","Straight engine","Other engine")
#Value's Numeric coding
value_num<-c("1","2","3")
#Variable names
var <- c("vs")
#Variable Labels
var_lab<-c("Engine")
Then my question is, how would I use my character vector elements inside the apply_labels function? e.g. how would I do something like this:
#Apply Variable Labels and Value Labels (and Numeric Coding) to each Variable.
mtcars = apply_labels(mtcars,
var[1] = var_lab[1],
var[1] = c(value_lab[1] = value_num[1],
value_lab[2] = value_num[2],
value_lab[3] = value_num[3])
)
I have tried various combinations of paste and toString without success. My next step will be to apply this to my 500,000+ rows x 20,000 columns of data with a to-be-determined number of possible Value Labels/Numeric Codings.
Obligatory: I am new to R.
Thank you.
To achieve your desired result
Make use of named lists and vectors to store your variable and value labels
Doing so you can make use of do.call to pass the variable and value labels to apply_labels
To make the example more interesting I added labels for a second variable.
library(expss)
# Variable Labels
var_labels <- list(vs = "Engine", am = "Transmission")
#Value Labels
val_labels <- list(
vs = c("V-engine" = 0, "Straight engine" = 1),
am = c("Automatic" = 0, "Manual" = 1)
)
mtcars2 <- do.call(apply_labels, c(list(data = mtcars), var_labels, val_labels))
table(mtcars2$am, mtcars2$vs)
#>
#> V-engine Straight engine
#> Automatic 12 7
#> Manual 6 7
Great, thank you! That has led me to understand named lists and build a solution with setNames.
I ended up not using expss. It appeared to work within R and labelled everything as expected, but when I exported the final dataframe from R to SPSS using haven::write_sav, the value labels were not maintained (but the variable labels were).
Instead I used the haven labelled vector class to apply the Variable and Value labels. My final solution looks like this:
#Load in the data
data(mtcars)
#Variables
var <- c("vs")
#Variable Labels
var_labels<-c("Engine")
#Value Labels (for first Variable)
value_labs<-c("V-engine","Straight engine","Other engine")
#Value's Numeric coding )
value_num<-c("1","2","3")
#Make a named list to use as the value labels
value_labels <- setNames(as.integer(value_num),value_labs)
#Apply the label with haven
mtcars[,c(var[1])]<-labelled(mtcars[, c(var[1])],
labels=value_labels,
label=var_labels[1])
#Save out in spss format
haven::write_sav(mtcars, "test.sav")
Also, I have set it up so my data comes in one grouping of values labels at a time, but your example of expanding to the second variable helped me generalise this too, so thanks again!

printable table with partial bolding/italics within a cell

I'm looking for a way to print out a table from R, but with formatting within a cell, like having some of the text within a cell be bold or italic. Normally I would first make the appropriate data.frame/tibble and then I'd format and print it using a package like huxtable or kable. Looking over documentation for huxtable or kableExtra, it seems as though both packages treat formatting as properties of cells, implying that within-cell formatting is either unsupported or must be implemented some other way.
If I was making a ggplot, I'd use expression for text formatting, e.g.
library(tidyverse)
ggplot(data=mtcars) +
ggtitle(expression(paste(bold("bold part"), " not bold part")))
I thought I could be clever by putting expressions into a data.frame, but this doesn't seem to be supported:
data.frame(var = c(expression(paste(bold("bold part"), "not bold part")),
expression(paste(bold("bold part"), "not bold part"))
))
#> Error in as.data.frame.default(x[[i]], optional = TRUE): cannot coerce class ""expression"" to a data.frame
If you want to make changes to data tables, I recommend you use the grid and gridExtra packages to construct your table and then make changes to the theme parameters.
Without any data to play with I can't see exactly what you want but here's a general idea of what you could do (see below). I've included other aesthetic parameters, for future reference.
You could then generate a pdf output to your C drive, which could then be printed.
d <- data.frame(A = c(1,2,3,4,5),
B = c(6,7,8,9,10),
C = c(11,12,13,14,15))
pdf("Test.pdf", height = 11, width = 10)
grid.table(d, rows = NULL, theme = ttheme_minimal(
core=list(fg_params=list(
hjust=0,
x=0.1,
fontface=matrix(c(1,2,3))))))
dev.off()
Re huxtable, you're correct, but you can get round it. Here's a 1 row, 1 column example, assuming you are printing to HTML:
my_hux <- huxtable("<b>Bold part</b> Not bold part")
escape_contents(my_hux)[1, 1] <- FALSE
You can include arbitrary HTML. Something similar would work for TeX, obviously with TeX formatting instead.

How to format currency values in valueBox shinydashboard?

I'm trying to write a dashboard with shinydashboard in R to display some values using renderValueBox and valueBoxOutput. These values are not hardcoded but are being scraped from another source daily.
These values are currency numbers and should be reporting like $XXX,XXX.XX but instead I see XXXXXX.XX. Is there a way, like a wrapper, to easily format those values? Otherwise I've thought of brute forcing some regex on it with gsub...but ew. Please and thanks :)
Discovered the function prettyNum(): this function is amazing for simple conversion to comma separated numerics.
> prettyNum(56789, big.mark = ",")
> 56,789
Another way is to use the {scales} package and the dollar_format() function.
This function is a labelling function factory, in the sense that it creates other functions.
I usually need to output numbers in euros, so I defined the following function:
euro_format <- scales::dollar_format(
prefix = "\u20ac", # the euro symbol
suffix = "",
big.mark = ",",
decimal.mark = ".",
accuracy = 1
)
>euro_format(20842)
[1] "€20,842"

How to set highchart global options IN R

I see a lot of examples in javascript but I cannot find an example to do it in R
Here is the api link: http://api.highcharts.com/highcharts#global
I am trying to set "timezoneOffset" and I have tried many different ways.
When I do this in R: highChart$global(timezoneOffset=-300)
I do not get any warning or error, but it's not working.
Thanks a lot for the help!
Here is a piece of code:
library(rCharts)
highChart <- Highcharts$new()
highChart$global(timezoneOffset=-300)
highChart$chart(zoomType = "xy")
highChart$exporting(enabled = T)
highChart$xAxis(type="datetime",list( title = list(text = "Time")))
highChart$yAxis(list
(
list(title = list(text = "Variance"))
))
highChart$series(data=list(c(x=1407795845000,y=1),c(x=1407795846000,y=2),c(x=1407795847000,y=3)))
highChart
As you can see, the timezoneOffset is not working when I run this piece of code and the time is still displayed in GMT.
As of version 0.5.0 of highcharter, it seems the option highcharter.options is not there any more, but there are several separate options, e.g. highcharter.lang, highcharter.global, etc. So the following approach works:
lang <- getOption("highcharter.lang")
lang$decimalPoint <- ","
lang$numericSymbols <- highcharter::JS("null") # optional: remove the SI prefixes
options(highcharter.lang = lang)
In addition to changing the decimal point, the SI prefixes ("k", "M", "G", etc.) are turned off by setting the numericSymbols to null, see Replacing/removing the metric notations like thousands "k" abbreviation.
The highcharter options can be accessed, but they are set inside the standard R options under the list element highcharter.options. They are not given directly to the highchart, and inside highchart(), there is the code line opts <- getOption("highcharter.options", list()).
I don't think there is another way than just get the options, alter whatever options you need to change and then set the options again with your additions.
The following is a simple illustration:
library(highcharter)
# normal highchart
highchart() %>%
hc_add_serie_labels_values(1:901, seq(1, 10, 0.01))
opts <- getOption("highcharter.options")
opts$lang$decimalPoint <- "."
options(highcharter.options = opts)
# now with "," instead of "." (confirm in tooltip)
highchart() %>%
hc_add_serie_labels_values(1:901, seq(1, 10, 0.01))
Of course in your case, you need to set the $global$timezoneOffset part.
First you have to switch of the useUTC flag to FALSE. Than you can set the timezoneOffset as you wish and save the options back.
global <- getOption("highcharter.global")
global$useUTC <- FALSE
global$timezoneOffset <- -300
options(highcharter.global = global)
For better understanding make sure you take a look at global:
str(global)

Resources