0

I Am creating a dynamic update in R shiny with the following code using the iris dataset

 write.csv(iris, file = "iris.csv", row.names = F)
# to create a local version of iris dataset

# next create UI


  ui <- fluidPage(


   fileInput("file", "Browse",
        accept = c("text/csv",
                   "text/comma-separated-values,text/plain",
                   ".csv")
    ),
   selectInput(inputId = "Speciesname", label = "Name",choices = 
    NULL,selected = NULL),
   tableOutput("data")
   )


   #Create server
    server <- function(input, output,session) {



 df = reactive({
 req(input$file)
 return(read.csv(input$file$datapath))
 })

  observe({
  choices1 = unique(df()$Species)
  updateSelectInput(session,"Speciesname", choices =  choices1)
  })
  output$data <- renderTable({
  req(input$Speciesname)
  df[as.character(input$Speciesname), ]
  }, )
   }


 #Run app
 shinyApp(ui, server)

I am able to read the file in. The subsetting however is showing the following error and the shiny app

 Warning: Error in [: object of type 'closure' is not subsettable
 [No stack trace available]

I am unable to understand or sort this error out. The code runs when I dont use a local copy of the dataset but use the built in R iris dataset. I request someone to guide me here

2
  • 1
    it should be df(), as in df()[as.character(input$Speciesname), ] Commented Oct 9, 2018 at 9:38
  • Thank you Sir. I have made the correction but now I am only getting NA values in all rows of the output. The colun headings appear Commented Oct 9, 2018 at 9:40

1 Answer 1

2

This should do the job

library(shiny)
write.csv(iris, file = "iris.csv", row.names = F)
# to create a local version of iris dataset

# next create UI

ui <- fluidPage(


  fileInput("file", "Browse",
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv")),
  selectInput(inputId = "Speciesname", label = "Name",choices = 
                NULL,selected = NULL),
  tableOutput("data")
)


#Create server
server <- function(input, output,session) {

  df = reactive({
    req(input$file)
    return(read.csv(input$file$datapath))
  })

  observe({
    choices1 = unique(df()$Species)
    updateSelectInput(session,"Speciesname", choices =  choices1)
  })

  output$data <- renderTable({
    req(input$Speciesname)
    df()[df()$Species %in% input$Speciesname,]
  })
}


#Run app
shinyApp(ui, server)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Sir. Is the %in% operator essential
Yes, its a better version of == it will take care of multiple matches, if any. For example if you have 2 species selected and want them to be displayed it easier to have this operator instead of writing == twice
Lovely. Thank You Sir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.