2017-06-02 42 views
0

如果在閃亮的操作按鈕單擊後,如何獲得自定義消息(該消息包含取決於反應值的迷你表)?這是我的代碼。該消息應該包含被稱爲rows()的反應表。單擊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) 

EDITED。它只顯示錶格的第一行。任何建議,將不勝感激

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

您可以使用'模態對話框'。請參閱[本](https://shiny.rstudio.com/reference/shiny/latest/modalDialog.html)鏈接。 – SBista

+0

謝謝!非常有用 –

+0

但有點困惑如何關聯我的情況 –

回答

0

要顯示錶中的modalDialog,您將需要添加一個dataTableOutputmodalDialog內,使表創建modalDialog後。我編輯了你的服務器代碼來做同樣的事情。希望這可以幫助你。

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

非常感謝你!很有用! –

+1

如果您的問題得到解答,您能接受嗎? – SBista