R以交互方式选择数据帧行 [英] R select row of data frame interactively

查看:120
本文介绍了R以交互方式选择数据帧行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,说:

  df<  -  data.frame(a = 1:10,b = runif(10))

我希望能够向用户显示数据框并让他们选择(点击)一行,检索该行



有点像编辑(df ),除了我想要的是更简单,因为我不需要编辑功能---我只需要听一个单击事件在其中一行,并获得该行的索引(我甚至不需要特定的单元格!)



有谁知道我能做到这一点吗?我更喜欢用base R或 grid (为了不添加很多包),也许我可以以某种方式在网格上绘制数据框图形与ay比例定义从1到 nrow(df)并使用 grid.locator() function? p>

避免引入gui包是很好的,但如果我这样做,应该是跨平台的(linux / windows)。 gwidgets 是非常好的(虽然他们似乎没有将它们与 gdf ​​小部件集成在一起)。



欢呼。

解决方案

额外的包,但如果您希望表格对齐,圆整等等,您可能必须调整格式:

  df <  -  data.frame(a = 1:10,b = runif(10))
df [menu(apply(df,1,paste,collapse =),graphics = TRUE) $ b

如果需要,设备会自动扩展,并在必要时自动显示滚动条。


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.

这篇关于R以交互方式选择数据帧行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆