我有一个很简单的问题,但无法弄清楚为什么它不起作用
library(shiny)
library(leaflet)
pts <- data.frame(
id = letters[seq(from = 1, to = 10)],
x = rnorm(10, mean = -93.625),
y = rnorm(10, mean = 42.0285),
stringsAsFactors = F
)
# Define UI
ui <- fluidPage(uiOutput('Select'))
server <- function(input, output, session) {
pts
output$Select <- renderUI({
Range <- sort(unique(pts$id))
selectInput("dataselect",
"select",
choices = Range,
selected = 'a')
})
mydata <- reactive({
if (input$dataselect != 'a') {
data <- pts[pts$id == input$dataselect,]
}
else
{
data <- pts
}
})
observe(print(mydata()))
}
shinyApp(ui = ui, server = server)
如果使用所选值选择了'a'
以外的任何内容,我基本上会尝试对我的数据集进行子集化。如果选择'a'
,我希望返回整个df。
只需碰上
警告:if:参数长度为零的错误[无堆栈跟踪 可用]
答案 0 :(得分:1)
如果input $ dataselect不可用,则不需要运行mydata(),可以通过插入以下命令来完成:req(input $ dataselect)
如下所示:
library(shiny)
library(leaflet)
pts <- data.frame(
id = letters[seq(from = 1, to = 10)],
x = rnorm(10, mean = -93.625),
y = rnorm(10, mean = 42.0285),
stringsAsFactors = F
)
# Define UI
ui <- fluidPage(uiOutput('Select'))
server <- function(input, output, session) {
output$Select <- renderUI({
Range <- sort(unique(pts$id))
selectInput("dataselect",
"select",
choices = Range,
selected = 'a')
})
mydata <- reactive({
req(input$dataselect)
if (input$dataselect != 'a') {
data <- pts[pts$id == input$dataselect,]
}
else
{
data <- pts
}
})
observe(print(mydata()))
}
shinyApp(ui = ui, server = server)