Open-Systems-Pharmacology / OSPSuite-R

R package for the OSPSuite
https://www.open-systems-pharmacology.org/OSPSuite-R/
Other
28 stars 12 forks source link

ospsuite addins #1412

Open pchelle opened 4 months ago

pchelle commented 4 months ago

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.

[!IMPORTANT] Feel free to comment

pchelle commented 4 months ago

Selecting and copying a unit

Example

image

Code

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)
}
pchelle commented 4 months ago

Get parameter from path by browsing a simulation tree

Example

Step 1 Step 2 Step 3
image image image

Code

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)
}
PavelBal commented 4 months ago

Nice! Should we maybe have a separate package ospsuite.addins?

pchelle commented 4 months ago

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 commented 4 days ago

@PavelBal Following up on that I drafted a repo with a few addins.

Yuri05 commented 4 days ago

@pchelle Very nice, great enhancements! @KatrinCoboeken @AnnikaRPS FYI