adding points interactively to a plot in Shiny

1,222 views
Skip to first unread message

Thibault Putseys

unread,
Aug 7, 2014, 4:31:15 AM8/7/14
to shiny-...@googlegroups.com
Dear Shining stars,

I'm working on a portfolio-app in Shiny and was wondering if there is a way to let the user add points to a graph repeatedly. 
I know you can just add 2 numericInputs for both the x and y input, but this way the point is not retained on the graph whenever the user changes the numericInput. 
Maybe it's possible to use some kind of ADD button or something? Have not found this yet on any Shiny galleries.. 

Thanks a lot for the help,

Tibo



Sigbert Klinke

unread,
Aug 7, 2014, 11:30:39 AM8/7/14
to shiny-...@googlegroups.com
Hi,

for similar problem I used a global variable in server.R, e.g. like

points <- list(x=vector("numeric", 0), y=vector("numeric", 0))

shinyServer
(function(input, output) {

  output$plotPoints
<-  <- renderPlot({
     ok
<- input$add
     points$x
<<- c(points$x, isolate(input$x))
     points$y
<<- c(points$y, isolate(input$y))
     plot
(points$x, points$y)
 
})
 
})


"add" is the button to click on and "x" and "y" are the input fields for the new additional coordinates. Best Sigbert


Thibault Putseys

unread,
Aug 8, 2014, 3:53:57 AM8/8/14
to shiny-...@googlegroups.com
Ok, I see what you're doing here, almost understand :) Just can't figure out what "ok <- input$add" is exactly doing? So, add refers to a button, but what value gets assigned to ok then and what's its use? 

Many thanks

Op donderdag 7 augustus 2014 17:30:39 UTC+2 schreef Sigbert Klinke:

Sigbert Klinke

unread,
Aug 8, 2014, 4:50:49 AM8/8/14
to shiny-...@googlegroups.com
No specific use, I just do not like to write only "input$add" which is necessary to make renderPlot aware of the button click, Sigbert

Thibault Putseys

unread,
Aug 8, 2014, 5:04:26 AM8/8/14
to shiny-...@googlegroups.com
Ahhh, I see, I haven't used any buttons before , thanks a lot. 

Op vrijdag 8 augustus 2014 10:50:49 UTC+2 schreef Sigbert Klinke:

Jitu Deka

unread,
Aug 4, 2019, 4:26:59 AM8/4/19
to Shiny - Web Framework for R
I have Searched the net but couldn't find How to do the same thing for the geom_bars.


Can I do the same for geom_bar? If Yes then what would be the approach. I have Y values which are selected based on my selection of  a different variable. As for example I have this dataframe

                   df <- data.frame(Scenarios = c(rep("Land",3),"Land_1"), Year = c(rep("2050",2), "2030"), Area_Crop = 
                                            c(100,200,300,400), Area_Water = c(40,50,60,70), Area_Forest =  c(150,250,350,450))

                                     
The selection from this table has to be used to plot a bar on an already existing bar plot where the Y values are already plotted for each year. This values can be melted using Scenarios and Year as measure.vars to bring it under one column (our Y value) and the same has been done on the table for the already existing plot. 

In  short I select a scenario than its respective bar plot should be added to the existing barplot just like you did it for the points.   
Reply all
Reply to author
Forward
0 new messages