|
| 1 | +library(reactR) |
| 2 | +library(htmltools) |
| 3 | +library(pipeR) |
| 4 | +library(tibble) |
| 5 | + |
| 6 | +react_virt <- htmlDependency( |
| 7 | + name = "react-virtualized", |
| 8 | + version = "9.7.3", |
| 9 | + src = c(href = "https://unpkg.com/react-virtualized@9.7.3/"), |
| 10 | + script = "dist/umd/react-virtualized.js", |
| 11 | + stylesheet = "styles.css" |
| 12 | +) |
| 13 | + |
| 14 | +rand_tbl <- tibble( |
| 15 | + id = 1:10000, |
| 16 | + data = lapply(1:10000, function(d){runif(10)}) |
| 17 | +) |
| 18 | + |
| 19 | +js_data <- tags$script(HTML( |
| 20 | +sprintf( |
| 21 | +' |
| 22 | +var data = %s; |
| 23 | +', |
| 24 | + jsonlite::toJSON(rand_tbl, dataframe="rows") |
| 25 | +) |
| 26 | +)) |
| 27 | + |
| 28 | +js <- tags$script(HTML(babel_transform( |
| 29 | +sprintf( |
| 30 | +" |
| 31 | +function rowGetter(params) { |
| 32 | + return data[params.index]; |
| 33 | +} |
| 34 | +
|
| 35 | +function rowRenderCallback(x) { |
| 36 | + $('.sparkline').sparkline('html',{width:200}) |
| 37 | +} |
| 38 | +
|
| 39 | +class App extends React.Component { |
| 40 | + render() { |
| 41 | + return ( |
| 42 | + <ReactVirtualized.Table |
| 43 | + rowGetter={rowGetter} |
| 44 | + rowCount={data.length} |
| 45 | + height={300} |
| 46 | + width={400} |
| 47 | + rowHeight={40} |
| 48 | + headerHeight={30} |
| 49 | + onRowsRendered={rowRenderCallback} |
| 50 | + > |
| 51 | +
|
| 52 | + <ReactVirtualized.Column |
| 53 | + label='Index' |
| 54 | + cellDataGetter={ |
| 55 | + ({ columnData, dataKey, rowData }) => rowData.id |
| 56 | + } |
| 57 | + dataKey='index' |
| 58 | + width={50} |
| 59 | + /> |
| 60 | +
|
| 61 | + <ReactVirtualized.Column |
| 62 | + disableSort |
| 63 | + label='Data' |
| 64 | + dataKey='data' |
| 65 | + cellRenderer={ |
| 66 | + ({ cellData, columnData, dataKey, rowData, rowIndex }) => <span className='sparkline' values={cellData.join(',')}></span> |
| 67 | + } |
| 68 | + flexGrow={1} |
| 69 | + width={200} |
| 70 | + /> |
| 71 | +
|
| 72 | + </ReactVirtualized.Table> |
| 73 | + ) |
| 74 | + } |
| 75 | +
|
| 76 | +} |
| 77 | +
|
| 78 | +const app = document.getElementById('rvtable'); |
| 79 | +ReactDOM.render(<App />, app); |
| 80 | +" |
| 81 | +) |
| 82 | +))) |
| 83 | + |
| 84 | +tagList( |
| 85 | + tags$div(id="rvtable"), |
| 86 | + js_data, |
| 87 | + js, |
| 88 | + html_dependency_react(), |
| 89 | + react_virt, |
| 90 | + htmlwidgets::getDependency('sparkline')[2:3] |
| 91 | +) %>>% |
| 92 | + browsable() |
| 93 | + |
0 commit comments