Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image name from plot #22

Open
hmbotelho opened this issue Jul 29, 2016 · 11 comments
Open

Image name from plot #22

hmbotelho opened this issue Jul 29, 2016 · 11 comments

Comments

@hmbotelho
Copy link

In a plot: get the name of the image generating a point without clicking on it.

Two ways of doing this:

  • By hovering the mouse pointer over the point (which I believe requires a Shiny app)
  • A new "click & list" feature, whereby clicking a data point prints the treatment, Xcoordinate & Ycoordinate to the console.

Thanks!

@tischi
Copy link
Owner

tischi commented Aug 1, 2016

Long term: if I ever find the time I would love to migrate the GUI to Shiny.
Short term: Would it be enough if it prints all the information AND opens an image?
You could then go to the Visualisation Settings and select that NO image is shown and you would get the desired behavior with just one C&V button. Would that be OK?

@hmbotelho
Copy link
Author

Long term: That would be great. I know it is a lot of work though. Please consider my post as a "feature request"

Short term: That's fine. We have actually been doing just that.
Thanks for the feedback!

@tischi
Copy link
Owner

tischi commented Aug 2, 2016

I implemented the Short term solution.
Could you please check it?

@tischi
Copy link
Owner

tischi commented Aug 5, 2016

below code almosts does the job!
"just" have to figure out how to pass the data table to the app.R, unless everything is handling from within the shiny app, which is probably anyway the nicer solution!

# save this file in a folder and call it app.R
# launch it with print(source("../app.R"))

data = iris
fiji_binary = '/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx -debug '

library(plotly)
library(shiny)

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

  output$plot <- renderPlotly({
    plot_ly(data = data, x = Sepal.Length, y = Petal.Length, mode = "markers") 
    layout(xaxis = list(title = ""), 
           yaxis = list(title = ""))
  })

  output$selection <- renderPrint({
    s <- event_data("plotly_click")
    if (length(s) == 0) {
      "Click on a data point!"
    } else {
      print("You selected:")
      print(s)
      i = s[['pointNumber']]
      print(data[i,])
      print("Launching Fiji now....")
      system(fiji_binary,wait=F)
    }
  })

}

ui <- fluidPage(
  mainPanel(
    plotlyOutput("plot")
  ),
  verbatimTextOutput("selection")
)


shinyApp(ui = ui, server = server)

@hmbotelho
Copy link
Author

Cool!

I think the easiest way to pass the data table to app.r is through the global environment.
If the variable name is the same in HTM Explorer and the Shiny app you can use the "superassignment" operator <<- to make it visible across both apps.

Take a look at how I managed to do it. To be sure it worked I created my own dataset. Just place both files in the same folder.

R Console

setwd("yourpath")  
source("run.R")

run.R

# Create a dummy dataset
# data1 becomes available through the Global Environment
set.seed(100)
data1 <<- data.frame(Sepal.Length = runif(100, 4, 8))
data1$Petal.Length = data1$Sepal.Length * rnorm(100, 1.2, 0.1) - rnorm(100, 3.3, 0.5)

# Run shiny app using the data1 data frame
print(source("app.R"))

app.R

library(plotly)
library(shiny)

# Path to Fiji
fiji_binary <- if (Sys.info()['sysname'] == "Windows"){
                    'c:/Fiji.app/ImageJ-win64.exe -debug '
                } else {
                    '/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx -debug '}


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

    output$plot <- renderPlotly({

        # data1 should have been defined in run.R
        plot_ly(data = data1, x = Sepal.Length, y = Petal.Length, mode = "markers") 
        layout(xaxis = list(title = ""), 
               yaxis = list(title = ""))
    })

    output$selection <- renderPrint({
        s <- event_data("plotly_click")
        if (length(s) == 0) {
            "Click on a data point!"
        } else {
            print("You selected:")
            print(s)
            i = s[['pointNumber']]
            print(data1[i,])
            print("Launching Fiji now....")
            system(fiji_binary,wait=F)
        }
    })

}

ui <- fluidPage(
    mainPanel(
        plotlyOutput("plot")
    ),
    verbatimTextOutput("selection")
)


shinyApp(ui = ui, server = server)

@tischi
Copy link
Owner

tischi commented Aug 8, 2016

Yes! I think this is one option! I started reading on data handling in shiny and it seems that the "input" variable that is passed to each function might be also a place to store all the data. We should check what is better, i.e. global or input.
What I do not like about the shiny approach is that we loose control over the R console window....so we cannot access our data via the console while the gui is running, or?

@tischi
Copy link
Owner

tischi commented Aug 8, 2016

this seems relevant: http://shiny.rstudio.com/articles/scoping.html

@tischi
Copy link
Owner

tischi commented Aug 8, 2016

I tried to source a scatter-plot shiny app from within our HTM-Explorer GUI.

That is, I

  • use the HTM gui to configure the plot
  • store all settings globally in htm@settings@visualisation, for instance I store the columns in
    htm@settings@visualisation$cx and htm@settings@visualisation$cy.
  • source the shiny-app
  • and get the settings within the shiny-app via accessing the global htm@settings@visualisation
    This works in principle, however only once. If I try it a second time the whole R session crashes completely!

I have the feeling the using the gWidgets GUI together with transient shiny apps is too much for R to handle, as it does not know anymore to whom to "listen to".

@hmbotelho: what do you think?

@hmbotelho
Copy link
Author

Handling data
Hmm, maybe you are right. Maybe gWidgets+shiny is too much. That would be a pitty... Does the R sesssion always crash the second time you try to run the shiny app?

I can try it in our machines if you want.

That being said, I have noticed that sometimes shiny apps fail to boot. i.e., when I run runApp() the browser window opens but nothing else happens. In that case, I have to kill the app and do runApp() again to make it work. Maybe this kind of "hickup" may also explain your crashes. Wild guess...


**loosing control over the R console** This is unfortunately true and I also don't like it... Related issue: I have so far only able to open 1 Fiji instance at a time from within shiny+plotly :-( Need to explore this further
**Off-topic** I have been experimenting with how I would like the "HTMexplorer/Shiny/Plotly/Fiji" tool to look like in the end. Take a look:

https://github.com/hmbotelho/shinyHTM

The repo includes an image+csv dataset, which make for a ~200Mb download.

I have so far been replicating most of the HTM Explorer configurations simply because I have been developing this as a separate entity. At the moment, data must be uploaded directly to the shiny app. Get it at ./myplate_01/image_concat.csv

At the moment, I consider it as much an experimentation tool as a fun spare time personal project of mine. If you have the time, let me know what you think. I hope the Fiji stuff works on the Mac.

@tischi
Copy link
Owner

tischi commented Aug 8, 2016

cool! i was tempted to do the same :)
it does not run yet for me (see issues in your repo).

@hmbotelho
Copy link
Author

You are right. I have now pushed the bug fixes.
Thanks for noticing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants