我正在使用以下代码,并且始终会收到此子表错误。我在搞什么,我在哪里错。这应该是一些我修改过的基本输入代码,并且在某些时候确实起作用了,但我看不到错误。
谢谢
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
})
})
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的那一刻就中断了?如果“数据”是一个内置函数,那是否也不会与下面的示例发生冲突?对不起您的理解,但这使我感到困惑。
服务器
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")
)
)
)
扩展@Roland的注释:您正在进行命名空间冲突。data
基本R中有一个函数,因此,如果Rdata
在当前环境中找不到对象,data
则从全局环境中引用该函数。在您的特殊情况下,发生这种情况是因为ui.R
和server.R
处于不同的环境中,此外,各个功能主体都有各自的环境。因此,data
influidRow(...)
中没有引用data
from output$table
。您需要传递参数和/或使用该函数动态构建UI。例如参见这里。
更新已更新的问题:
替换data
为mpg
in可以ui.R
解决此问题,因为它mpg
被定义为全局环境中的数据集(这是的副作用library(ggplot2)
)。因此mpg
,(几乎)总是可以访问并具有必要的属性。对于一个更加公平的比较,替换mpg
在ui.R
用data
,这应该带回的老问题,是因为data
在全球环境指的是功能,而不是你想操纵数据帧。
超级更新具有用于动态定义和加载每个数据集选择元素的更通用的解决方案:
服务器代码循环遍历所选数据帧的所有列,并为具有非double类型的每一列动态生成一个选择框。(带双精度的唯一性和相等性只是在问麻烦。)这避免了范围问题,因为UI元素是在server.R
调用加载数据的反应函数之后创建的。
library(shiny)
library(ggplot2)
# Define a server for the Shiny app
shinyServer(function(input, output) {
get.data <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars,
"mpg" = mpg,
"mtcars" = mtcars,
"diamonds" = diamonds)
})
# Filter my.data based on selections
output$table <- renderDataTable({
my.data <- get.data()
for(n in names(my.data)){
# avoid too many cases ...
# unique() with double is just asking for trouble
if(typeof(my.data[,n]) != "double"){
val <- eval(parse(text=paste0("input$",n)))
print(val)
if(val != "All"){
my.data <- eval(parse(text=paste0("subset(my.data,",n,"==",val,")")))
}
}
}
my.data
})
output$dyn.ui <- renderUI({
my.data <- get.data()
sel <- NULL
for(n in names(my.data)){
# avoid too many cases ...
# unique() with double is just asking for trouble
if(typeof(my.data[,n]) != "double"){
sel <- c(sel,
selectInput(n, n, choices=c("All",unique(my.data[,n])))
)
}
}
sel
})
})
library(shiny)
# Define the overall UI
shinyUI(fluidPage(
titlePanel("Displaying tables dynamically with renderUI() and eval()"),
sidebarLayout(
sidebarPanel(h2("Selection"),
selectInput("dataset", "Dataset", c("rock", "pressure", "cars","mtcars","diamonds")),
# Create a new Row in the UI for selectInputs
uiOutput("dyn.ui")
)
,mainPanel(h2("Data"),
dataTableOutput(outputId="table")
)
)
))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句