Gustavo
Gustavo

Reputation: 65

Plotting shinyTable data

I'm trying to embed a editable shiny table into a rmarkdown file. After interactively input the table my code must plot the content of the table along with other data from my workspace.

I've succeeded in the first step, I've managed to input the table using the shinyTable package. But I'm stuck trying to create the plot. I dont know how to call the table values so I can use them in other sections of my code.

I want to be able to call the generated table outside the shinyApp(.) block (see code below).

Code sample

---
output: html_document
runtime: shiny
---

#This is a R Markdown file

```{r, echo=FALSE}
library(shinyTable)
shinyApp(
server = function(input, output,session) {
  cachedTbl <- NULL
  output$tbl <- renderHtable({
    if (is.null(input$tbl)){
      rows <- 5
      tbl <- data.frame(list(num1=1:rows, 
                             num2=(1:rows)*20,
                             letter=LETTERS[1:(rows)]))
      rownames(tbl) <- LETTERS[2:(rows+1)]      
      cachedTbl <<- tbl      
      return(tbl)
    } else{
      tbl <- input$tbl
      cachedTbl <<- tbl
      return(tbl)
    }
  })  
},  
ui = fluidPage(
  mainPanel(
    htable("tbl", colHeaders="provided")
  )
)
)
renderText({
  paste(is.null(input$tbl))   ## This line returns "TRUE"
  })
```

Upvotes: 2

Views: 698

Answers (1)

Vincent
Vincent

Reputation: 5249

Try using a reactive value, either with reactiveValues or makeReactiveBinding (http://shiny.rstudio.com/reference/shiny/latest/)

```{r, echo=FALSE}
cachedTbl <<- 0
makeReactiveBinding("cachedTbl")

library(shinyTable)
shinyApp(
server = function(input, output,session) {
  # cachedTbl <- NULL
  output$tbl <- renderHtable({
    if (is.null(input$tbl)){
      rows <- 5
      tbl <- data.frame(list(num1=1:rows, 
                             num2=(1:rows)*20,
                             letter=LETTERS[1:(rows)]))
      rownames(tbl) <- LETTERS[2:(rows+1)]      
      cachedTbl <<- tbl      
      return(tbl)
    } else{
      tbl <- input$tbl
      cachedTbl <<- tbl
      return(tbl)
    }
  })  
},  
ui = fluidPage(
  mainPanel(
    htable("tbl", colHeaders="provided")
  )
)
)
renderText({
  # paste(is.null(input$tbl))   ## This line returns "TRUE"
  cachedTbl[[2]]
})
```

Upvotes: 1

Related Questions