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 = "\t"),
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?
Here's a variant of server.R; use it with akrun's ui.R. This one dynamically adapts the filtering choices, and allows you to add columns back by adding their column name even after clicking the button.
library(shiny)
library(DT)
server <- function(session, input, output) {
data <- reactive({
req(input$dataset)
read.csv(input$dataset$datapath, header = input$header,sep = input$sep)
})
filtereddata <- eventReactive({
input$update
data()
}, {
req(data())
if(is.null(input$select) || input$select == "")
data() else
data()[, colnames(data()) %in% input$select]
})
observeEvent(data(), {
updateSelectInput(session, "select", choices=colnames(data()))
})
output$mytable <- renderDataTable(filtereddata())
}
We could modify it
library(shiny)
library(DT)
-ui
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 = "\t"),
selected = ","),
selectInput("select", "Select columns to display", c('col1', 'col2'), 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'),
#tableOutput("mytable")
DT::dataTableOutput("mytable")
)
)
)
-server
server <- function(input, output) {
values <- reactiveValues(df_data = NULL)
observeEvent(input$dataset, {
values$df_data <- read.csv(input$dataset$datapath,
header = input$header,
sep = input$sep
)}
)
observeEvent(input$update, {
temp <- values$df_data[input$select]
values$df_data <- temp
})
output$mytable <- renderDataTable(values$df_data)
}
-Run the application
shinyApp(ui = ui, server = server)
-output

-after updating

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With