简体   繁体   English

基于CSV文件的R闪亮子集数据帧中的错误

[英]Error in R shiny subsetting dataframe based on CSV file

I Am creating a dynamic update in R shiny with the following code using the iris dataset 我正在使用虹膜数据集使用以下代码在R 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[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. 当我不使用数据集的本地副本但使用内置的R iris数据集时,代码将运行。 I request someone to guide me here 我要求有人在这里指导我

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM