Nextjournal / Jul 20 2020
Dash for R
This is a template for running Dash apps hosted in R runtimes. See Dash tutorials for more examples.
nextjournal.display.Dash <- function(app) { require(future) # reuse shiny display kind path <- paste0(nextjournal.out.filename(..nextjournal$device.n.plot), ".shiny.html") write(sprintf('<script data-rndm-id="%s"></script>', tempfile()), file=path) # run fiery in a future (block = FALSE option isn't working) future({ app$run_server(host = '0.0.0.0', port = 9998) }) message("Dash R: Fiery server launched")}0.1s
setup Dash display methodR
DashR
library(urltools)njUrl <- url_parse(Sys.getenv('NEXTJOURNAL_RUNTIME_SERVICE_URL'))message(njUrl)library(dash)library(dashCoreComponents)library(dashHtmlComponents)app <- Dash$new(requests_pathname_prefix = paste0('/', njUrl$path, '/'))df <- read.csv( file = "https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", stringsAsFactor=FALSE, check.names=FALSE)continents <- unique(df$continent)years <- unique(df$year)# dccSlider starts from 0;app$layout( htmlDiv( list( dccGraph(id = 'graph-with-slider'), dccSlider( id = 'year-slider', min = 0, max = length(years) - 1, marks = years, value = 0 ) ) ))app$callback( output = list(id='graph-with-slider', property='figure'), params = list(input(id='year-slider', property='value')), function(selected_year_index) { which_year_is_selected <- which(df$year == years[selected_year_index + 1]) traces <- lapply(continents, function(cont) { which_continent_is_selected <- which(df$continent == cont) df_sub <- df[intersect(which_year_is_selected, which_continent_is_selected), ] with( df_sub, list( x = gdpPercap, y = lifeExp, opacity=0.5, text = country, mode = 'markers', marker = list( size = 15, line = list(width = 0.5, color = 'white') ), name = cont ) ) } ) list( data = traces, layout= list( xaxis = list(type = 'log', title = 'GDP Per Capita'), yaxis = list(title = 'Life Expectancy', range = c(20,90)), margin = list(l = 40, b = 40, t = 10, r = 10), legend = list(x = 0, y = 1), hovermode = 'closest' ) ) })app1.5s
R
DashR
Env
install.packages("dash")install.packages("future")171.5s
DashR (R)