我想在R闪亮中创建一个矢量,添加元素并在ui.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)
})
})
答案 0 :(得分:1)
您好,您的应用中存在两个问题:
您未在actionButton
server.R
的正确ID
添加新元素时必须覆盖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个按钮并不容易。你可以尝试下面的应用程序,hic是你必须在“删除”后clic“添加”。我已为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()
})
} ))