I tried to use flexdashboard to make interactive vizualisation of word embeddings results, but I don’t get the logic behind the use of shiny modules.
In my Rmd dashboard, after loading the needed libraries, I first load the 3 models I want to deal with:
---
title: "Embedding Explorer"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(wordVectors)
library(plotly)
library(shiny)
library(shinyjs)
```
```{r global, include=FALSE}
model_1 = wordVectors::read.vectors('./models/word_embeddings_1.bin')
model_2 = wordVectors::read.vectors('./models/word_embeddings_2.bin')
model_3 = wordVectors::read.vectors('./models/word_embeddings_3.bin')
```
Then I would like each component of my dashboard to load a specific module. In the sidebar for instance, I want to display a selectInput object to select one of the models. To do so, I have coded a R script module Models.R:
# UI function
selectModelInput <- function(id) {
ns <- NS(id)
tagList(
selectInput(ns("target_model"),label="Target model",c("gloVe","Word2Vec","Fasttext"))
)
}
# Server function
selectModel <- function(input, output,session) {
observeEvent(input$target_model,{
if (input$target_model=="gloVe"){
model = model_1
}else if (input$target_model=="Word2Vec"){
model = model_2
}else if (input$target_model=="Fasttext"){
model = model_3
}
output$model <- model #add an output element
})
}
Thus, I call this module in my Rmd file:
```{r sidepanel}
# include the module
source("./modules/Models.R")
# call the module
selectModelInput("mod")
model <- callModule(selectModel,"mod") #add model in variable
renderText("Vocabulary size:")
renderPrint(nrow(model))
renderText("Embeddings dim:")
renderPrint(ncol(model))
```
Doing so, I have an error message stating that ‘model’ object does not exist. Maybe the problem is that the module doesn’t have access to the model_x from the global part? Or do I miss something allowing to save the ‘model’ object somewhere? By the way, I don’t really understand the call module behavior (especially, what is the role of the id argument "mod" or whatever?).
I precise that I have already developed a classical Shiny app which works perfectly (server / ui, no modules used), but I want to make the vizualisation more "professional"...
Thanks for any help!
UPDATE: By curiosity, I have printed the "model" output:
selectModelInput("mod")
model <- callModule(selectModel,"mod")
renderPrint(model)
And this is the result:
<Observer>
Public:
.autoDestroy: TRUE
.autoDestroyHandle: function ()
.createContext: function ()
.ctx: environment
.destroyed: FALSE
.domain: session_proxy
.execCount: 2
.func: function (...)
.invalidateCallbacks: list
.label: observeEvent(input$target_model)
.onDomainEnded: function ()
.onResume: function ()
.prevId: 13
.priority: 0
.suspended: FALSE
clone: function (deep = FALSE)
destroy: function ()
initialize: function (observerFunc, label, suspended = FALSE, priority = 0,
onInvalidate: function (callback)
resume: function ()
run: function ()
self: Observer, R6
setAutoDestroy: function (autoDestroy)
setPriority: function (priority = 0)
suspend: function ()
It seems that 'model' variable is not what is expected (ie model_1, model_2 or model_3)... Why?