r - How to populate variables after clicking an action button? -
i'm trying build simple roller 1 can click button , populate series of variables. i'm sure easy solution, i'm having hard time getting work.
this i've got. have interface set want it, want new value strength row.
library(shiny) ui = fluidpage( titlepanel(""), sidebarlayout( sidebarpanel( textinput("char_name","name"), textinput("char_sex","sex"), actionbutton("rollbutton", "roll!", width = "100%"), hr(), helptext("please consult _ if need assitance.") ), mainpanel( htmloutput("name"), htmloutput("sex"), htmloutput("natl"), htmloutput("strength") ) ) ) server = function(input, output) { observe({ if(input$rollbutton > 0) { strength <- sum(sample(1:6,3,replace=true)) } }) output$name <- rendertext({ input$rollbutton isolate(paste0('<b>name</b>: ', input$char_name)) }) output$sex <- rendertext({ input$rollbutton isolate(paste0('<b>sex</b>: ', input$char_sex)) }) output$strength <- rendertext({ input$rollbutton isolate(paste0('<b>strength</b>: ', strength)) }) } shinyapp(ui = ui, server = server)
you can't read strength variable because set in function. can create vector of shared reactive values
server = function(input, output) { val <- reactivevalues(strength=null) observe({ if(input$rollbutton > 0) { val$strength <- sum(sample(1:6,3,replace=true)) } }) output$name <- rendertext({ input$rollbutton isolate(paste0('<b>name</b>: ', input$char_name)) }) output$sex <- rendertext({ input$rollbutton isolate(paste0('<b>sex</b>: ', input$char_sex)) }) output$strength <- rendertext({ input$rollbutton isolate(paste0('<b>strength</b>: ', val$strength)) }) } shinyapp(ui = ui, server = server)
Comments
Post a Comment