Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rhot x Shiny: params is null #198

Closed
Steeefan opened this issue Oct 10, 2017 · 6 comments
Closed

rhot x Shiny: params is null #198

Steeefan opened this issue Oct 10, 2017 · 6 comments

Comments

@Steeefan
Copy link

Steeefan commented Oct 10, 2017

Hi there,

I'm encountering an issue when trying to implement a custom renderer as mentioned here. I'm creating a rhot inside renderRHandsontable in which based on a value selected input$routeID the rows containing the selected value should be highlighted.

The code I have is this:

highlight = as.numeric(rownames(data[data$route_id==input$routeID,]))-1

rhandsontable(
  data,
  height = 300,
  width = '100%',
  row_highlight = highlight,
  stretchH = 'all'
) %>%
hot_cols(renderer = "
  function(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);

    tbl = HTMLWidgets.widgets.filter(function(widget) {
      return widget.name === 'rhandsontable'
    })[0];

    hrows = tbl.params.row_highlight
    hrows = hrows instanceof Array ? hrows : [hrows]

    if(hrows.indexOf(row) > -1) {
      td.style.backgroundColor = 'pink';
    }

    return td;
  }"
)

Running this w/o the call to hot_cols() works just fine but with it no table is shown. Using my browser's console I was able to trace it to the fact that I am indeed able to assign tbl to the corret table. The .params however is empty (params: null). This in turn leads me to believe that the code crashes on hrows = tbl.params.row_highlight (VM112:9 Uncaught TypeError: Cannot read property 'row_highlight' of null at eval (eval at window.HTMLWidgets.evaluateStringMember (htmlwidgets.js:732), <anonymous>:9:28)).

Has anyone encountered sth like this before and is able to help out?

Take care,
-Stefan

@Steeefan Steeefan changed the title rhot x Shiny: params is null null rhot x Shiny: params is null Oct 10, 2017
@Steeefan
Copy link
Author

Workaround for determing hrows:

hot_cols(renderer = paste0("
  function(instance, td, row, col, prop, value, cellProperties) {
    //snip

    hrows = [",
    paste(highlight, collapse=','),
    "];

    //snip
  }")
)

@jpark711
Copy link

Having a same issue. Please let me know if anyone has found a solution to load the params imported from R to the rhandsontable within Shiny.

@bklingen
Copy link

I just checked with version 0.3.6 and it seems that an R provided params cannot be accessed with rhandsontable in a shiny app. This code for row highlighting used to work fine for highlighting a user-specified row:

DF <- data.frame(val = 1:20, bool = TRUE, nm = LETTERS[1:20],
                 dt = seq(from = Sys.Date(), by = "days", length.out = 20),
                 stringsAsFactors = F)

app <- shinyApp(
ui = fluidPage(
  numericInput("myindex", "Highlight Row:", value=1, min = 1, max = 20),
  rHandsontableOutput("hot", height=200, width = 300)
),
server = function(input, output) {
  output$hot <- renderRHandsontable({
    myindex <- input$myindex - 1
    rhandsontable(DF, myindex = myindex) %>%
        hot_cols(renderer = "function(instance, td, row, col, prop, value, cellProperties) {
               Handsontable.renderers.TextRenderer.apply(this, arguments);
               if (instance.params) {
               mhrows = instance.params.myindex;
               mhrows = mhrows instanceof Array ? mhrows : [mhrows];
               }
               if (instance.params && mhrows.includes(row)) td.style.background = 'lightblue';
               "
        )
    })
  }
)
runApp(app)

However, even when installing older versions of rhandsontable this code no longer seems to work, so maybe the error is somewhere else.

@jrowen
Copy link
Owner

jrowen commented Jan 29, 2018 via email

@bklingen
Copy link

bklingen commented Feb 6, 2018

Sorry, my code above was missing a } and therefor did not work. Using an R provided params works just fine in the newest version 0.3.6 of rhandsontable. Here is code where you can choose with rows to highlight:

DF <- data.frame(val = 1:20, bool = TRUE, nm = LETTERS[1:20],
                 dt = seq(from = Sys.Date(), by = "days", length.out = 20),
                 stringsAsFactors = F)

app <- shinyApp(
  ui = fluidPage(
    textInput("myindex", "Chose Row(s) to highlight, separated by a comma:", value="2,3"),
    rHandsontableOutput("hot", height=200, width = 300)
  ),
  server = function(input, output) {
    output$hot <- renderRHandsontable({
      myindex <- as.integer(unlist(strsplit(input$myindex,","))) - 1
      rhandsontable(DF, myindex = myindex) %>%
        hot_cols(renderer = "function(instance, td, row, col, prop, value, cellProperties) {
          Handsontable.renderers.TextRenderer.apply(this, arguments);
          if (instance.params) {
            mhrows = instance.params.myindex;
            mhrows = mhrows instanceof Array ? mhrows : [mhrows];
          }
          if (instance.params && mhrows.includes(row)) td.style.background = 'lightblue';
          }"
        )
    })
  }
        )
runApp(app)

This should result in something like this:

image

@jrowen
Copy link
Owner

jrowen commented Jan 22, 2019

It looks like this is resolved, but please re-open if it's still an issue.

@jrowen jrowen closed this as completed Jan 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants