使用输入变量在Shiny中切片数据集

我有以下Shiny代码。我想用这个切片数据集,并从该数据子集创建一个图形。使用输入变量在Shiny中切片数据集

library(shiny) 

library(ggplot2)

library(dplyr)

# Define UI for dataset viewer app ----

ui <- fluidPage(

# App title ----

titlePanel("Shiny Text"),

# Sidebar layout with a input and output definitions ----

sidebarLayout(

# Sidebar panel for inputs ----

sidebarPanel(

# Input: Selector for choosing dataset ----

selectInput(inputId = "dataset",

label = "Choose a dataset:",

choices = c("ga_data")),

selectInput(inputId = "week",

label = "Choose a number:",

choices = c(21000, 23400, 26800)),

# Input: Numeric entry for number of obs to view ----

numericInput(inputId = "obs",

label = "Number of observations to view:",

value = 10)

),

# Main panel for displaying outputs ----

mainPanel(

# Output: Verbatim text for data summary ----

verbatimTextOutput("summary"),

# Output: HTML table with requested number of observations ----

plotOutput("view")

)

)

)

# Define server logic to summarize and view selected dataset ----

server <- function(input, output) {

employee <- c('John Doe','Peter Gynn','Jolie Hope')

salary <- c(21000, 23400, 26800)

startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))

ga_data <- data.frame(employee, salary, startdate)

ga_data %>%

filter(salary == input$week)

# Return the requested dataset ----

datasetInput <- reactive({

switch(input$dataset,

"ga_data" = ga_data)

})

# Show the first "n" observations ----

output$view <- renderPlot({

ggplot(data=ga_data, aes(x=employee, y=salary)) + geom_bar(stat="identity")

+ ggtitle("input$week")

})

}

# Create Shiny app ----

shinyApp(ui = ui, server = server)

有了这个,我希望能够根据输入列表过滤数据集。所以如果我选择df应该切片的数字。

然而,当我尝试它,我得到:

invalid argument to unary operator 

在什么位置出了问题有什么想法?

回答:

您好像还可以用,所以我离开了出来:

datasetInput <- reactive({ switch(input$dataset, "ga_data" = ga_data) })

我定义之外的服务器上的数据帧以及

library(shiny) 

library(ggplot2)

library(dplyr)

employee <- c('John Doe','Peter Gynn','Jolie Hope')

salary <- c(21000, 23400, 26800)

startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))

ga_data <- data.frame(employee, salary, startdate)

# Define UI for dataset viewer app ----

ui <- fluidPage(

# App title ----

titlePanel("Shiny Text"),

# Sidebar layout with a input and output definitions ----

sidebarLayout(

# Sidebar panel for inputs ----

sidebarPanel(

# Input: Selector for choosing dataset ----

selectInput(inputId = "dataset",

label = "Choose a dataset:",

choices = c("ga_data")),

selectInput(inputId = "week",

label = "Choose a number:",

choices = c(21000, 23400, 26800)),

# Input: Numeric entry for number of obs to view ----

numericInput(inputId = "obs",

label = "Number of observations to view:",

value = 10)

),

# Main panel for displaying outputs ----

mainPanel(

# Output: Verbatim text for data summary ----

verbatimTextOutput("summary"),

# Output: HTML table with requested number of observations ----

plotOutput("view")

)

)

)

# Define server logic to summarize and view selected dataset ----

server <- function(input, output) {

data <- eventReactive(input$week,{

ga_data[ga_data$salary %in% as.numeric(input$week),]

})

# Show the first "n" observations ----

output$view <- renderPlot({

ggplot(data= data(), aes(x=employee, y=salary)) + geom_bar(stat="identity")+ ggtitle(input$week)

})

}

# Create Shiny app ----

shinyApp(ui = ui, server = server)

以上是 使用输入变量在Shiny中切片数据集 的全部内容, 来源链接: utcz.com/qa/265614.html

回到顶部