GithubHelp home page GithubHelp logo

Is it expected behaviour that input$<plot>_click does not produce an event when the same selection is clicked a 2nd time? about upsetjs_r HOT 3 CLOSED

upsetjs avatar upsetjs commented on June 1, 2024
Is it expected behaviour that input$_click does not produce an event when the same selection is clicked a 2nd time?

from upsetjs_r.

Comments (3)

sgratzl avatar sgratzl commented on June 1, 2024 1

see e.g. https://github.com/upsetjs/upsetjs_r/blob/main/shiny/selectionP/app.R

from upsetjs_r.

RichardJActon avatar RichardJActon commented on June 1, 2024

OK so I've been trying to solve this problem in some different ways and I think there may actually be a bug here. These are the approaches I've tried:

library(shiny)
library(upsetjs)
library(purrr)
library(dplyr)

set_list_2_combinations <- function(set_list) {
	set_list <- set_list[
		order(purrr::map_int(set_list, length), decreasing = TRUE)
	]
	set_names <- names(set_list)
	purrr::map(seq_along(set_names), ~{
		lst <- combn(set_names, .x) %>% as.data.frame() %>% as.list()
		names(lst) <- purrr::map_chr(lst, ~paste0(.x, collapse = "&"))
		lst
	}) %>% unlist(recursive = FALSE)
}

set_list <- list("A" = c(1,2,3,4,5),"B" = c(1,2,3),"C" = c(2,6,7,8))
combinations <- c(list("none" = ""), set_list_2_combinations(set_list))

# Proxy ----
# Based on the Docs this seemed like the most efficient way to implement
# setting setSelection value by updating the values with the proxy
# functions but this did not work, the setSelection did not update on click
ui_proxy <- fluidPage(
	shiny::sidebarLayout(
		shiny::sidebarPanel(
			actionButton("reset_selection", "Reset Selection")
		),
		shiny::mainPanel(
			upsetjs::upsetjsOutput("upset_plot")
		)
	)
)

server_proxy <- function(input, output, session) {
	
	output$upset_plot <- upsetjs::renderUpsetjs({
		upsetjs::upsetjs() %>% 
			upsetjs::fromList(set_list)
	})
	
	observeEvent(input$upset_plot_click, {
		req(input$upset_plot_click$name)
		upsetjs::upsetjsProxy("upset_plot", session) %>% 
			upsetjs::setSelection(strsplit(
				isolate(input$upset_plot_click$name), split = "&"
			)[[1]])
	})
	
	observeEvent(input$reset_selection, {
		upsetjs::upsetjsProxy("upset_plot", session) %>% 
			upsetjs::setSelection("")
			# NB empty string not NULL, NULL does not reset the selection
	}) 
}

shinyApp(ui_proxy, server_proxy)

# input ----
# I went back to basics to see If I could get it working with and input
# to select the highlighted set and re-rendering the plot when the selection 
# changed. Abandoning the update on click/reset on click twice temporarily
ui_input <- fluidPage(
	shiny::sidebarLayout(
		shiny::sidebarPanel(
			selectInput(
				"set_2_highlight", "Set to highlight",
				choices = names(combinations)
			)
		),
		shiny::mainPanel(
			upsetjs::upsetjsOutput("upset_plot")
		)
	)
)

server_input <- function(input, output, session) {
	
	output$upset_plot <- upsetjs::renderUpsetjs({
		upsetjs::upsetjs() %>% 
			upsetjs::fromList(set_list) %>%
			upsetjs::setSelection(combinations[[input$set_2_highlight]])
	})
}

shinyApp(ui_input, server_input)


# reactive values ----
# I next tried a compromise still re-rendering the plot in place of proxy
# but with a reactive value that would respond to clicks in place of a direct
# input - this also did not work
ui_reactive <- fluidPage(
	shiny::sidebarLayout(
		shiny::sidebarPanel(
			actionButton("reset_selection", "Reset Selection"),
			verbatimTextOutput("split_sel")
		),
		shiny::mainPanel(
			upsetjs::upsetjsOutput("upset_plot")
		)
	)
)

server_reactive <- function(input, output, session) {
	
	output$upset_plot <- upsetjs::renderUpsetjs({
		upsetjs::upsetjs() %>% 
			upsetjs::fromList(set_list) %>%
			upsetjs::setSelection(upset_selection$selected_sets)
	})
	
	upset_selection <- reactiveValues(selected_sets = "")
	
	observeEvent(input$upset_plot_click, {
		req(input$upset_plot_click$name)
		upset_selection$selected_sets <- strsplit(
			input$upset_plot_click$name, split = "&"
		)[[1]]
	})
	
	observeEvent(input$reset_selection, {
		upset_selection$selected_sets <- ""
	}) 
	
	# debug info
	output$split_sel <- reactive({
		strsplit(
			input$upset_plot_click$name, split = "&"
		)[[1]]	
	})
}

shinyApp(ui_reactive, server_reactive)

from upsetjs_r.

sgratzl avatar sgratzl commented on June 1, 2024

re same selection: this is an odd behavior of rshiny, since it triggers an event with the same data, it won't be transferred to the server since the reactive state hasn't changed. currently investigating a workaround, e.g. by adding a nonce like an event timestamp

from upsetjs_r.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.