How to add tooltips for gactions in gmenu bars? - r

I want to add a tooltip which pops up (at the position of the curser) by mouse hovering over the specific entry in a menubar.
I am using Windows, gwidgets with RGtk2 and R version 3.0.3
My code looks like this:
PG_top <- gwindow(...)
action_list = list (
open = gaction(label = "Open...", tooltip = "Open a File", icon = "open", handler = openDialogHandler, parent = PG_top)
)
menu_bar_list <- list(File = list (
Open = action_list$open
)
)
menu_bar <- gmenu(menu_bar_list, cont=PG_top)
I get no error messages nor any tooltip. Is this a toolkit Problem? I found a Handler called "addHandlerMouseMotion" but I don't know if this works for my kind of problem and what to do inside this Handler. If I try
tooltip<-(action_list[["open"]],"Open File")
or
tooltip<-(action_list$open,"Open File")
I get the errormessage: Error in (...): unexpected ","
Hope you can help me!

Related

How can I create a popup input dialogue box with multiple inputs in R?

I'd like to generate a popup box in R with six inputs.
I prefer to have all the inputs in the same pop-up box because most of the time most will be the default, and I'd like to quickly see at a glance that they are all correct rather than having to click through six popups.
I know dlg_input in the svDialogs package gives a popup box, but I don't see an option to modify it to allow for multiple inputs. Here's the code for dlg_input:
function (message = "Enter a value", default = "", ..., gui = .GUI)
{
if (!guistartUI("dlginput",call=match.call(),default=default,
msg="Displayingamodalinputdialogbox",msg.no.ask="Amodalinputdialogboxwasby−passed"))
return(invisible(gui))
if(!length(message))
message<−"Enter a value"
message<−paste(as.character(message),collapse="\n")
if(is.null(default)){
default<−""
}
else {
default<−as.character(default)[1]
}
gui$setUI(args = list(message = message, default = default))
UseMethod("dlgInput", gui)
}
<environment: namespace:svDialogs>
I assume there's a way to modify this code to give me what I want, but it's lost on me. Any help would be much appreciated.

Search from a textInput to a Handsontable in Shiny

I've been working for some days with Handsontable in Shiny and I got stuck in what I guess will be a very dumb question but I have not this much idea how to solve.
I have a Handsontable that has a custom function that allows searching and it works. It works but is not intuitive enough because you have to right-click on the table to pop the search option.
Because of this, I decided that I would like to have a textInput that does the same function but in a prettier way. I know that it should be related with an observeEvent of the input variable (input$searchId) but I have no idea of how to do it due to my lack of experience with Shiny and Handsontable.
This is the code from server.R that prints the table and that has a custom function that allows the user to search.
output$hot <-renderRHandsontable({rhandsontable(Dataset(),height = 600)%>%
hot_table( columnSorting = TRUE,highlightCol = TRUE, highlightRow = TRUE, search = TRUE) %>%
hot_context_menu(
customOpts = list(
search = list(name = "Search",
callback = htmlwidgets::JS(
"function (key, options) {
var aux = document.getElementById('searchId').value;
var srch = prompt(Search);
this.search.query(srch);
this.render();
}")))) })
And what I would like is to archive the same result but without having to right-click on the table and create a prompt.
Thank you so much,
Well I've been able to solve my problem. I've been inspired by this post
and then I got with something like:
js_search <- "
$(document).ready(setTimeout(function() {
document.getElementById('searchId').onchange = function(e){
var hot_instance = HTMLWidgets.getInstance(hot).hot
console.log('hola')
var aux = document.getElementById('searchId').value;
hot_instance.search.query(aux);
hot_instance.render();
}
}))
"
that has to be included in your ui.R with a tags$head(tags$script(HTML(js_search)))
That's all the problem I was having is that I ahd no idea of how to get the "this" from the custom operation in the server side I had before. Once you know that is hot_instance. where hot is the name of my table, I think is easy.

Function to generate dropdown notifications Shiny R

Inside the dashboardHeader, how can you programatically generate dropdown menu items?
dropdownMenu(
type = "notifications",
notificationItem(
text = "message",
icon = icon("welcome"),
status = "warning"
),
notificationItem(
text = "message",
icon = icon("welcome"),
status = "warning"
),
... #Generate lots more messages
What is the general method in R to generate messages, say from another function that takes in an argument which is the number of messages:
GenerateMessages <- function(number.of.messages) {
#Code to generate messages
}
What would be the code, would it be written in the UI or the Server functions in shiny, dashboard header?
I'll answer my own question since it goes beyond the basic dynamic tutorial. I really like this solution because my notifications and logic can go outside of my code, it shortened my app.R by hundreds of lines.
The general form is:
Code inside the UI function:
# Code to create outputs goes in dashboardHeader
dropdownMenuOutput("messages.type"),
dropdownMenuOutput("notifications.type"),
dropdownMenuOutput("tasks.type")
Code inside Server function:
# Code to generate headers
output$messages.type <- renderMenu(
dropdownMenu(type = "messages", .list = MessageGenerator())
)
output$notifications.type <- renderMenu(
dropdownMenu(type = "notifications", .list = NotificationsGenerator())
)
output$tasks.type <- renderMenu(
dropdownMenu(type = "tasks", .list = TasksGenerator())
)
The main logic is in the MessageGenerator() function. These functions generate a list required to render in the output. The data structure is from a data.frame containing the message information with the proper headers.
This solution scales to generate messages of the three types Messages, Tasks, and Notifications using three functions MessageGenerator(), TasksGenerator(), NotificationsGenerator().

Setting the interaction model of a Dygraph in Shiny for R

I am looking to add the custom interaction seen at http://dygraphs.com/gallery/#g/interaction under "Custom interaction model" into my Shiny web app.
As far as I understand it, this requires attaching some JS to the page and setting the interaction model on the graph:
interactionModel : {
'mousedown' : downV3,
'mousemove' : moveV3,
'mouseup' : upV3,
'click' : clickV3,
'dblclick' : dblClickV3,
'mousewheel' : scrollV3
}
However, interactionModel does not seem to be listed as a parameter in the dyOptions function on the R side.
Is there a way to work around this?
Update:
Looking at the source for dyOptions, it seems that options can be modified directly:
g <- dyGraph(series)
g$x$attr$option <- "Value"
However, setting the interactionModel here does not seem to work.
See: https://github.com/rstudio/dygraphs/blob/master/R/options.R
Update:
You can indeed set the options using:
g$x$attrs$option <- "Value" # Note that it is "attrs", not "attr"
This can be used to switch off the interaction mode:
graph$x$attrs$interactionModel <- "{}"
The remaining problem is passing JS function references via JSON to the page.
You can use the JS function to pass JavaScript over JSON to the client.
In ui.R:
tags$head(tags$script(src="interaction.js"))
In server.R:
g <- dygraph(series(), main = "Graph", xlab = "Date", ylab = "Amount") %>%
dySeries(label = "X")
g$x$attrs$interactionModel <- list(
mousedown = JS("downV3"),
mousemove = JS("moveV3"),
mouseup = JS("upV3"),
click = JS("clickV3"),
dblclick = JS("dblClickV3"),
mousewheel = JS("scrollV3"))

How do I update a gedit box instantly after using gfile to specify a filepath, using gWidgetsRGtk2, in R

I am trying to create a GUI using gWidgetsRGtk2 for a program that I have written in R. My GUI has a gedit() text box in which the user can type a file path for the input data file to be put into the program. It also has a 'browse' button, which, when clicked, opens up a gfile() box so that they can browse for the file that they want. What I am having trouble with is updating the value in my gedit() box, after the user has selected their file using the 'browse' button. The code below may make this clearer:
dir <- getwd()
sfilepath <- paste0(dir,"/")
win = gwindow("Set Parameters:",width=400,height=550)
nb = gnotebook(cont=win)
tab2 <- glayout(cont=nb, label = "Advanced Settings")
tab1 <- glayout(cont=nb, label = "Basic Settings")
tab1[2,2] <- glabel("BD:",cont=tab1)
tab1[2,4:5] <- gedit(1,cont=tab1)
addhandlerkeystroke(tab1[2,4],handler=function(h,...){BD <<- as.numeric(svalue(h$obj))})
tab1[3,2:5] <- gseparator(cont=tab1)
tab1[4,2:5] <- glabel("File path:",cont=tab1)
tab1[5,2:4] <- gedit(paste0(dir,"/"),cont=tab1)
tab1[5,5] <- gbutton(text="Browse", handler=function(h,...){ gfile("Select a file",type="open", filter = list("text files" = list(patterns = c("*.csv","*.txt")), "R files" =list(patterns = c("*.R","*.Rdata"))), handler = function(h,...){ sfilepath <<- h$file},cont=TRUE)},cont=tab1)
addhandlermousemotion(tab1[5,2],handler=function(h,...){svalue(h$obj) <- sfilepath})
So far I have tried using addhandlermousemotion, as in the code above, so the text in the gedit() box is only updated when you move the mouse over the box itself. However, I would prefer it if the text in the box updated instantly.
I have also tried using addhandleridle(), with an interval of 1 second, so that the text in the box will be automatically updated every 1 second. This worked. However, it made it impossible to type in the box properly, because the text box was being updated with the old 'sfilepath' before it had saved the new 'sfilepath' that was being typed in.
I am a beginner at making at GUIs (I have written a program for work, but it needs to be used by someone else once I leave, so decided last Friday that I should figure out how to make it into a GUI). So any help that anyone can offer would be greatly appreciated.
Here is the pattern you want (passing a handler to gfilebrowse):
w <- gwindow("test")
g <- ggroup(cont=w, horizontal=FALSE)
file_upload <- gfilebrowse(cont=g, handler=function(h,...) {
svalue(e) <- svalue(h$obj)
})
e <- gedit("", cont=g)

Resources