2017-02-20 3 views
1

Ich versuche, einen Wert aus einer Eingabe (im vorliegenden Fall input$n) an eine Liste anzuhängen (im vorliegenden Fall die Variable "keyword_list"), wenn der Benutzer die Aktion a drückt Schaltfläche (im vorliegenden Fall die Schaltfläche input$goButton).Eine Variable mit Eingabedaten aktualisieren

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) 
    }) 


} 

Antwort

1

Wie wäre es damit:

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) 
+0

ich mit 'reactiveValues' als gut, aber statt' if (Eingang $ goButton == 0) return() 'Ich würde verwenden' req gehen würde (Eingang $ goButton! = 0) ':) –

+1

Ich war auch kein Fan von diesem Teil, um ehrlich zu sein, aber lieber den ursprünglichen Code so wenig wie möglich zu ändern, um nur die eigentliche Frage zu beantworten. – BigDataScientist

+0

Hallo, Beide Lösungen funktionieren gut! Vielen Dank. Jetzt kann ich in Ruhe Skifahren gehen ;-). Haben Sie einen guten Tag. – Bastian

Verwandte Themen