r - Shiny generate random values every time press button -


i new shiny , simple shiny application, generates iid normal variables , prints histogram. input has:

  • 2 numericinput fields, mu , sigma
  • actionbutton

output tabsetpanel:

  • tab1: 5 generated values
  • tab2: histogram

so not work. tried lot variants , insufficiently working solution this.

here code ui.r

library(shiny) library(xtable)  meaninput1 <- numericinput("id1", "$$mean \\ of \\ x_1$$", 1, min = -10, max = 10, step = 1) meaninput2 <- numericinput("id2", "$$sd \\ of \\ x_2$$", 1, min = -10, max = 10, step = 1) tabpanel1 <- tabpanel("generated values", tableoutput("table1")) tabpanel2 <- tabpanel("plot", plotoutput("plot1"))  shinyui(fluidpage(     withmathjax(),     titlepanel("title"),      sidebarlayout(         fluid=true,          sidebarpanel(             meaninput1,             meaninput2,             actionbutton("gobutton", "go!")         ),     mainpanel(          tabsetpanel(             tabpanel1,             tabpanel2         )     ) ))) 

here code server.r

shinyserver(     function(input, output) {          output$plot1 <- renderplot({             if (input$gobutton >= 1){                 sigma <- input$id2                 muinput <- input$id1                  table <- as.data.frame(rnorm(n = 5,muinput,sd = sigma))                 names(table) <- "x"                  output$plot1 <- renderplot(hist(table));                 output$table1 <- rendertable(head(table));             }         })     } ) 

questions:

  • it works if buttonclicked once , tabpanel2 selected. how make work every time click button.

you want use eventreactive in case. can find demos using actionbutton here. have strange structure code render statements inside of render statements.

if create eventreactive function , separate out rendertable , renderplot calls clearer , works correctly. practice not name variables same functions changed table variables my_table clarity.

shinyserver(   function(input, output) {      rand <- eventreactive(input$gobutton,{       sigma <- input$id2       muinput <- input$id1        my_table <- as.data.frame(rnorm(n = 5,muinput,sd = sigma))       names(my_table) <- "x"       return(my_table)     })      output$plot1 <- renderplot({       my_table <- rand()       if(is.null(my_table)){         return(null)       }else{         hist(head(my_table$x));       }     })       output$table1 <- rendertable({       my_table <- rand()        if(is.null(my_table)){         return(null)       }else{         my_table       }     });   } ) 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -