闪亮:在 inFile 更改时更新 selectinput

Shiny: update selectinput on change of inFile

我想根据上传的文件 (fileInput) 提供输入选择 "choices"。在下面的示例中,我希望 as.list(mydata[1, 1:5]) 作为 inputselect 选择的值。稍后子集值将是动态的,此处未显示。

我尝试了论坛中建议的几种方法(reactive、observe、reactiveValue 及其组合),但没有取得太大成功。

我的脚本部分运行,但是我需要刷新页面才能上传 "choices" 并重新加载文件。

server.R

shinyServer(function(input, output, session) {

output$contents <- renderDataTable({

    inFile <<- input$SoftRecom
    if (is.null(inFile))
        return(NULL)
    filedatapath <<- reactive({inFile$datapath})
    mydata <<- read.csv(filedatapath(), header = TRUE, sep = ',')
    mydata
})


mychoices <<- reactive({
    mydata
    print(mydata)
    })

output$vg <- renderUI({
    selectInput("vg", label =  p("goal", style = "color:#FFA500"), 
       mychoices()[1,1:5], selected = 1)
})   

output$vp <- renderUI({
    selectInput("procedure", label =  p("procedure", style = "color:#FFA500"), 
                choices = c("proecudures"), selected = 1)

})

output$vm <- renderUI({
    selectInput("procedure", label =  p("procedure", style = "color:#FFA500"), 
                choices = c("ChIP-seq"), selected = 1)

})
})

ui.R

shinyUI(fluidPage(theme = "bootstrap.css",
titlePanel("simple software recommendation sytem"),
sidebarLayout(
    sidebarPanel(
        fileInput('SoftRecom', 'choose dataset'),

        uiOutput("vg"), # variable goal 
        uiOutput("vp"), # variable procedure
        uiOutput("vm")  # variable method


    ),
    mainPanel(
        dataTableOutput('contents')
    )
)
))

我在论坛上看到很多例子和答案,非常接近(甚至匹配)我的问题。抱歉这么迟钝。如果有人能指出我的问题,我将非常感激。

周杰伦

最后我自己找到了解决办法。不要对我的问题和答案中的不同服务器代码感到困惑。看看

之间的关系
  • uiOutput('pipelinestep') 和
  • output$pconst <<- renderUI({selectizeInput( 'pconst', 'construct software workflow', 选择 = as.character(mysoft[mysoft$goal==mypipefilter, 3]), multiple = TRUE, 选项 = list(maxItems = 1))}

UI.R

我必须插入:uiOutput("pipelinestep") 见第 8 行

shinyUI(fluidPage(theme = "bootstrap.css",
titlePanel( h2("simple software recommendation system", style = "color:#FFA500")),
    sidebarLayout(position = "left",
        sidebarPanel(width =3,
             # chose standard pipeline
             selectInput("selectpipe", "select standard pipeline:", choices = pipechoices),
             # software details
             *uiOutput("pipelinestep")*, # software per pipeline step,
             # construct software workflow based on selected pipeline step 
             uiOutput("pconst")
        ))))

server.R

请参阅第 5 行到第 7 行。"Choices" 会在检测到更改后立即分配新值。请在此处查看文档:http://shiny.rstudio.com/articles/dynamic-ui.html

pipelinestepsoftInput <<- reactive({
    mypipefilter <- input$pipelinestep
    softperpipe <<- mysoft[mysoft$goal==mypipefilter ,c(1,3,5:7), drop = FALSE]
    ## provides software choices related to the pipeline step
    output$pconst <<- renderUI({selectizeInput(
        'pconst', 'construct software workflow', choices = as.character(mysoft[mysoft$goal==mypipefilter, 3]),
        multiple = TRUE, options = list(maxItems = 1))})
    ## input for outputDataTable
    softperpipe 
    })