Get Selected Rows of Rhandsontable

select number of rows to preview in rhandsontable

You can pass min/max rows/columns to the function: https://github.com/handsontable/handsontable/issues/225

library(shiny)
library(rhandsontable)

shinyApp(
ui = fluidPage(
fluidRow(
column(12,
sliderInput('input', label = "Rows",
min = 1, max = nrow(iris), value = 10)
),

column(12,
rHandsontableOutput('table')
)
)
),
server = function(input, output) {
output$table <- renderRHandsontable(
rhandsontable(iris, width = 550, height = 300, maxRows = input$input)
)
}
)

Sample Image

Rhandsontable collect values from logical == TRUE

You could try the following approach. Use hot_to_r to get your data from the handsontable as an R object. Check the first column for any checked items (which would be TRUE boolean values). If there are, you can extract the second column of data, using the row indices based on the first column that are TRUE.

Note that the code in output$selected can be moved to a separate reactive expression, so that the selected results can be used elsewhere.

Also, you would need parentheses for selected2(). selected2() should return a character vector of the Type selected.

To select the appropriate columns from your second data.frame Aims_DF_NEW, you can try:

Aims_DF_NEW[, names(Aims_DF_NEW) %in% selected2(), drop = F]

This will only include columns in Aims_DF_NEW that are included in the selected2() result. drop = F is added so the result is not coerced to a vector if only 1 column selected (and remains a data.frame).

Here is a revised version to subset the second table based on the first table (second table simplified to demonstrate).

library(rhandsontable)
library(shiny)

orgs <- c("Community leaders/representatives",
"Members of local community/indigenous committees",
"Landowners/customary area owners",
"National government",
"Sub-national or local government",
"Managed area manager/personnel",
"International NGO",
"Local or national NGO",
"Community based organizations - women’s groups",
"Community based organizations - men’s groups",
"Community based organizations - youth/school groups",
"Community based organizations - religious groups",
"Community based organizations - conservation groups",
"Industry",
"Private sector",
"Academic institute or research facility",
"Other")

proj_aim3 <- data.frame(Category = c("Area", "Condition", "Diversity"))
proj_aim3 <- cbind(proj_aim3, setNames( lapply(orgs, function(x) x=NA), orgs))
Aims_DF_NEW <- proj_aim3
imps2 <- c("Primary", "Secondary", "Tertiary")

ui <- fluidPage(
rHandsontableOutput('Intiated'),
verbatimTextOutput('selected'),
br(),
rHandsontableOutput("Aims2")
)

server <- function(input, output, session) {

cats <- c("Community leaders/representatives", "Members of local community/indigenous committees", "Landowners/customary area owners", "National government", "Sub-national or local government", "Managed area manager/personnel",
"International NGO", "Local or national NGO", "Community based organizations - women’s groups", "Community based organizations - men’s groups",
"Community based organizations - youth/school groups", "Community based organizations - religious groups", "Industry", "Private sector",
"Academic institute or research facility", "Not recorded", "Other")

DF <- data.frame(Tick = rep(FALSE, length(cats)), Type = cats, Name = rep("", length(cats)))

output$Intiated <- renderRHandsontable(
rhandsontable(DF, selectCallback = TRUE, readOnly = FALSE)
)

selected2 <- reactive({
dat <- hot_to_r(input$Intiated)
if (any(dat[[1]])) {
dat[which(dat[[1]]), 2]
}
})

output$selected <- renderPrint({
cat(paste(selected2(), collapse = "\n"))
})

output$Aims2 <- renderRHandsontable({
rhandsontable(Aims_DF_NEW[, names(Aims_DF_NEW) %in% selected2(), drop = F], rowHeaders = NULL, width = 1500, height = 600)
})

}

shinyApp(ui = ui, server = server)

Retrieving values from an rhandsontable object (R, R shiny)

The question is 4 years old, but still relevant for the rhandsontable package users. Also, the solution provided by Lyx above no longer works. Following is an easy fix to the problem.

Every rhandsontable object is a deeply nested list. One of its elements is the data element, which itself is nested under the x element. However, the data is in json format, but it can easily be converted to a data.frame by using the fromJSON() function in the jsonlite package.

library(rhandsontable)
library(jsonlite)

hands_on_table <- rhandsontable(mtcars)
data_frame <- fromJSON(hands_on_table$x$data)
head(data_frame)

mpg cyl disp hp drat wt qsec vs am gear carb
1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

Edit:

It is important to also mention that the main difference between using hot_to_r and jsonlite::fromJSON is that the former is used while the app is running and the latter only works in an interactive R session.

Adding multiple rows in rhandsontable via context menu

I had the same issue, and solved it by designating the option that will be customized by adding insert_row = list inside list :

library(rhandsontable)

iris %>%
head(5) %>%
rhandsontable() %>%


hot_context_menu(customOpts =

list(
insert_row = list(
name = "Add 5 rows at the top",
callback = htmlwidgets::JS(
"function (key, options) {
this.alter('insert_row',0, 5);
this.render();
}"
)
)
))

I also changed this.alter('insert_row',1, 5);as in your example to this.alter('insert_row',0, 5);. That is because if you have it as in your example, the new 5 rows are added below the first row, whereas as in my example the new rows will be created at the top of the table. To get the rows below, use this.alter('insert_row',[0], 5);



Related Topics



Leave a reply



Submit