Creating multiple numeric input according to the variables of an uploaded dataset
Clash Royale CLAN TAG#URR8PPP
Creating multiple numeric input according to the variables of an uploaded dataset
I am working on a Shiny app that allows the user to upload their own data and analyze them. At a certain point, I'd like to allow the user to introduce new data as numeric input and to build a new table including them.
I'd like my app to do it dynamically, i.e. creating a box in the sidebar panel containing new numeric input accordingly and with the name of the variables of my uploaded dataset.
I can do it by indicating a priori specific variables, but I'd like to make it dynamic.
I'd be really glad if somebody could attend to this matter.
Here's included a simple example of my code and a screenshot showing how it looks like (but with a priori specified variables).
library(shiny)
ui <- fluidPage(
tabPanel("New Cases", icon=icon("edit"),
sidebarLayout(
sidebarPanel(width=3, #sidebarPanel "New Cases"
conditionalPanel(
'input.dataset02 === "Edit Table"',
textInput('NewID', HTML('<h5><b>Enter Name</b></h5>')), #Enter Factor?
numericInput('NewVal1', HTML('<h5><b>Enter Item</b></h5>'), NULL),
br(),
fluidRow(
column(2, HTML('<h5><b>E14</b></h5>')),
column(4, numericInput("NewVal3", label = NULL, value = NULL)),
column(2, HTML('<h5><b>E16</b></h5>')),
column(4, numericInput("NewVal4", label = NULL, value = NULL))
),
fluidRow(
column(2, HTML('<h5><b>E18_1</b></h5>')),
column(4, numericInput("NewVal5", label = NULL, value = NULL)),
column(2, HTML('<h5><b>E18</b></h5>')),
column(4, numericInput("NewVal6", label = NULL, value = NULL))
),
fluidRow(
column(2, HTML('<h5><b>FAEE</b></h5>')),
column(4, numericInput("NewVal7", label = NULL, value = NULL)),
column(2, HTML('<h5><b>EtG</b></h5>')),
column(4, numericInput("NewVal8", label = NULL, value = NULL))
),
br(),
actionButton("goButton", "Update Table",icon("cloud-upload"),
style="width: 100%; height: 60px; color: steelblue; background-color: #337ab7; border-color: #2e6da4"),
br()
)),
mainPanel(
tabsetPanel(
id = 'dataset02',
tabPanel("Edit Table",
br(),
dataTableOutput("table3"))
))
)))
server <- function(input, output)
mydata3 = data.frame(Name=letters[NULL], Item=sample(NULL),Piece=sample(NULL), E14=sample(NULL), E16=sample(NULL),
E18_1=sample(NULL), E18=sample(NULL), FAEE=sample(NULL), ETG=sample(NULL))
output$table3 <- renderDataTable( df3())
df3 <- eventReactive(input$goButton,
if(input$NewID!=" " && !is.null(input$NewVal1)
&& !is.null(input$NewVal3) && !is.null(input$NewVal4) && !is.null(input$NewVal5)
&& !is.null(input$NewVal6) && !is.null(input$NewVal7) && !is.null(input$NewVal8)
&& input$goButton>0)
newrow = data.frame(
Name = input$NewID,
Item = input$NewVal1,
Piece = 1,
E14 = input$NewVal3,
E16 = input$NewVal4,
E18_1 = input$NewVal5,
E18 = input$NewVal6,
FAEE = input$NewVal7,
ETG = input$NewVal8)
mydata3 <<- rbind(mydata3, newrow)
mydata3
, ignoreNULL = FALSE)
shinyApp(ui = ui, server = server)
1 Answer
1
You can create inputs dynamically with renderUI
and uiOutput
.
renderUI
uiOutput
Example:
# LIBRARIES & SOURCING --------------
library(shiny)
library(shinydashboard)
# UI -----------------
ui <- dashboardPage(title="App Title",
dashboardHeader(disable = TRUE),
dashboardSidebar(disable = TRUE),
dashboardBody(
fluidRow(
uiOutput("uiOut1")
)
))
# SERVER -----------------
server <- function(input, output)
output$uiOut1 = renderUI(
list(
numericInput("dynInput1", "First dynamic input:", 20, 0, 40),
numericInput("dynInput2", "Second dynamic input:", 20, 0, 40)
)
)
shinyApp(ui = ui, server = server)
You can then refer to the inputs with input$dynInput1
or input[["dynInput1"]]
if you need to use a variable to select which input you want.
input$dynInput1
input[["dynInput1"]]
More info here: https://shiny.rstudio.com/articles/dynamic-ui.html
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.