我正在开发一个应用程序,用户将上传数据,然后从该数据中选择要绘制在图表上的变量。我希望变量的类型显示在选择器中。
我正在使用示例 here和 here (see "email contacts") 。 Shiny 的 selectize 示例通过 ajax 获取数据,但我不知道如何使其适应用户上传的数据,这些数据作为 reactiveValues
对象存储在我的应用程序中。我还尝试使用 selectize 网站示例中的语法。我所有的努力都导致选择器出现,没有任何可见的数据。
# the code that creates the data I'm hoping to pass to the selector
stored_data$variable_selector <- data_frame(
varlist = names(stored_data$data),
type = map_chr(names(stored_data$data), ~class(stored_data$data[[.x]]))
) %>%
purrr::transpose()
# the code which currently renders an empty selector
selectizeInput('variable', 'select a variable:', choices = '', options = list(
valueField = 'varlist',
labelField = 'type',
searchField = 'varlist',
options = stored_data$variable_types,
create = FALSE,
render = I("{
option: function(item, escape) {
return '<div>' +
' <em>' + escape(item.varlist) + '</em>' +
' (by ' + escape(item.type) + ')' +
'</div>';
}
}")))
我应该将variable_types 制作成json 对象,然后再将其传递给selectize 中的渲染吗?我还有什么遗漏的吗?
最佳答案
看起来我测试不正确:下面呈现了一个 selectizeInput,其类型信息在下拉列表中可见。
library(shiny)
library(tidyverse)
iris_type <- data_frame(
varlist = names(iris),
type = map_chr(names(iris), ~class(iris[[.x]]))
) %>%
purrr::transpose()
ui <- basicPage(
# the code which currently renders an empty selector
tagList(
selectizeInput('variable', 'select a variable:', choices = '',
options = list(
valueField = 'varlist',
labelField = 'varlist',
searchField = c('varlist', 'type'),
options = iris_type,
create = FALSE,
render = I(
"{
option: function(item, escape) {
return '<div>' +
' <em>' + escape(item.varlist) + '</em>' +
' (type: ' + escape(item.type) + ')' +
'</div>';
}
}"
)
)
),
textOutput("out")
)
)
server <- function(input, output, session) {
output$out <- renderText(input$variable)
}
shinyApp(ui, server)
关于r - Shiny + selectize.js : render custom item from local source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51576624/