val*_*val 1 r dynamic shiny shiny-reactivity
列表选择selectInput()是通过对值进行硬编码来完成的,如下例所示?selectInput:
selectInput(inputID = "variable", label ="variable:",
choices = c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"))
Run Code Online (Sandbox Code Playgroud)
但是,我希望我的列表是choices来自用户选择的列的唯一值列表,该列来自用户上传的文件(csv).我怎么能这样做?这是我得到的:
UI
shinyUI(fluidPage(
fluidRow(
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
uiOutput("selectcol10"),
uiOutput("pic")
))
)
Run Code Online (Sandbox Code Playgroud)
服务器
shinyServer(function(input, output) {
filedata <- reactive({
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
temp<-read.csv(infile$datapath)
#return
temp[order(temp[, 1]),]
})
output$selectcol10 <- renderUI({
df <-filedata()
if (is.null(df)) return(NULL)
items=names(df)
names(items)=items
selectInput("selectcol10", "Primary C",items)
})
col10 <- reactive({
(unique(filedata()$selectcol10))
})
output$pic <- renderUI({
selectInput("pic", "Primary C values",col10())
})
})
Run Code Online (Sandbox Code Playgroud)
Gre*_*lia 10
对于动态UI,基本上可以采用两种方式:updateXXX或renderUI.这是采用该updateXXX方法的解决方案.
library(shiny)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
selectInput("dataset", "choose a dataset", c("mtcars", "iris")),
selectInput("column", "select column", "placeholder1"),
selectInput("level", "select level", "placeholder2")
),
mainPanel(tableOutput("table"))
))
server <- function(input, output, session){
dataset <- reactive({
get(input$dataset)
})
observe({
updateSelectInput(session, "column", choices = names(dataset()))
})
observeEvent(input$column, {
column_levels <- as.character(sort(unique(
dataset()[[input$column]]
)))
updateSelectInput(session, "level", choices = column_levels)
})
output$table <- renderTable({
subset(dataset(), dataset()[[input$column]] == input$level)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
在server功能中,有线
updateSelectInput(session, "level", choices = column_levels)
Run Code Online (Sandbox Code Playgroud)
它会更新choices第三个下拉菜单的参数level.要计算的水平,你可以使用base::levels的功能,但是这不适用于numerc的列上工作.所以我用过
as.character(sort(unique( . )))
Run Code Online (Sandbox Code Playgroud)
代替."占位符"将在启动后立即被替换.
下面的代码显示了如何将这种逻辑结合起来fileInput.只要没有选择文件,我conitionalPanel在ui隐藏下拉菜单中添加了一个.看到这里.
library(shiny)
# test data
write.csv(reshape2::tips, "tips.csv", row.names = FALSE)
write.csv(mtcars, "mtcars.csv")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput('datafile', 'Choose CSV file',
accept = c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
conditionalPanel(
# use a server side condition
condition = "output.fileUploaded",
# placeholders will be replaced from the server
selectInput("pic", "Primay C", "placeholder 1"),
selectInput("level", "select level", "placeholder 2")
)
),
mainPanel(
h3("filtered data"),
tableOutput("table")
)
)
)
server <- function(input, output, session){
# create reactive version of the dataset (a data.frame object)
filedata <- reactive({
infile <- input$datafile
if (is.null(infile))
# User has not uploaded a file yet. Use NULL to prevent observeEvent from triggering
return(NULL)
temp <- read.csv(infile$datapath)
temp[order(temp[, 1]),]
})
# inform conditionalPanel wheter dropdowns sohould be hidden
output$fileUploaded <- reactive({
return(!is.null(filedata()))
})
outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)
# update the selectInput elements according to the dataset
## update 'column' selector
observeEvent(filedata(), {
updateSelectInput(session, "pic", choices = names(filedata()))
})
## update 'level' selector
observeEvent(input$pic, {
column_levels <- unique(filedata()[[input$pic]])
updateSelectInput(session, "level", choices = column_levels,
label = paste("Choose level in", input$pic))
}, ignoreInit = TRUE)
# show table
output$table <- renderTable({
subset(filedata(), filedata()[[input$pic]] == input$level)
}, bordered = TRUE)
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)