Re: [shinyapps-users] Creating non reactive instances of reactive varaibles

398 views
Skip to first unread message

Tareef Kawaf

unread,
May 23, 2016, 8:55:14 AM5/23/16
to Michael, shiny-...@googlegroups.com
This feels more like a shiny question so I am moving it to shiny-discuss, I am guessing that you are looking for is a way to pick off the data using something like the isolate() call.

On Sun, May 22, 2016 at 7:07 PM, Michael <michaela...@gmail.com> wrote:
I am not sure whether this is possible or not.
For example say a user enters some number into a reactive function they hit a button which multiplies every cell.
Then when they like those numbers they can hit another button which will send those particular values to a data frame (this data frame being completely non reactive).
In short I am asking, can you make reactive values non reactive? To me this seems like something pretty important.

Thanks,

Michael

--
You received this message because you are subscribed to the Google Groups "ShinyApps Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shinyapps-use...@googlegroups.com.
To post to this group, send email to shinyap...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shinyapps-users/6bfcea2c-300d-4e62-9bdf-67ed5328401e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael

unread,
May 24, 2016, 6:51:00 PM5/24/16
to Shiny - Web Framework for R, michaela...@gmail.com
I am not sure whether this is possible or not.
For example say a user enters some number into a reactive function they hit a button which multiplies every cell.
Then when they like those numbers they can hit another button which will send those particular values to a data frame (this data frame being completely non reactive).
In short I am asking, can you make reactive values non reactive? To me this seems like something pretty important.
Here is my code in which I am trying to make tabplot become an unreactive copy. 

library(shiny)
library(rhandsontable)

seq1 <- seq(1:6)
mat1 <- matrix(seq1, 2)

tabplot<-data.frame(car=numeric(2),num=numeric(2),truck=numeric(2))

did_recalc <- FALSE

ui <- fluidPage(
  rHandsontableOutput('table'),
  tableOutput('result'),
  textOutput('ca'),
  actionButton("goButton","Confirm"),
  verbatimTextOutput("otest"),
  actionButton("checkButton","Apply"),
  br(),
  actionButton("recalc", "Return to original values")
  
)

server <- function(input,output,session)({
  
 # tabplot<-data.frame(car=numeric(2),num=numeric(2),truck=numeric(2))

  seq1 <- seq(1:6)
 # mat1 <- matrix(seq1, 2)
  
  mat1<- data.frame(x=1:2,y=3:4,z=5:6)

  #creates reactive values for the data frame
  #obviously they have to be reactive values to function with the rhandsontable which is being continuously updated
  #as the documentation says "values taken from the reactiveValues object are reactive but the object itself is not
  values <- reactiveValues(data=mat1)
  
  #if recalc --- which connects to an action button in the ui is hit, values goes back to original data frame
  observe({
    input$recalc
    values$data<-mat1
  })
  
  #Where the magic happens
  output$table <- renderRHandsontable({
    rhandsontable(values$data,selectCallback = TRUE)
  })
  
  #this changes the handsontable format to an r object
  observe({
    if(!is.null(input$table))
      values$data <-hot_to_r(input$table)
  })
  
  #Here we create a reactive function that creates a data frame of the rhandsontable output but it is a reactive function
  fn<-reactive({
    co<-data.frame((values$data))                                
    return(co)
  })
  
  #Bit of testing, this demonstrates that the fn() is only updated after the button is pressed
  output$result<-renderTable({
    input$goButton
    isolate({
      fn()
    })
  })   
  
  #observe({
 #   if(!is.null(input$table))
 #     values$data <-hot_to_r(input$table)
 # })
 
 # observe({
   # if(did_recalc = FALSE)
#  tabplot<-reactive({                              #Format co[desired row:length(colums)][desired column] 

  #observeEvent(input$checkButton,{
  
    isolate({ 
      tabplot[1,1:3][1]<-fn()[1,1:3][1]
      tabplot[1,1:3][2]<-fn()[1,1:3][2]
      tabplot[1,1:3][3]<-fn()[1,1:3][3]
      
      tabplot[2,1:3][1]<-fn()[2,1:3][1]
      tabplot[2,1:3][2]<-fn()[2,1:3][2]
      tabplot[2,1:3][3]<-fn()[2,1:3][3]

    })

  observe({
    input$checkButton
    output$ca<-renderText({
      tabplot$car 
      cat('\nAccessing Subset with $:', tabplot$car)
      cat('\nAccessing specific cell:',tabplot[1,3])
      cat('\nAccessing specific cell:',tabplot[[1]])
      cat('\noperations on specific cell:',tabplot[1,3]*2)
    })
  })
  
})
shinyApp(ui = ui, server = server)
Reply all
Reply to author
Forward
0 new messages