r - 如何连接闪亮的字符串列表?
问题描述
我想根据用户输入生成一个字符串列表。但是当我使用粘贴功能时,我收到了这个错误:
object 'b' not found
我正在寻找一种方法来返回所有要在服务器中显示或进一步使用的输入值——
例如:
LV =~ x1 + x2 + x3
LV ~ LV1
等等。
也许我需要将用户输入的值存储为反应值。但我不确定该怎么做。
这是完整的代码:
library(shiny)
newlist <- as.list(c("LV", "LV2", "x1", "x2", "x3", "x4", "x5", "x6"))
lvar <- function(...) {
params <- list(...)
stopifnot(length(params)%%2==0)
lefts = params[seq(1,length(params), by=2)]
rights = params[seq(2,length(params), by=2)]
rights <- Map(paste, rights, collapse="+")
paste(paste0(lefts, " =~", rights), collapse="\n")
}
regs <- function(...) {
params <- list(...)
stopifnot(length(params)%%2==0)
lefts = params[seq(1,length(params), by=2)]
rights = params[seq(2,length(params), by=2)]
rights <- Map(paste, rights, collapse="+")
paste(paste0(lefts, "~", rights), collapse="\n")
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("variable1", "Variable:", choices = c(' ', newlist), selected = NULL),
selectInput("variable2", "Variable:", choices = c(' ', newlist), selected = NULL, multiple = TRUE),
selectInput("variable3", "Variable:", choices = c(' ', newlist), selected = NULL),
selectInput("variable4", "Variable:", choices = c(' ', newlist), selected = NULL, multiple = TRUE)
),
mainPanel(
verbatimTextOutput("listout1"),
verbatimTextOutput("listout2"),
verbatimTextOutput("listout3"),
verbatimTextOutput("listout4"),
verbatimTextOutput("listout5")
)
)
)
server <- function(input, output) {
output$listout1 <- renderText({
input$variable1
})
output$listout2 <- renderText({
input$variable2
})
output$listout3 <- renderText({
a <- lvar(lefts = input$variable1, rights = input$variable2)
})
output$listout4 <- renderText({
b <- regs(lefts = input$variable3, rights = input$variable4)
})
output$listout5 <- renderText({
paste(a, b, sep = "/n")
})
}
shinyApp(ui = ui, server = server)
解决方案
a
并且b
内部的变量分配renderText
不适用于服务器中的其他功能。
尝试:
library(shiny)
newlist <- as.list(c("LV", "LV2", "x1", "x2", "x3", "x4", "x5", "x6"))
lvar <- function(...) {
params <- list(...)
stopifnot(length(params)%%2==0)
lefts = params[seq(1,length(params), by=2)]
rights = params[seq(2,length(params), by=2)]
rights <- Map(paste, rights, collapse="+")
paste(paste0(lefts, " =~", rights), collapse="\n")
}
regs <- function(...) {
params <- list(...)
stopifnot(length(params)%%2==0)
lefts = params[seq(1,length(params), by=2)]
rights = params[seq(2,length(params), by=2)]
rights <- Map(paste, rights, collapse="+")
paste(paste0(lefts, "~", rights), collapse="\n")
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("variable1", "Variable:", choices = c(' ', newlist), selected = NULL),
selectInput("variable2", "Variable:", choices = c(' ', newlist), selected = NULL, multiple = TRUE),
selectInput("variable3", "Variable:", choices = c(' ', newlist), selected = NULL),
selectInput("variable4", "Variable:", choices = c(' ', newlist), selected = NULL, multiple = TRUE)
),
mainPanel(
verbatimTextOutput("listout1"),
verbatimTextOutput("listout2"),
verbatimTextOutput("listout3"),
verbatimTextOutput("listout4"),
verbatimTextOutput("listout5")
)
)
)
server <- function(input, output) {
output$listout1 <- renderText({
input$variable1
})
output$listout2 <- renderText({
input$variable2
})
output$listout3 <- renderText({
#a <-
lvar(lefts = input$variable1, rights = input$variable2)
})
output$listout4 <- renderText({
#b <-
regs(lefts = input$variable3, rights = input$variable4)
})
output$listout5 <- renderText({
paste(lvar(lefts = input$variable1, rights = input$variable2), regs(lefts = input$variable3, rights = input$variable4), sep = "\n") # changed /n to \n
})
}
shinyApp(ui = ui, server = server)
如果您想要a
并且b
可以使用其他功能,您应该使用响应式值。
推荐阅读
- c# - 如何在c#中动态寻址对象
- symfony - Symfony / Doctrine:关于存储库注入的 Restful API 设计和设计模式
- reactjs - React-Router 和 iFrame
- verilog - 谁能解释 SystemVerilog 中“$sreadmemh”的用法?我在任何地方都没有得到明确的解释
- database - Cassandra CQLEngine 允许过滤
- c++ - C++ 调用构造函数来初始化对象会产生错误
- javascript - 没有 MongoDB 的 Meteor 1.7
- json - 带有接口的 Angular http 请求:statusText:“未知错误”
- spring-boot - Spring Kafka在侦听器中按条件丢弃消息
- python - 为csv中的每一行创建一个具有相应名称的文件夹?