【发布时间】:2014-06-16 09:26:11
【问题描述】:
我正在使用以下代码,但我总是得到这个子集错误。 我在设置什么子集,我错在哪里。 这应该是我修改过的一些基本入口代码 在某些时候工作,我看不到错误。
谢谢
服务器.R
library(shiny)
# Define a server for the Shiny app
shinyServer(function(input, output) {
# Filter data based on selections
output$table <- renderDataTable({
data <- read.table("my.csv", sep =',', header =TRUE)
if (input$shortdesc != "All"){
data <- data[data$ShortDescription == input$shortdesc,]
}
if (input$taken != "All"){
data <- data[data$Taken == input$taken,]
}
if (input$location != "All"){
data <- data[data$Location == input$location,]
}
data
})
})
ui.R
library(shiny)
# Define the overall UI
shinyUI(
fluidPage(
titlePanel("My Items"),
# Create a new Row in the UI for selectInputs
fluidRow(
column(4,
selectInput("man",
"What:",
c("All",
unique(as.character(data$ShortDescription))))
),
column(4,
selectInput("trans",
"Where:",
c("All",
unique(as.character(data$Location))))
),
column(4,
selectInput("cyl",
"Who:",
c("All",
unique(as.character(data$Taken))))
)
),
# Create a new row for the table.
fluidRow(
dataTableOutput(outputId="table")
)
)
)
更新:
为什么示例(见下文)有效,而当我将其更改为 my.csv 时它会中断? 如果“数据”是一个内置函数,那不会与下面的示例发生冲突吗? 很抱歉不明白,但这让我很困惑。
服务器.R
library(shiny)
# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)
# Define a server for the Shiny app
shinyServer(function(input, output) {
# Filter data based on selections
output$table <- renderDataTable({
data <- mpg
if (input$man != "All"){
data <- data[data$manufacturer == input$man,]
}
if (input$cyl != "All"){
data <- data[data$cyl == input$cyl,]
}
if (input$trans != "All"){
data <- data[data$trans == input$trans,]
}
data
})
})
ui.R.
library(shiny)
# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)
# Define the overall UI
shinyUI(
fluidPage(
titlePanel("Basic DataTable"),
# Create a new Row in the UI for selectInputs
fluidRow(
column(4,
selectInput("man",
"Manufacturer:",
c("All",
unique(as.character(mpg$manufacturer))))
),
column(4,
selectInput("trans",
"Transmission:",
c("All",
unique(as.character(mpg$trans))))
),
column(4,
selectInput("cyl",
"Cylinders:",
c("All",
unique(as.character(mpg$cyl))))
)
),
# Create a new row for the table.
fluidRow(
dataTableOutput(outputId="table")
)
)
)
【问题讨论】:
-
你能包含完整的错误信息吗?通常有关于行号的提示...
-
监听127.0.0.1:4427 数据$ShortDescription 错误:“闭包”类型的对象不是子集
-
在基础 R 中有一个名为
data的函数。因此,如果 R 在当前环境中找不到data对象,它会假定您指的是该函数并告诉您它无法对其进行子集化。 -
嗯。不知道我是否理解你。 “base R”是指“Server.R”,还是建议我不应该使用“data”作为变量名?感谢您的回复。
-
“base R”是指包
base或更一般地说是所有在R中自动加载的函数和包。换句话说,data也引用了一个内置函数。