简体   繁体   中英

can we observe output from rendertable in shiny

I have a application that has a reative table(based on 2 selectInputs) and a graph. The data for graph is taken from reactive table.

So both graph and table is using the same data. So while constructing a graph, can I observe what the table is having.

Or should I read the same table again in the graph? I mean should we call head(iris,n = as.numeric(input$rows)) again twice below?

Example,

library(shiny)
library(DT)
library(rAmCharts)

ui <- fluidPage(
  selectInput("rows","Rows",c(1:150)),
  dataTableOutput("input_table"),
  amChartsOutput("barplot",width = 750, height = 500)
)

server <- function(input, output, session) {
  
  output$input_table <- renderDataTable({
    new_iris <- head(iris,n = as.numeric(input$rows))
    datatable(new_iris)
  })
  
  
  
  output$barplot <-  renderAmCharts({
    new_iris1 <- head(iris,n = as.numeric(input$rows))  ## should i call this again???????? Cannot we use from rendertable?
    new_iris1 <- new_iris1 %>% group_by(Species) %>% summarise(total = sum(Petal.Length))
    pipeR::pipeline(
      amBarplot(
        x = "Species",
        y = "total",
        ylab = "X",
        xlab = "Y",
        data = new_iris1,
        labelRotation = 90
      ),
      setChartCursor()
    )
    
  })
}

shinyApp(ui, server)

You may want to put your data object in a reactive expression so you can see what is being rendered, like so, this way you can access data() later on in your app

library(shiny)
library(DT)
library(dplyr)
library(rAmCharts)

ui <- fluidPage(
  selectInput("rows","Rows",c(1:150)),
  dataTableOutput("input_table"),
  amChartsOutput("barplot",width = 750, height = 500)
)

server <- function(input, output, session) {
  
  data <- eventReactive(input$rows,{
    head(iris,n = as.numeric(input$rows))
  })

  output$input_table <- renderDataTable({
    datatable(data())
  })
  
  output$barplot <-  renderAmCharts({
    new_iris1 <- data()
    new_iris1 <- new_iris1 %>% group_by(Species) %>% summarise(total = sum(Petal.Length))
    pipeR::pipeline(
      amBarplot(
        x = "Species",
        y = "total",
        ylab = "X",
        xlab = "Y",
        data = new_iris1,
        labelRotation = 90
      ),
      setChartCursor()
    )
    
  })
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM