[英]Error in R shiny subsetting dataframe based on CSV file
我正在使用虹膜數據集使用以下代碼在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)
我可以讀取文件。但是子設置顯示以下錯誤和閃亮的應用程序
Warning: Error in [: object of type 'closure' is not subsettable
[No stack trace available]
我無法理解或解決此錯誤。 當我不使用數據集的本地副本但使用內置的R iris數據集時,代碼將運行。 我要求有人在這里指導我
這應該做的工作
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.