i've been trying find solution how add , remove input fields button in shiny. don't have source code since haven't made progress, jquery example (http://www.mkyong.com/jquery/how-to-add-remove-textbox-dynamically-with-jquery/) gives idea on i'm trying accomplish. possible in shiny or should use shinyjs this? thank in advance!
edit: read jquery example bit more, , added code snippet doing think looking for.
i don't know jquery, couldn't make out of example link. took guess on wanted, think key idea use of renderui , uioutput if suggestion here misses point.
to toggle ui element:
if don't want use shinyjs, this:
library(shiny) ui <- shinyui(fluidpage( actionbutton("btn", "toggle textbox"), textoutput("btn_val"), uioutput("textbox_ui") )) server <- shinyserver(function(input, output, session) { output$btn_val <- renderprint(print(input$btn)) textboxtoggle <- reactive({ if (input$btn %% 2 == 1) { textinput("textin", "write something:", value = "hello world!") } }) output$textbox_ui <- renderui({ textboxtoggle() }) }) shinyapp(ui, server) add , remove elements:
after reading bit of jquery example, think similar looking for:
library(shiny) ui <- shinyui(fluidpage( sidebarpanel( actionbutton("add_btn", "add textbox"), actionbutton("rm_btn", "remove textbox"), textoutput("counter") ), mainpanel(uioutput("textbox_ui")) )) server <- shinyserver(function(input, output, session) { # track number of input boxes render counter <- reactivevalues(n = 0) observeevent(input$add_btn, {counter$n <- counter$n + 1}) observeevent(input$rm_btn, { if (counter$n > 0) counter$n <- counter$n - 1 }) output$counter <- renderprint(print(counter$n)) textboxes <- reactive({ n <- counter$n if (n > 0) { lapply(seq_len(n), function(i) { textinput(inputid = paste0("textin", i), label = paste0("textbox", i), value = "hello world!") }) } }) output$textbox_ui <- renderui({ textboxes() }) }) shinyapp(ui, server) the problem approach each time press add or remove button, of input boxes re-rendered. means input might have had on them disappears.
i think around saving current input values of input boxes reactivevalues object, , setting values object starting values of re-rendered input boxes using value option in textinput. i'll leave implementation of now, though.
Comments
Post a Comment