Hi all.
I'm wondering if there's a way of creating an arbitrary number of textinput
fields in shiny.
Right now I can see a way of creating a fixed number of textinput
fields. (In the example below I made 3.)
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("Get Names"),
fluidRow(
# new column so that the table doesn't take the full page
column("",
width = 10, offset = 1,
tags$h3("Text Box Area"),
panel(
textInput("name1", "Name 1"),
textInput("name2", "Name 2"),
textInput("name3", "Name 3"),
actionButton("getNames", "Use these names")
),
tags$h3("Table Area"),
# Show a table of the players
dataTableOutput("table")
)
)
)
# Define server logic
server <- function(input, output) {
names_df <- eventReactive(input$getNames, {
tibble(
ID = 1:3,
Name = c(input$name1, input$name2, input$name3)
)
})
output$table <- renderDT({
names_df()
})
}
# Run the application
shinyApp(ui = ui, server = server)
But I'm wondering how it would work to read text boxes from a numeric source (slider maybe?), then have an action button to a user-specified number of textinput
fields based on a the numeric source.
I have a guess for how it should maybe look, but I'm okay with being totally wrong / taking an entirely new approach.
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("Get Names"),
fluidRow(
# new column
column("",
width = 10, offset = 1,
tags$h3("Select Area"),
# get player count
panel(
sliderInput("nNames", "Number of People", 5, 20, 9),
actionButton("getNamesNumber", "Set Number of People")
),
# set player names
panel(
# I NEED HELP HERE 3rd
# i have no idea how i would get a multiple text box input
# THANKS
actionButton("getNames", "Set Names")
),
tags$h3("Table Area"),
# Show a table of the players
dataTableOutput("table")
)
)
)
# Define server logic
server <- function(input, output) {
build_text_fields <- eventReactive(input$nNames, {
# I NEED HELP HERE 1ST
# some sort of do.call command combined with inputtext
# that also uses the nNames input above? I'm really lost
# THANKS
})
output$text_fields <- ### I NEED HELP HERE 2ND
# I don't what sort of field this should be.
# My best guess is
renderSomething({ build_text_fields() })
# but i don't know what something i should render
# THANKS
names_df <- eventReactive(input$getNames, {
tibble(
ID = 1:input$nNames,
Name = input$nameList
)
})
output$table <- renderDT({
names_df()
})
}
# Run the application
shinyApp(ui = ui, server = server)
Thanks for looking at my weird request.