2017-06-02 4 views
0

Wie kann ich eine benutzerdefinierte Nachricht erhalten (die Nachricht enthält eine Mini-Tabelle abhängig von reaktiven Werten), sobald die Aktionstaste glänzend gedrückt wird. Hier ist mein Code. Die Nachricht sollte die reaktive Tabelle enthalten, wo ich sie als rows() nenne.Benutzerdefinierte Nachricht beim Klicken auf Aktionsschaltfläche in r

library(shiny) 
library(datasets) 
library(rhandsontable) 
library(data.table) 


my_message= writeLines("validation is not successful. \nCheck following 
commodities:\nCPCCode Commodity Year") 

# This is the message I want when action button is clicked 


# validation is not successful. Please check the following commodities: 

#CPCCode Commodity  Year 
# 100   Maize  2010 
# 200   Rice  2015 
# 300   Tea  2016 
# 400   Banana  2014 
#. 
#. 
#. 
# and so on. The table can be upto max 50 rows 

ui = fluidPage(

actionButton("message", "message") 
) 

server = function(input,output,session){ 

rows= reactive({ 


d=data.frame(CPCCode=100, Commodity="Maize", Year= 2010) 
d$Commodity = as.character(d$Commodity) 
d=rbind(d, c(200, "Rice", "2015")) 
d=rbind(d, c(300,"Tea", 2016)) 

}) 


observeEvent(input$message,{ 
if (is.null(input$message) || input$message == 0){return()} 

isolate({ 
    input$message 
    the_message <- paste("validation is not successful") 
    js_string <- 'alert("SOMETHING");' 
    js_string <- sub("SOMETHING",the_message,js_string) 
    session$sendCustomMessage(type='jsCode', list(value = js_string)) 
    }) 

    }) 

} 

shinyApp(ui,server) 

BEARBEITET. Es zeigt nur die erste Zeile der Tabelle. Alle mögliche Vorschläge würden

library(shiny) 
library(datasets) 
library(rhandsontable) 
library(data.table) 


my_message= writeLines("validation is not successful. \nCheck following 
commodities:\nCPCCode Commodity Year") 

# This is the message I want when action button is clicked 


# validation is not successful. Please check the following commodities: 

#CPCCode Commodity  Year 
# 100   Maize  2010 
# 200   Rice  2015 
# 300   Tea  2016 
# 400   Banana  2014 
#. 
#. 
#. 
# and so on. The table can be upto max 50 rows 



ui = fluidPage(

    actionButton("show", "Show modal dialog") 

) 

server = function(input, output) { 


rows= reactive({ 


    d=data.frame(CPCCode=100, Commodity="Maize", Year= 2010) 
    d$Commodity = as.character(d$Commodity) 
    d=rbind(d, c(200, "Rice", "2015")) 
    d=rbind(d, c(300,"Tea", 2016)) 

    }) 



# Return the UI for a modal dialog with data selection input. If 'failed' 
is 
# TRUE, then display a message that the previous value was invalid. 
dataModal <- function(failed = FALSE) { 
modalDialog(

    span("Validation is not successful", rows()), 


    footer = tagList(

    actionButton("ok", "OK") 
    ) 
    ) 
    } 


observeEvent(input$show, { 
    showModal(dataModal()) 
    }) 


    observeEvent(input$ok, { 

    removeModal() 

    }) 


    } 



     shinyApp(ui,server) 
+1

Sie können 'modal Dialog' verwenden. Lesen Sie den Link [this] (https://shiny.rstudio.com/reference/shiny/latest/modalDialog.html). – SBista

+0

Vielen Dank! sehr nützlich –

+0

Aber ein bisschen verwirrt, wie ich meine Situation zuordnen –

Antwort

0

geschätzt werden Tabelle anzuzeigen, in dem modalDialog Sie benötigen ein dataTableOutput innerhalb der modalDialog hinzuzufügen und die Tabelle zu machen, nachdem die modalDialog erstellt wird. Ich habe Ihren Server-Code bearbeitet, um das Gleiche zu tun. Hoffentlich hilft dir das.

server = function(input, output) { 

    rows= reactive({ 
     d=data.frame(CPCCode=100, Commodity="Maize", Year= 2010) 
     d$Commodity = as.character(d$Commodity) 
     d=rbind(d, c(200, "Rice", "2015")) 
     d=rbind(d, c(300,"Tea", 2016)) 

    }) 

    # Return the UI for a modal dialog with data selection input. If 'failed' 
    # is 
    # TRUE, then display a message that the previous value was invalid. 
    dataModal <- function(failed = FALSE) { 
     modalDialog(

     span("Validation is not successful"), 

     dataTableOutput("table"), 

     footer = tagList(

      actionButton("ok", "OK") 
     ) 
    ) 
    } 


    observeEvent(input$show, { 
     showModal(dataModal()) 
     output$table <- renderDataTable({rows()}) 
    }) 


    observeEvent(input$ok, { 
     removeModal() 
    }) 

    } 
+0

Vielen Dank! Sehr hilfreich! –

+1

Können Sie es akzeptieren, wenn Ihre Frage beantwortet wurde? – SBista

Verwandte Themen