I am trying a small shiny app wherein I load a CSV file from the local directory and then select specific columns from the dataframe and use this subsetted dataframe for further data analysis.
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
fileInput("dataset", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Include clarifying text ----
#helpText(em("Note: This app requires file in csv format only!!")),
helpText(em("Note:Select all the inputs and click on button as given below to exectute the app")),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = ""),
selected = ","),
selectInput("select", "Select columns to display", names(datasetInput), multiple = TRUE),
actionButton("update", "Update Data set", class = "btn-primary",style='padding:4px; font-size:120%')
),
# Show a plot of the generated distribution
mainPanel(
h2('The Mydata'),
dataTableOutput('mytable')
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
datasetInput <- eventReactive(input$update, {
validate(need(input$dataset != "", "Please select a data set in csv format only!!!"))# custom error message on opening the app
read.csv(input$dataset$datapath,
header = input$header,
sep = input$sep)
}, ignoreNULL = FALSE)
dataset <- reactive({
df_input<-datasetInput()
df_input$x<-NULL
df_input
})
output$mytable = renderDataTable({
columns = names(dataset)
if (!is.null(input$select)) {
columns = input$select
}
dataset[,columns,drop=FALSE]
})
}
# Run the application
shinyApp(ui = ui, server = server)
Looking in some SO answers;got a few; one as given below:
shiny allowling users to choose which columns to display
But this answer, the dataset is predefined; I want to the user to download his own dataset.
I am getting the following error:
Error in lapply(obj, function(val) { : object 'datasetInput' not found
I think somewhere I have to use an observeEvent function?
See Question&Answers more detail:
os