R 与Shining中的selectize.js选项交互
假设我有以下闪亮的应用程序:R 与Shining中的selectize.js选项交互,r,shiny,selectize.js,R,Shiny,Selectize.js,假设我有以下闪亮的应用程序: library(shiny) shinyApp( ui=fluidPage( selectizeInput( inputId = "foo", label = NULL, choices = c("a", "b"), options = list( create = TRUE ) ) ), server=function(input, output, ses
library(shiny)
shinyApp(
ui=fluidPage(
selectizeInput(
inputId = "foo",
label = NULL,
choices = c("a", "b"),
options = list(
create = TRUE
)
)
),
server=function(input, output, session){
}
)
这是一个非常简单的应用程序,我在其中使用selectize.js
生成了一个下拉列表。create
选项允许用户输入自定义选择作为输入(与a或b不同)
如果用户键入内容,它将显示以下内容:
我希望,当用户单击“添加c…”时,应用程序将在应用程序库中保存一个名为c.txt
的文件,其中包含字符串“hello”。selectize.js
的文档表明create
选项可以将布尔值或函数作为其参数,因此,我凭直觉猜测
create = function(input){write("hello", paste0(input, ".txt"))}
而不是create=TRUE
将起作用,但事实并非如此
有人能帮我吗?
selectize.js
让我们添加一个js函数而不是R函数
但使用R可以实现同样的效果:
library(shiny)
shinyApp(
ui = fluidPage(
selectizeInput(
inputId = "foo",
label = NULL,
choices = c("a", "b"),
options = list(create = TRUE)
)
),
server = function(input, output, session) {
writeSelectizeTxt <- function(selectedChoices) {
for (selection in selectedChoices) {
fileName <- paste0(selection, ".txt")
if (!file.exists(fileName)) {
write("hello", fileName)
cat("Wrote file: ", file.path(getwd(), fileName))
}
}
}
observeEvent(input$foo, {
req(input$foo)
writeSelectizeTxt(input$foo)
}, ignoreNULL = TRUE,
ignoreInit = FALSE)
}
)
库(闪亮)
shinyApp(
ui=fluidPage(
选择输入(
inputId=“foo”,
label=NULL,
选项=c(“a”、“b”),
选项=列表(创建=真)
)
),
服务器=功能(输入、输出、会话){
WriteSelectizeText