我试图将用户上传的Excel工作表中的标题名称列表作为左侧选择框的选择选项传递给自定义输入控件代码,如下所示: http://shiny.rstudio.com/gallery/custom-input-control.html
选择框只显示文本数据集..而不是所有标题。
我目前的代码是
ui.r:
library(shiny)
source("chooser.R")
shinyUI(fluidPage(# Application title
titlePanel("Linear Regression"),
mainPanel(tabsetPanel(
tabPanel("Upload",
# Sidebar with a slider input for number of bins
sidebarLayout(
wellPanel(
fileInput(
'file1',
'Choose XLSX File',
accept = c('sheetName', 'header'),
multiple = FALSE
)
)
,
mainPanel(tableOutput('contents'))
)),
tabPanel("Choose",
sidebarLayout(
sidebarPanel(
chooserInput("mychooser", "Available Rows", "Chosen Rows",
unlist(strsplit("dataset", split=",")) , c(), size = 10, multiple = TRUE
),
verbatimTextOutput("selection")
) ,
mainPanel("")
))
))))
server.r:
library(shiny)
require(gdata)
shinyServer(function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.xls(inFile$datapath,1)
})
output$dataset<-reactive({
inFile <- input$file1
colnames(read.xls(inFile$datapath, 1))
})
output$selection <- renderPrint(
input$mychooser
)
})
答案 0 :(得分:0)
设法使用一些javascript修复它
server.r
observe({
input$file1
inFile <- input$file1
if (is.null(inFile))
return(NULL)
colnameslist = colnames(read.xls(inFile$datapath, 1))
session$sendCustomMessage(type = "myCallbackHandler", colnameslist)
})
message.js
Shiny.addCustomMessageHandler("myCallbackHandler",
function(colnameslist) {
// alert(colnameslist);
var select = document.getElementsByClassName("left")[0];
var partsOfStr = JSON.stringify(colnameslist).split(',');
for (var i = 0; i<=partsOfStr.length; i++){
var opt = document.createElement("option");
opt.value = partsOfStr[i];
opt.innerHTML = partsOfStr[i];
select.appendChild(opt);
}
});