r - 基于 CSV 文件的 R 闪亮子集数据帧中的错误
问题描述
我正在使用 iris 数据集使用以下代码在 R 闪亮中创建动态更新
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)
推荐阅读
- html - 在 Django 中的所有页面上处理 LoginView 登录的最佳方法
- html - 抓取网站特定部分的问题
- python - 为什么在成员变量上使用海象运算符会引发 SyntaxError?
- amazon-web-services - 从 AWS API Gateway 请求访问浏览器 UBID
- css - 无论是否激活暗模式,都已激活偏好颜色方案
- python - Python 命令行目录
- javascript - 是的,架构电子邮件使用 formik 重置密码字段验证
- javascript - 如何将值从异步响应数据推送到数组?
- c# - WPF命令绑定代码隐藏问题
- c++ - 我的合并排序实现中的错误在哪里?