我在获取 selectizeInput 的输出以更新上传的 csv 数据时遇到问题。有问题的具体代码块是
observeEvent(input$Go, {
tmp <- values$df_data
output$Grp = renderUI({
lvls <- tmp %>%
pull(Group) %>%
levels
selectizeInput('group', 'Groups', choices = lvls, selected = lvls[1], multiple = FALSE)
})
tmp1 <- tmp %>%
filter(Group == "A") # need to change here by updating the value from input$Grp
values$df_data <- tmp1
})
“组”列使用硬编码值进行过滤。我的目的是通过获取“Grp”的值来使其动态化。下图显示了问题
完整代码如下
library(shinydashboard)
library(shiny)
library(dplyr)
library(tidyr)
ui = dashboardPage(
dashboardHeader(),
dashboardSidebar(
width = 200,
sidebarMenu(
menuItem("File Upload", icon = shiny::icon("table")),
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
uiOutput('Grp'),
actionButton("Go","Subset Data"))
),
dashboardBody( fluidPage(
titlePanel("Data Table"),
sidebarLayout(
tabPanel("tab",
div( id ="Sidebar",sidebarPanel(
))),
mainPanel(
DT::dataTableOutput("tableOut")
)
)
)
)
)
server = function(input, output, session) {
values <- reactiveValues(df_data = NULL)
observeEvent(input$datafile, {
values$df_data <- read.csv(input$datafile$datapath)
})
observeEvent(input$Go, {
tmp <- values$df_data
output$Grp = renderUI({
lvls <- tmp %>%
pull(Group) %>%
levels
selectizeInput('group', 'Groups', choices = lvls, selected = lvls[1], multiple = FALSE)
})
tmp1 <- tmp %>%
filter(Group == "A") # need to change here by updating the value from input$Grp
values$df_data <- tmp1
})
output$tableOut <- DT::renderDataTable({
DT::datatable(values$df_data)
})
}
shinyApp(ui = ui, server = server)
我正在上传 .csv 文件。以下数据与我的数据相似。
set.seed(12345)
data <- data.frame(DATE = rep(seq(as.Date("2014-01-01"), length.out = 200,
by = "week"), each = 4), Group = rep(c("A", "B", "C", "D"), each = 200),
ValueColumn = rnorm(800))