Open pchelle opened 4 months ago
unitPicker <- function() {
ui <- miniPage(
gadgetTitleBar("OSP Unit Picker", left = NULL),
miniContentPanel(
selectInput(
inputId = "dimension",
label = span(icon("folder-open"), " Dimension"),
choices = names(ospsuite::ospUnits)
),
selectInput(
inputId = "unit",
label = span(icon("folder-tree"), " Unit"),
choices = as.character(ospsuite::ospUnits$`Abundance per mass protein`)
),
actionButton(
inputId = "copy",
label = "Copy",
icon = icon("copy")
)
)
)
server <- function(input, output, session) {
# Update available units based on selected dimension
observeEvent(input$dimension, {
updateSelectInput(
session = session,
inputId = "unit",
choices = as.character(ospsuite::ospUnits[[input$dimension]])
)
})
# Copy the unit wrapped with quotes in your current R script position
observeEvent(input$copy, {
rstudioapi::insertText(paste0('"', input$unit, '"'))
})
observeEvent(input$done, {
stopApp()
})
}
runGadget(ui, server)
}
Step 1 | Step 2 | Step 3 |
---|---|---|
simulationPathAddin <- function() {
# Get object from selected text in RScript
# Can add validation of the object type at this stage
selectedText <- rstudioapi::getSourceEditorContext()$selection[[1]]$text
uiSimulation <- get(selectedText)
simulationTree <- ospsuite::getSimulationTree(uiSimulation)
ui <- miniPage(
gadgetTitleBar("Simulation Test", left = NULL),
miniContentPanel(
# Print the selected text and simulation name to check
htmlOutput("selectedText"),
htmlOutput("simulationName"),
br(),
span(
icon("square-check"), strong(" Selected Path:"),
verbatimTextOutput("selectedPath"),
actionButton(
inputId = "copy",
label = "Copy",
icon = icon("copy")
),
br(),
span(icon("folder-tree"), " Simulation Tree"),
# Print simulation paths as an interactive tree leveraging shinyTree package
shinyTree(
"tree",
themeIcons = FALSE,
themeDots = FALSE,
theme = "proton"
)
)
)
)
server <- function(input, output, session) {
output$selectedText <- renderPrint({
span(
icon("arrow-pointer"),
" Selected text: ",
strong(selectedText)
)
})
output$simulationName <- renderPrint({
span(
icon("id-card"),
" Simulation name: ",
strong(uiSimulation$name)
)
})
# Get path from selected shiny tree node
getPath <- reactive({
treeNames <- get_selected(input$tree, format = "names")
if (length(treeNames) == 0) {
return()
}
treeNames <- treeNames[[1]]
ospsuite::toPathString(attr(treeNames, "ancestry"), treeNames)
})
output$tree <- renderTree({
simulationTree
})
output$selectedPath <- renderPrint({
getPath()
})
# Copy the path wrapped with example text in your current R script position
observeEvent(input$copy, {
if (is.null(getPath())) {
return()
}
printedText <- paste0(
'param <- getParameter("',
getPath(),
'", container = ', selectedText, ")"
)
rstudioapi::insertText(printedText)
})
# When the Done button is clicked, return a value
observeEvent(input$done, {
stopApp()
})
}
runGadget(ui, server)
}
Nice! Should we maybe have a separate package ospsuite.addins
?
Great idea !
This way only the addins will need to support the extra interactive dependencies such as rstudioapi
, shiny
, shinyTree
or miniUI
.
And we can centralize in the repo what user would like as addins.
@pchelle Very nice, great enhancements! @KatrinCoboeken @AnnikaRPS FYI
RStudio provides an Addins feature that users could leverage when writing scripts.
I drafted below 2 simple functions for potential addins that could be used as starting point for discussing of such features.