如何在R中交互编辑矢量
问题描述:
我想在R中创建一个矢量,添加元素并在ui.R中显示结果。如何在R中交互编辑矢量
但似乎在server.R中,它会覆盖向量中的旧元素,而不是附加向量。
这里是我的代码
ui.R
shinyUI(pageWithSidebar(
headerPanel("actionButton test"),
sidebarPanel(
textInput("symb", "Symbol", "SPY"),
br(),
actionButton("addButton", "Add"),
p("Click the button to update the value displayed in the main panel.")
),
mainPanel(
verbatimTextOutput("nText")
)
))
server.R
equity_list = c("ACN")
shinyServer(function(input, output) {
output$nText <- renderText({
# Take a dependency on input$goButton
input$goButton
# Use isolate() to avoid dependency on input$n
isolate(input$symb)
equit_list <<- append(equity_list, input$symb)
})
})
答
嗨,你在你的应用程序两个问题:
你做不使用的正确编号在
server.R
你必须覆盖
equity_list
当你添加一个新的元素
试试这个:
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel("actionButton test"),
sidebarPanel(
textInput("symb", "Symbol", "SPY"),
br(),
actionButton("addButton", "Add"),
p("Click the button to update the value displayed in the main panel.")
),
mainPanel(
verbatimTextOutput("nText")
)
),
server = function(input, output) {
equity_list = c("ACN")
output$nText <- renderText({
input$addButton
isolate({
equity_list <<- append(equity_list, input$symb)
})
})
}
))
编辑:2个按钮,这并不容易。你可以尝试下面的应用程序,这是你必须在“删除”之后添加“添加”。我已经改变了textInput
为selectizeInput
,它更灵活:
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel("actionButton test"),
sidebarPanel(
selectizeInput(inputId = "symb", label = "Symbol", selected = "SPY", choices = "SPY", multiple = TRUE, options = list(create = TRUE)),
br(),
actionButton("addButton", "Add"),
p("Click the button to update the value displayed in the main panel."),
actionButton("deleteButton", "Delete")
),
mainPanel(
verbatimTextOutput("nText")
)
),
server = function(input, output, session) {
equity_list = c("ACN")
equity_rea <- reactive({
equity_list <- append(equity_list, input$symb)
})
observe({
input$deleteButton
updateSelectizeInput(session = session, inputId = "symb", selected = "SPY")
})
output$nText <- renderText({
# with add button
input$addButton | input$deleteButton
isolate(equity_rea())
# without add button
#equity_rea()
})
} ))
真棒!解决我的问题。 – jerry2605 2014-09-11 11:33:34
不客气! – Victorp 2014-09-11 11:51:55
你好Vcitorp,另外一个问题,如果我想添加一个删除按钮,怎么办?对不起,我在Shiny中很新。 – jerry2605 2014-09-15 07:50:06