Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions tutorials/shiny/esquisse.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# Load libraries ----
library(shiny)
library(esquisse)
library(datamods)
library(tidyverse)

# Define UI ----
ui <- navbarPage(
title = "Navigation",

# Import datamods
tabPanel(
title = "Load Data",
fluidRow(
htmlOutput("welcome"),
tags$head(tags$style(HTML("
#welcome {
text-align: center;
}
")
)
),
hr()
),
fluidRow(
column(
width = 4,
checkboxGroupInput(
inputId = "from",
label = "Input Source",
choices = c("env", "file", "copypaste", "googlesheets", "url"),
selected = c("env", "file")
),
actionButton("launch_modal", "Launch Data Modal Window")
),
column(
width = 8,
htmlOutput("info")
),
column(
width = 8,
hr(),
htmlOutput("loaded"),
verbatimTextOutput("summary"),
tags$head(tags$style("#summary{max-width: 500px;}")) # sets max box window for scrolling
)
)
),

# Output: View Data ----
tabPanel(
title = "View Data",
htmlOutput("dtheader"),
hr(),
DT::dataTableOutput("contents")
# tableOutput("contents")
),

# Output: Esquisse ----
tabPanel(
title = "Plot (Esquisse)",
htmlOutput("esqheader"),
checkboxGroupInput(
inputId = "aes",
label = "Aesthetic Options:",
choices = c(
"fill", "color", "size", "shape",
"weight", "group", "facet", "facet_row", "facet_col"
),
selected = c("fill", "color", "size", "shape", "facet"),
inline = TRUE
),
esquisse_ui(
id = "esquisse",
header = FALSE, # set to TRUE to see blue esquisse icon header
container = esquisseContainer(
fixed = c(150, 0, 0, 0)
)
)
)
)

server <- function(input, output, session) {

output$welcome <- renderUI({
str <- tags$h3("Welcome")
str0 <- print("This app adapts code from add-ins and modules created by the dreamRs team at ")
tag <- tags$a(href="https://github.com/dreamRs/", "github.com/dreamRs")
spacer <- print(".")
HTML(paste(str, str0, tag, spacer, sep = ""))
})

output$info <- renderUI({
str1 <- print("<b>Instructions</b>")
str2 <- print("- Select input source options to upload dataset.")
str3 <- print("- Example datasets can be loaded through 'env' (global environment).")
str4 <- print("- Once desired dataset is selected, variable classes can be changed as needed.")
str5 <- print("&ensp; e.g., characters can be changed to factors if data is categorical.")
str_launch <- print("<br>Click on <b>'Launch Data Modal Window'</b> to launch module window.")
HTML(paste(str1, str2, str3, str4, str5, str_launch, sep = '<br/>'))
})

# Launch import data modal
observeEvent(input$launch_modal, {
req(input$from)
import_modal(
id = "myid",
from = input$from,
title = "Import data to be used in application"
)
})
data_imported_r <- import_server("myid", return_class = "tbl_df")
# data_imported_r <- datamods::import_server("import-data")

data_rv <- reactiveValues(data = data.frame())
observeEvent(data_imported_r$data(), {
data_rv$data <- data_imported_r$data()
data_rv$name <- data_imported_r$name()
})

# Output name as part of summary ----
output$loaded <- renderUI({
str_ld <- print("<b>Loaded Dataset: </b>")
str_fn <- print(data_rv$name) # dataset/file name
HTML(paste(str_ld, str_fn, sep = ""))
})


# Generate a summary of the dataset ----
output$summary <- renderPrint({
if(nrow(data_rv$data)==0){
# do nothing
}
else{
summary(data_rv$data)
}
# print(names(data_rv$data)) # variable names
})

# dataset contents header ----
output$dtheader <- renderUI({
HTML("<b>Please note:</b> activating a column filter cannot be reset to neutral at this time.")
})

# Show contents of dataset ----
output$contents <- DT::renderDataTable({
data_rv$data %>%
DT::datatable(options = list(pageLength=10),
rownames=FALSE)
})

# Header for Esquisse ----
output$esqheader <- renderUI({
HTML("<b>Instructions:</b> Drag and drop variables into aesthetic boxes below to generate plots.")
})

# Launch Esquisse ----
esquisse_server(id = "esquisse", data_rv = data_rv, default_aes = reactive(input$aes))

}

# Launch RShiny App
if (interactive())
shinyApp(ui, server)
52 changes: 52 additions & 0 deletions tutorials/solara/pokemon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""# Pokemon search
From: https://solara.dev/examples/general/pokemon_search

This example shows to to dynamically fetch data and render the results in a table when done, and show an error on failure.

It also shows an optional filter to narrow down the results.
"""

import solara
from solara import use_fetch
from solara.alias import rv

github_url = solara.util.github_url(__file__)

json_url = "https://jherr-pokemon.s3.us-west-1.amazonaws.com/index.json"


@solara.component
def Page():
data = use_fetch(json_url)
json = solara.use_json_load(data)
filter, set_filter = solara.use_state("")

with solara.Div() as main:
if json.error:
solara.Error(f"Error {json.error}")
solara.Button("Retry", on_click=data.retry)
else:
if json.value:
solara.InputText(label="Filter pokemons by name", value=filter, on_value=set_filter, continuous_update=True)
pokemons = json.value
if filter:
pokemons = [k for k in pokemons if filter.lower() in k["name"].lower()]
if len(pokemons) == 0:
solara.Warning("No pokemons found, try a different filter")
else:
solara.Info(f"{len(pokemons)} pokemons found")
else:
solara.Info(f"{len(pokemons)} pokemons in total")
with solara.GridFixed(columns=4, align_items="end", justify_items="stretch"):
for pokemon in pokemons[:20]:
with solara.Div():
name = pokemon["name"]
url = "https://jherr-pokemon.s3.us-west-1.amazonaws.com/" + pokemon["image"]
# TODO: how to do this with solara
rv.Img(src=url, contain=True, max_height="200px")
solara.Text(name)
else:
with solara.Div():
solara.Text("Loading...")
rv.ProgressCircular(indeterminate=True, class_="solara-progress")
return main
Loading