r - Shiny - how to send updated data with renderUI and eventReactive? -
i want send form data server only when submit button clicked, use eventreactive
method. render form elements using renderui
method. lastly, use observe
method observe changes in form elements - if of radio buttons in plot 2 clicked, update , deselect radio buttons in plot 1, , vice versa.
so when click submit button, expect data plot 1 null
, data server side still a value. below test code.
ui.r
library(shiny) # define ui application draws histogram shinyui(fluidpage( # application title titlepanel("hello shiny!"), # sidebar slider input number of bins sidebarlayout( sidebarpanel( uioutput("plot1ui"), uioutput("plot2ui"), actionbutton('goplot', 'enter') ), # show plot of generated distribution mainpanel( plotoutput("plot") ) ) ))
server.r
library(shiny) # define server logic required draw histogram shinyserver(function(input, output, session) { output$plot1ui <- renderui({ radiobuttons( inputid = "plot1", label = "plot 1:", choices = c( "option 1" = "1", "option 2" = "2", "option 3" = "3" ), selected = null, inline = false ) }) output$plot2ui <- renderui({ radiobuttons( inputid = "plot2", label = "plot 2:", choices = c( "option a" = "a", "option b" = "b", "option c" = "c" ), selected = character(0), inline = false ) }) observe({ plot1 <- input$plot1 if (!is.null(plot1) && plot1 != '1') { updateradiobuttons( session, "plot2", label = "plot 2:", choices = c( "option a" = "a", "option b" = "b", "option c" = "c" ), selected = character(0), inline = false ) } }) observe({ plot2 <- input$plot2 if (!is.null(plot2)) { updateradiobuttons( session, "plot1", label = "plot 1:", choices = c( "option 1" = "1", "option 2" = "2", "option 3" = "3" ), selected = character(0), inline = false ) } }) # call when button pressed. eventplot <- eventreactive(input$goplot, { cat('\n') cat('plot 1:') str(input$plot1) cat('\n') cat('plot 2:') str(input$plot2) }) output$plot <- renderplot({ # render plot eventreactive. eventplot() }) })
what have done wrong? how can send data described above?
using updateradiobuttons()
unfortunately updates radio button in ui without affecting actual input$
value. set input$
value null
can use shiny.addcustommessagehandler
.
to can add script ui.r
tags$script(" shiny.addcustommessagehandler('resetvalue', function(variablename) { shiny.oninputchange(variablename, null); }); ")
and utilize message handler while updating radio buttons in server.r
session$sendcustommessage(type = "resetvalue", message = "inputid")
below full implementations of think address question. additionally converted observe
s observeevent
s since have specific event they're reacting to.
ui.r
library(shiny) # define ui application draws histogram shinyui(fluidpage( tags$script(" shiny.addcustommessagehandler('resetvalue', function(variablename) { shiny.oninputchange(variablename, null); }); "), # application title titlepanel("hello shiny!"), # sidebar slider input number of bins sidebarlayout( sidebarpanel( uioutput("plot1ui"), uioutput("plot2ui"), actionbutton('goplot', 'enter') ), # show plot of generated distribution mainpanel( plotoutput("plot") ) ) ))
server.r
library(shiny) # define server logic required draw histogram shinyserver(function(input, output, session) { output$plot1ui <- renderui({ radiobuttons( inputid = "plot1", label = "plot 1:", choices = c( "option 1" = "1", "option 2" = "2", "option 3" = "3" ), selected = character(0), inline = false ) }) output$plot2ui <- renderui({ radiobuttons( inputid = "plot2", label = "plot 2:", choices = c( "option a" = "a", "option b" = "b", "option c" = "c" ), selected = character(0), inline = false ) }) observeevent(input$plot1, { updateradiobuttons(session, inputid = "plot2", label = "plot 2:", choices = c( "option a" = "a", "option b" = "b", "option c" = "c" ), selected = character(0), inline = false) session$sendcustommessage(type = "resetvalue", message = "plot2") }) observeevent(input$plot2, { updateradiobuttons(session,inputid = "plot1", label = "plot 1:", choices = c( "option 1" = "1", "option 2" = "2", "option 3" = "3" ), selected = character(0), inline = false) session$sendcustommessage(type = "resetvalue", message = "plot1") }) # call when button pressed. eventplot <- eventreactive(input$goplot, { cat('\n') cat('plot 1:') str(input$plot1) cat('\n') cat('plot 2:') str(input$plot2) plot(rnorm(100)) }) output$plot <- renderplot({ # render plot eventreactive. eventplot() }) })
Comments
Post a Comment