R select row of data frame interactively - r

I have a data frame, say:
df <- data.frame(a=1:10,b=runif(10))
I'd like to be able to display the data frame to the user and have them select (click) a row, and retrieve that row.
Something a bit like edit(df), except that what I want is much simpler in that I don't need editing functions --- I just need to listen for a click event on one of the rows and get the index for that row (I don't even need the particular cell!)
Does anyone know how I can do this? I'd prefer to do it with base R or grid (for the sake of not adding in lots of packages) -- maybe I can somehow draw the data frame on a grid graphics with a y scale defined from 1 to nrow(df) and use the grid.locator() function?
It'd be nice to avoid bringing in gui packages, but if I do, it should be cross-platform (linux/windows). gwidgets is quite nice (although they don't seem to have the click event nicely integrated with their gdf widget).
cheers.

well, here's a quick way, no extra packages, but you may have to fiddle with formatting if you want the table to be nicely aligned, rounded, etc:
df <- data.frame(a=1:10,b=runif(10))
df[menu(apply(df,1,paste,collapse=" "),graphics=TRUE),]
The device widens itself if necessary and scrollbars automatically appear when necessary.

I was going to suggest a combination of a blank plot filled with addtable2plot and then use locator to pick a point and calculate the row with a combination the y-specification and the cellheight <- max(strheight(c(column.names, row.names, as.vector(unlist(table))),... but effort in that direction seems silly since #timrifle seems to have hit the nail on the head.

Related

Coloring Cells of Data Tables with legend below Extracting it to excel

I want to color some of the cells of datatable and then create some kind of legend below. This is my code
x<-c(1,2,3,4,5,6,7,8,9,1,10,11,11,11,12,1,2,2,3,4,4,55,5,0,6,0,9)
y<-c(30,303,303,30,50,50,5,50,5,55,50,5,50,5,50,66,7,6,6,6,6,6,66,6,77,8,88)
Data<-cbind(x,y)
Data<-datatable(Data,caption = 'Data')%>%
formatStyle('y',
backgroundColor = styleInterval(c(0,50),c( 'normal','normal','yellow')))
save_html(Data,file="Data.html")
I want to add a memo or legend below which will explain what the color yellow means and extracting it as excel file
If you're going to be extracting it to Excel anyway, why not just extract it once you've colored your cells and then add it manually?
One of my favorite college professors gave me some great advice once when I was first learning R. I was trying to drop columns from a long dataset (30+ columns) and had asked him what the easiest way to do it in R would be. He asked me if I knew how to do it in Excel, which of course I did, and he said "There's a lot of ways to solve a problem, but the best way is the way that YOU can solve it."
It wouldn't hurt to know how to do it in R, but this is something that I don't think would be easier to do there than in Excel (or necessary, outside of edge cases).

Remove plot element without replotting everything else in R (Shiny)

This is not an specific question, but mostly a "looking for suggestions", since any kind of answer is welcome.
I built a huge website using pure shiny in R, but a really small thing is bothering me..
I'm plotting some elements over an image, let's say.. rectangles (could be anything), and interact with those elements using their position on the image. One of the actions is deleting some of those elements, but -as far as my understanding of R plotting system is- that can't be done, since an element plotted in R can't be removed, so my workaround is basically "plotting everything else" but the element i just deleted.
So, my question is, is there any way to "remove" an object in a plot without having to replot everything else (basically not making a new plot with everything but the deleted element)?
Thanks in advance!

Creating a grid on a map in R using grid points

I'm clearly struggling with this problem for a day now and can't seem to find a nice solution to it. I would really appreciate some help and I'm really a novice in R (since last week).
Problem 1:
I have a set CSV representing grid points which I can parse into a data frame (pointname, latitude, longitude).
Eg:
name,latitude,longitude
x0y0,35.9767,-122.605
x1y0,35.9767,-122.594
x2y0,35.9767,-122.583
x0y1,35.9857,-122.605
x1y1,35.9857,-122.594
x2y1,35.9857,-122.583
x0y2,35.9947,-122.605
x1y2,35.9947,-122.594
x2y2,35.9947,-122.583
The points in this file represent the lower left corner and are arranged in row major format, meaning lowest horizontal grid points first. Each point is a certain great circle distance away from its neighbors (1km). I want to create a grid overlay on a map which I've plotted using ggmap.
What I've tried or considered:
map.grid() - this is really not useful to me as I'm not looking for any kind of projection.
geom_vline() and geom_hline(). These look good but I don't have constant x and y intercepts on a plane. Moreover, once I create a grid, I'd like to use the grid to color against a density.
geom_rect() and geom_tile(). These look really promising and may be what I want. But I'm not able to find a good way of working with these.
I'd like to fill these grid boxes later with another parameter. Any suggestions on how I can create such a grid? This may be a trivial question but I don't know a lot of R yet.
Problem 2:
How can I store or hold such a grid so that I given a point (lat,lon), I can quickly get to that grid. In fact my whole back end is in C++ and can directly output the grid name x<n>y<n> directly against a given search point. I somehow am finding it difficult to count such points against grid points so that I can fill grid with a representative color.
I'm not sure if everything of what I'm saying is clear. Please tell me if I've to clarify something.
Also note that I've Googled quite a lot and not found relevant answers although some looked close.
Eg: This, ThisToo
Thanks for the help!

Manually Drawing Curves on R like Paint

I was trying to allow the user to give a curve as input for some analysis. Is there a package that can be used in this regard? Basically, a GUI like paint wherein the user can draw a simple curve and which would be stored in R as a set of data points or as an equation if its a simple curve.
Thanks
Gopher
Reading mouse input
Below is a list of sample approaches to first the problem, from the most complex to simplest (IMHO).
I would recommend that you have a look at the
qtpaint
package.
Less laborious solution may involve looking at the way interactive
plots in Shiny are handled. Using the click option would
enable you to get the mouse cursor position. Together with the Shiny
framework this would provide a convenient wrapper to get mouse input
from a user.
Finally, third approach could rely on making use of the locator function available in the graphics package. Depending on what you intend to do, this may be the simplest solution as you could then use read values to inform generation of your graphic.
Generating graphics
Assuming that you got your values via the locator functionality you could attempt to draw your line using segments. It would be a little fiddly as you would have to translate your coordinates to some placement of the dot in your desired chart but this wouldn't computationally too taxing, just a matter of bring mouse pointer values to some x/y values on the chart.
If you care to update your post with a sample code and reproducible attempts of your previous work, I bet the question will receive more replies. You may find this discussion on making a reproducible example in R helpful.

How to create a dyraph with multiple x-axis labels?

I'm working with a lot of rank data that would benefit from a way to simultaneously display its respective year on the x-axis. For example, I want to create the following graph adapted from the dygraph gallery:
Note how the rank information (red arrow) for a particular weekend (green arrow) are both displayed on the x axis.
I know this might not be possible with dygraphs now, at least it wasn't available in these demos, so I guess my follow up question would be are there any plans to make this possible (how about in the [r] {dygraph} package)? Apparently a plotter called flot can do this.
UPDATE 1
If indeed this feature does not exist yet, then the following, although potentially obvious to Dygraph developers, is a thought for accomplishing the task easily (perhaps I'm wrong). At first I thought it would be necessary to provide input data of the form shown in Table A
However such input is a major deviation from the existing Dygraph parser model who expects one abscissa. Such suggests that a modification to the parser to accept a "Dual Label" option, requiring that both labels be contained in a single abscissa element as in Table B, would be easier. Thereafter, with the option specified, the parser would manage CSV as it usually would with the exception that it is now "bin cognizant" and detects division between labels 1 and 2 by use of an acceptable delimiter (in this case a single quotation mark - maybe not the best choice) and divisions between label 1 abscissa elements by name change. Behind the scenes each point gets its unique x coordinate and the "Dual Label" option causes the dygraph to visually scoot up a couple pixels to accommodate an extra label. Not sure how to handle full zoomed scrolling put simply leaving a label 1 element centered until an adjacent label 1 element comes on screen is an option.
Dygraphs rule!
There's no simple way to do this with dygraphs now. As you say, there's a fairly deep assumption that there's only a single x-axis.
Your best bet would be to either modify the existing legend plugin to do this, or to create a new plugin which renders just the "Weekend 1", "Weekend 2" line. While the plugins API isn't official yet, it is at least somewhat documented.
If you get this to work, please share your code!

Resources