【发布时间】:2018-10-07 04:18:41
【问题描述】:
我想创建一个简单的闪亮应用程序。在应用程序中我上传了一个 csv 文件。这里我使用的 csv 文件是 mtcars.csv。然后有两个selectInputs。我用上传的 csv 文件的列填充第一个。第二个由第一个 selectInput 中已选择的字段的所有值填充。直到这里一切都很好。当我单击 actionButton 时问题就开始了:我想渲染表上传的 csv 的所有记录,其中第一个 selectInput 等于第二个。例如,如果 csv 文件是 mtcars,第一个 selectInput 是圆柱体,第二个是 6,我想渲染表所有有 6 个圆柱体的汽车。
但是,结果始终为空。这是我的 UI 的代码:
ui <- fluidPage(
# Application title
titlePanel("This is a test!"),
sidebarLayout(
sidebarPanel(
fileInput("file", "Browse",
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
selectInput("first","Variable Name", choices = NULL),
selectInput("second","Ranges", choices = NULL),
actionButton("btn","Proceed")
),
mainPanel(
tableOutput("out")
)
)
)
这是我的服务器:
server <- function(input, output, session) {
#storing the csv in df
df = reactive({
req(input$file)
return(read_csv(input$file$datapath))
})
#populating first inputSelect
observe({
choices1 = colnames(df())
updateSelectInput(session,"first", choices = choices1)
})
#populating second inputSelect
observeEvent(input$first,{
choices1 = df() %>%
select(input$first) %>%
unique()
updateSelectInput(session,"second", choices = choices1)
})
#The goal is to return all rows in df() where the values
#of column in first selectInput is equal to second selectInput
observeEvent(input$btn,{
x = df() %>%
filter(input$first == input$second) #problem: this is always empty
output$out = renderTable(x)
})
}
我是闪亮的新手。我正在寻找正确的方法来做到这一点。 这是空输出的快照:
【问题讨论】: