Reputation: 313
Below I have an R Shiny code to upload an input file to the application. All csv files that'll be uploaded will have a column 'carname'. I want to have a dropdown that's able to access and select choices in that column. How do I access the uploaded file in the ui?
Error: Error in data$carname : object of type 'closure' is not subsettable
ui <- fluidPage(
headerPanel("Webpapp"),
sidebarPanel(
fileInput(inputId = "filedata",
label = "Upload the Raw Data File",
accept = c("text/csv", "text/comma-separated-values,text/plain",
".csv")),
selectInput("cars", "Select car:", choices = data$carname, selected = "Nissan Sentra"),
),
mainPanel(
plotOutput('plot'),
)
)
# Define server code ####
server <- function(input, output) {
data <- reactive({
req(input$filedata)
read.csv(input$filedata$datapath, skip = 15, header = F)
})
output$plot <- renderPlot({
if (is.null(data())) {
return(NULL)
}
plot_probe(input$cars) #Plotting function in global
})
}
shinyApp(ui = ui, server = server)
Upvotes: 0
Views: 1451
Reputation: 1453
This is not necessary and also I would say not recommend in this case to use renderUI
. updateSelectInput()
will be faster (Make R Shiny Dashboards Faster with updateInput, CSS, and JavaScript) and require less rewriting of code:
library(shiny)
ui <- fluidPage(
headerPanel("Webpapp"),
sidebarPanel(
fileInput(inputId = "filedata",
label = "Upload the Raw Data File",
accept = c("text/csv", "text/comma-separated-values,text/plain",
".csv")),
selectInput("cars", "Select car:", choices = "", selected = "Nissan Sentra"),
),
mainPanel(
plotOutput('plot'),
)
)
# Define server code ####
server <- function(session, input, output) {
data <- reactive({
req(input$filedata)
read.csv(input$filedata$datapath, skip = 15, header = F)
})
observe({
req(data())
updateSelectInput(session, "cars", choices = data()$carname)
})
output$plot <- renderPlot({
if (is.null(data())) {
return(NULL)
}
plot_probe(input$cars) #Plotting function in global
})
}
shinyApp(ui = ui, server = server)
What I have changed:
""
to the choice
argument in selectInput
, because as @gdevaux mentioned, you cannot use in this way data from server
in UI
.session
parameter to server
function - it is necessary for update*
functions.updateSelectInput()
inside observer
- it will update selectInput
if data()
won't be NULL
.choices = data$carname
into choices = data()$carname
. It is because data
as a reactive
you need to use as a function.Upvotes: 2
Reputation: 2505
You cannot access the uploaded file from the UI.
But you can create an UI element from the server, and send it to the UI with the functions renderUI
and UIOutput
.
ui <- fluidPage(
headerPanel("Webpapp"),
sidebarPanel(
fileInput(inputId = "filedata",
label = "Upload the Raw Data File",
accept = c("text/csv", "text/comma-separated-values,text/plain",
".csv")),
uiOutput("ui_select")
),
mainPanel(
plotOutput('plot'),
)
)
# Define server code ####
server <- function(input, output) {
data <- reactive({
req(input$filedata)
read.csv(input$filedata$datapath, skip = 15, header = F)
})
# UI element for select input
output$ui_select <- renderUI({
req(data())
selectInput("cars", "Select car:", choices = data()$carname, selected = "Nissan Sentra")
})
output$plot <- renderPlot({
if (is.null(data())) {
return(NULL)
}
plot_probe(input$cars) #Plotting function in global
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1