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

logical value and checkbox #18

Open
stla opened this issue Feb 16, 2015 · 19 comments
Open

logical value and checkbox #18

stla opened this issue Feb 16, 2015 · 19 comments

Comments

@stla
Copy link

stla commented Feb 16, 2015

Hello,
My dataframe has a column c(TRUE,TRUE,TRUE). When I render this dataframe with renderHtable and htable there is a checkbox in each cell and it is nice. But when I uncheck one box I get #bad value# in the rendering.
shinytable

@smartinsightsfromdata
Copy link

@trestletech This is the same problem as this one #8

I investigated the reason: the logic you use to build the deltas to send to handsontable uses a matrix, which converts everything to text including the logic / picker datatypes.
As a result the setDataAtCell call in the JS code sends data of a different datatype to update the cell == error.
Sadly I do not have time to work on this and submit a pull. Maybe there are other volunteers?
@jrowen or anyone?

@jrowen
Copy link

jrowen commented Feb 19, 2015

I'm testing a pull request #15 that includes some additional logic around maintaining data.frame types. I just added a fix for logical columns but have not done a lot of testing. Feel free to try it out and let me know if you run into any problems.

@smartinsightsfromdata
Copy link

@jrowen It does not work.
It is quite odd: on a trace I see everything ok between client and server, but the pickers are not rendered (but true / false is displayed instead).
I use options(shiny.trace = T) for the trace and for testing the following simple data.frame:

      tbl1 <- data.frame(num1=1:rows, 
                         num2=(1:rows)*20,
                         letter=LETTERS[1:(rows)],
                         select=FALSE,
                         stringsAsFactors=F) 

I'll keep investigating but I would appreciate if you could have a look as well. Thanks!

@jrowen
Copy link

jrowen commented Feb 22, 2015

If you are using the inst/examples/02-matrix be sure to update the htable parameters in ui.R to include colHeaders = "provided", setColTypes = TRUE.

@smartinsightsfromdata
Copy link

@jrowen Ah! Let me try with setColTypes = TRUE

@smartinsightsfromdata
Copy link

@jrowen It does not work: see attached sample file. Basically if a number of checkbox are picked only the last one is returned by shiny...

library(shiny)
suppressWarnings(suppressMessages(library(shinyTable)))
library(data.table)
# options(shiny.trace = T)  # change to T for trace
# options(shiny.reactlog=TRUE)

##
# source(paste0(".","/","global.R"), echo=FALSE)
##
a <- "Test"
dt_ <- data.frame(  val = as.numeric(11:22) ,  value = 1:12,
                    vals = 31:42, months= month.abb, select = F  ) 

##
runApp(list( ui = fluidPage(
    fluidRow(column(6, htable("hdtable2",colHeaders = "provided", setColTypes = TRUE)),
            column(3,uiOutput("outSel")) )                        
), 
server = function(input, output, session) {


output$hdtable2 <- renderHtable({

if (length(input$hdtable2)==0) {
  hdtable2 <- dt_
  return(hdtable2)
}
})

output$outSel <- renderUI({
if (length(input$hdtable2)==0) { return() }
dt1 <- data.table(input$hdtable2)

picked <- as.vector(unlist(unique(dt1[dt1$select==T,months])))
if(length(picked)==0) { return() }

buttn <- 1
iter <- length(picked)
valsTv <- sapply(1:iter, function(i) {
      if(is.null(input[[paste0(month.abb[i],"-","a",buttn)]])) {
        0
      } else {
       as.numeric(input[[paste0(month.abb[i],"-","a",buttn)]])
        }
})
toRender <- lapply(1:iter, function(i) {
  sliderInput(inputId = paste0(month.abb[i], "-","a",buttn),
              label =  h6(paste0(month.abb[i],"")),
              min =  -100,
              max = 100,
              step = 1,
              value = as.numeric(valsTv[i]) ,
              post="%",
              ticks = FALSE, animate = FALSE)
})


# print(toRender)
return(toRender) 
  })

# }
}))
#

@jrowen
Copy link

jrowen commented Feb 27, 2015

If I start with the 02-matrix example, replace line 19 in ui.R with
htable("df", contextMenu = TRUE, colHeaders = "provided", setColTypes=TRUE)
and add tbl[, 5] = TRUE at line 37 in server.R,
I'm seeing the check boxes in the rendered table and any changes in the check boxes showing in the table printed to the console.

Take a look at the caching logic in the example--you may need to add something similar to your code.

@smartinsightsfromdata
Copy link

@jrowen Many thanks for the help so far: you are right: the logic values perform all right.

On the other hand (unless I' m again missing the wood for the trees) shinyTable seems to have issues to update the table.
See the code below (based on 02-matrix as you suggested. From the shiny trace I would expect shinyTable to update the client with a progression of values for tbl[1,1] each time I select a checkbox.
I can see the incremented values in the trace alright, but nothing gets updated / displayed on the handsontable in the client.
Help!!

library(shiny)
library(shinyTable)
options(shiny.trace = T)
#' Define server logic required to generate simple table
#' @author Jeff Allen \email{jeff@@trestletech.com}
shinyServer(function(input, output, session) {
  cachedMat <- NULL
  cachedDf <- NULL

  output$df <- renderHtable({
    if (is.null(input$df) && is.null(cachedDf)){
      rows <- 5
      # Seed the element with some data initially
      tbl <- data.frame(round(matrix(rnorm(rows^2), ncol=rows), 5))
      colnames(tbl) = paste0("B", 1:5)
      tbl[, 5] = FALSE

      cachedDf <<- tbl
      #print(tbl)
      return(tbl)
    } else if (!is.null(input$df)) {
      cachedDf <<- tbl <- input$df
      tbl[1,1] <- tbl[1,1]+1
      #print(input$df)
      return(tbl)
    }
  }) 
})

@jrowen
Copy link

jrowen commented Mar 2, 2015

I just pushed another update that should fix the problem. Let me know if you're still running into trouble.

@smartinsightsfromdata
Copy link

@jrowen - thanks for the great works!!. Just two issues to report (after significant testing):

  • the update does not work with the current wip version on shiny (e.g. the one you get downloading it directly from hithub). The error appears after a full cycle when you click on the checkbox:
Error in orig(name = name, shinysession = self) : 
  attempt to apply non-function
  • Normally the way I use these components I have an observer pointing to the table that is invalidated when the table is rendered, declared as:
if(is.null(input$shinyTable)) { return()}

For example, referring to the code I posted 6 days ago with dynamic sliders, if I decide the first time to render the table with the column "select" equals to TRUE (e.g. to get all the sliders out at the same time), I would expect the dyn. sliders code to fire immediately after the table is rendered, realising that all the checkbox are selected and render immediately all of the sliders.
I need instead to unselect one of the checkbox for this to happen: it is reactive only on manual intervention (checking / unchecking of the checkbox), not after updating the table. This is creating issues with the logic of my code and I'm not sure what are the alternatives...

@jrowen
Copy link

jrowen commented Mar 4, 2015

In this case, I believe you need to use reactiveValues, which is similar to the cache variables in the examples. This works for me.

library(shiny)
library(shinyTable)

a <- "Test"
dt_ <- data.frame(val = as.numeric(11:22), value = 1:12,
                  vals = 31:42, mth = month.abb, 
                  sel = c(T, rep(F, 11)), stringsAsFactors = F) 

runApp(list( 
  ui = fluidPage(
    fluidRow(column(6, htable("hdtable", colHeaders = "provided", 
                              setColTypes = TRUE)),
             column(3, uiOutput("outSel")) 
    )                        
  ), 
  server = function(input, output, session) {
    cache = reactiveValues()

    output$hdtable <- renderHtable({
      if (is.null(input$hdtable)) {
        cache[["dt"]] = dt_
      } else {
        cache[["dt"]] = input$hdtable
      }
      return(cache[["dt"]])
    })

    output$outSel <- renderUI({
      if (!is.null(cache[["dt"]])) {
        dt1 = cache[["dt"]]

        picked <- dt1[dt1$sel == T, "mth"]
        if(length(picked) >= 0) { 
          buttn <- 1
          valsTv <- sapply(picked, function(i) {
            if(is.null(input[[paste0(i, "-", "a", buttn)]])) {
              list(value = 0)
            } else {
              list(value = as.numeric(input[[paste0(i, "-", "a", buttn)]]))
            }
          }, simplify = F)

          tagList(
            lapply(picked, function(i) {
              sliderInput(inputId = paste0(i, "-", "a", buttn),
                          label =  h6(paste0(i, "")),
                          min =  -100,
                          max = 100,
                          step = 1,
                          value = as.numeric(valsTv[[i]]$value) ,
                          post="%",
                          ticks = FALSE, animate = FALSE)
            })
          )
        }
      }
    })
  }
))

@smartinsightsfromdata
Copy link

@jrowen just marvellous!! Do you have an opinion about the issue with the current dev. version of shiny?

@jrowen
Copy link

jrowen commented Mar 4, 2015

You could try building the branch under my fork.

$ git clone https://github.com/jrowen/shinyTable.git
$ git checkout origin/update-context-menu

@smartinsightsfromdata
Copy link

@jrowen This is what I've been doing (once I understood how to!). You rock!
The issue I got is when I downloaded the latest version of shiny download from github shiny dev: devtools::install_github("rstudio/shiny").
With the current version (at least the version current this morning), I had the problem mentioned above.
Just take into account that the error appears after a full cycle when you click on the checkbox.
Following the last example you posted, this means that

  • renders fine the first time
  • gives error (message above) as soon as you click the checkbox.
    It may be a shiny regression...

@jrowen
Copy link

jrowen commented Mar 4, 2015

Everything appears to be working with the released version of shiny, so I'd suggest sticking with that one right now. It might be worth pointing this error to the shiny devs. If the error is expected, they may have a suggestion for upgrading existing libraries.

@smartinsightsfromdata
Copy link

@jrowen Your suggestion was spot on: I reported it to the shiny guys. Here is their comment rstudio/shiny#752

@smartinsightsfromdata
Copy link

@jrowen On the other hand the change Wch suggests seems to cause other issues. Have you tried it on the update-context-menu branch? (thanks in advance)

@jrowen
Copy link

jrowen commented Mar 5, 2015

I too ran into another error after making the suggested change. It sounds
like he is going to re-add support for the current implementation.

On Wed, Mar 4, 2015 at 1:24 PM Enzo [email protected] wrote:

@jrowen https://github.com/jrowen On the other hand the change Wch
suggests seems to cause other issues. Have you tried it on the
update-context-menu branch? (thanks in advance)


Reply to this email directly or view it on GitHub
#18 (comment)
.

@smartinsightsfromdata
Copy link

@jrowen Would you kindly contact me on my email please < enzo at smartinsightsfromdata doc com >? Thanks

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

3 participants