Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

r - Shiny How to dynamically select columns of imported dataset for further analysis

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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())

} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...