使用输入数据更新变量

时间:2017-02-20 22:07:50

标签: r variables shiny

当用户按下操作按钮时,我试图将输入中的值(在本例中为input$n)附加到列表(在本例中为变量“keyword_list”)在本例中,按钮input$goButton)。

ui.R
library(shiny)

pageWithSidebar(
  headerPanel("actionButton test"),
  sidebarPanel(
    #numericInput("n", "N:", min = 0, max = 100, value = 50),
    textInput("n", "Caption", "Enter next keyword"),
    br(),
    actionButton("goButton", "Go!"),
    p("Click the button to update the value displayed in the main panel.")
  ),
  mainPanel(
    verbatimTextOutput("nText"),
    dataTableOutput('mytable')
  )
)
})

server.R
library(shiny)

# Define server logic required to summarize and view the selected
# dataset
function(input, output,session) {

#prepare data
keyword_list <- matrix()
makeReactiveBinding('keyword_list')

observe({
  if (input$goButton == 0)
    return()

  isolate({
    keyword_list <- append(keyword_list,input$n)  })
})

  ntext <- eventReactive(input$goButton, {
   input$n
  })


  output$nText <- renderPrint({
    #input$n
    ntext()
  })

  output$mytable = renderDataTable({
    as.data.frame(keyword_list)
  })


}

1 个答案:

答案 0 :(得分:1)

这个怎么样:

library(shiny)

ui <- pageWithSidebar(
  headerPanel("actionButton test"),
  sidebarPanel(
    #numericInput("n", "N:", min = 0, max = 100, value = 50),
    textInput("n", "Caption", "Enter next keyword"),
    br(),
    actionButton("goButton", "Go!"),
    p("Click the button to update the value displayed in the main panel.")
  ),
  mainPanel(
    verbatimTextOutput("nText"),
    dataTableOutput('mytable')
  )
)
})

library(shiny)

# Define server logic required to summarize and view the selected
# dataset
server <- function(input, output,session) {

  global <- reactiveValues(keyword_list = "")

  observe({
    if (input$goButton == 0)
      return()

    isolate({
      global$keyword_list <- append(global$keyword_list, input$n)
    })
  })

  ntext <- eventReactive(input$goButton, {
    input$n
  })


  output$nText <- renderPrint({
    #input$n
    ntext()
  })

  output$mytable = renderDataTable({
    as.data.frame(global$keyword_list)
  })
}

shinyApp(ui, server)