r - Add columns to a reactive data frame in Shiny and update them -


i able calculate new column of data based on dividing 1 column another, both original columns selected user input. have calculated data joined original table (or copy of it).

i have managed figure out how make dataframe reacts column input selection, , have managed make calculation divides 1 column other, have not been able make final dataframe includes of original columns new calculated one.

here mock have made using built in iris data. displays data columns selected in first table, , calculation in second table (you need scroll down quite far see this).

how can join calculated data original source?

many thanks

#ui         pagewithsidebar(       headerpanel('calculate column'),       sidebarpanel(          #select variables iris dataset         selectinput('xcol', 'x variable', names(iris)),         selectinput('ycol', 'y variable', names(iris),                     selected=names(iris)[[2]])       ),       mainpanel(         #display selected variables             tableoutput("view"),          #display calculated variable             tableoutput("view2")       )     )   #server         function(input, output, session) {        # combine selected input variables new data frame       selecteddata <- reactive({         iris[, c(input$xcol, input$ycol),]       })         # divide 1 variable selection other       selecteddata2 <- reactive({                 iris$new<-iris[, c(input$xcol)]/iris[, c(input$ycol)]          })        # create data output selected variables       output$view <- rendertable({selecteddata()       })        # create data output calculated variable       output$view2 <- rendertable({selecteddata2()       })      } 

you forget iris not reactive element, code can't work. have 2 options here:

  • creating reactive value store data frame, using reactive().
  • creating list of "global" reactive values in can store updated data frame, using reactivevalues().

using global reactive expressions reactivevalues

using reactivevalues can make list of reactive expressions input , output are. in example below use store data frame iris globals$mydf. can use eg observe change value reactively, in following server function:

#server server <- function(input, output, session) {    globals <- reactivevalues(     mydf = iris   )    observe({     thedf <- globals$mydf     newvar <- thedf[[input$xcol]] / thedf[[input$ycol]]     globals$mydf$ratio <- newvar   })     # create data output selected variables   output$view <- rendertable({     globals$mydf   }) } 

using reactive()

you can make 2 reactive expressions depend on eachother:

  • one selects variables , calculates ratio
  • one combines result selected variables

your server :

server <- function(input, output, session) {    newdf <- reactive({     cbind(       iris[c(input$xcol, input$ycol)],       ratio = newvar()     )   })    newvar <- reactive({     iris[[input$xcol]] / iris[[input$ycol]]   })    # create data output selected variables   output$view <- rendertable({     newdf()   })  } 

although beliefe not you're looking for, can use newdf() in other code use globals$mydf in previous example. reactivevalues() pays off if different parts of code have able change data frame.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -