How to display a grid.table with Shiny - r

I want to display a dataframe in a table using grid.table(myDataFrame). I need help figuring out:
what output* and render* functions to use with shiny
what to write exactly in the render* function body
This far I have the following codes
In the UI.R, inside fluidPage and fluidRow:
dataTableOutput("TauxInsertion")
And then in Server.R:
output$TauxInsertion <- renderDataTable({
dataDepartement()
# TauxInsertionTable <- grid.table(dataDepartement())
# TauxInsertionTable
})
dataDepartement is a reactive variable that contains a dataFrame. Returning this data frame inside the renderDataTable gives me a table. But I need to be able to display the row names and add some color and style to the display. The commented part is what I have tried but doesn't display anything.
There are no significant messages in the console. I have also tried options(shiny.trace=TRUE) but to no avail.

I think you have to use functions dedicated to plot. Take a look
library(shiny)
library(grid)
library(gridExtra)
ui <- fluidPage(
plotOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlot({
grid.table(head(iris,3))
})
}
shinyApp(ui = ui, server = server)

Related

Is there a way to render plots from Data Explorer library on Shiny App

I'm trying to plot the outcomes of EDA on to Shiny App, I have been using DataExplorer library for the same and i'm able to perform operations on rmarkdown notebook. I was thinking to integrate the plots to shiny app using the below code but i'm running into errors,Can you please assist me in this regard and also suggest me if there is a way possible to achieve this.
UI part
library(shiny)
library(DataExplorer)
fluidRow(width=12,
column(12,plotOutput("struct"))
)
Server block
df<-read.csv("/path/to/csv/file.csv")
output$struct<-renderPlot({
req(df)
plot_str(df)
})
Thanks for the help in advance
DataExplorer::plot_str by default prints a networkD3::diagonalNetwork, however, it returns a list.
If you want to render the diagonalNetwork object in shiny you'll need to use networkD3::renderDiagonalNetwork. Please check the following:
library(shiny)
library(DataExplorer)
library(datasets)
library(networkD3)
# DF <- read.csv("/path/to/csv/file.csv")
DF <- mtcars
ui <- fluidPage(
fluidRow(column(12, diagonalNetworkOutput("struct")))
)
server <- function(input, output, session) {
output$struct <- renderDiagonalNetwork({
req(DF)
diagonalNetwork(plot_str(DF, print_network = FALSE))
})
}
shinyApp(ui, server)

Print output R shiny

can anyone explain to me how to view an output on r shiny?
The code of my app is very long, you can find it at this link Print output on R shiny that is a very similar question, however, I have not received an answer.
My question is: once I have created a dataframe, how do I print it? I insert the most important part of the code in my opinion:
shiny::observeEvent(input$modmatbut, {
disegno<<-as.data.frame(oa.design(factor.names=values$levnames))
})
input$modmatbut is when is when the button is pressed.
This function works (I insert the photo of an output obtained and as you can see it is a dataframe)
Now, I would like diesgno to be printed on the screen
enter image description here
Can you maybe switch to using eventReactive and access the table via disegno()
library(shiny)
ui <- fluidPage(
actionButton("modmatbut","modmatbut"),
tableOutput("draw")
)
server <- function(input, output, session){
disegno <- eventReactive(input$modmatbut,{
as.data.frame(oa.design(factor.names=values$levnames))
})
output$draw <- renderTable({
disegno()
})
}
shinyApp(ui, server)

How to show ggplot from external function in shiny R application?

I need to create shiny app which will create a plot basing on dropdown menu choise. The whole computation part is pretty complicated and so is the plot – I created a function which is returning ggplot and I just wanted to show it in the app.
My idea looks as follows:
library(shiny)
source('Analysis/function_external.R')
list_names = c('a', 'b', 'c')
ui <- fluidPage(
selectInput("data", "Select data to plot", choices = list_names)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
observe({function_external(input$data)})
}
# Run the application
shinyApp(ui = ui, server = server)
It is making function run every time I change the input, but it does not show anything. I would really appreciate if you can point me into good direction.
output$my_complicated_plot <- renderPlot({ function_external(input$data) })
Solved the issue.

Use `shinycssloaders::withSpinner` to cover more than one input

I am using shinycssloaders to show loading animation. There are multiple inputs on the page which are loaded from the server. These inputs are also dependent on each other.
In the below example I have used a reactive object to create such dependency. First the table is displayed and only when the calculation of table is completed (rv$a <- 1) plot can be completed.
library(shiny)
library(shinycssloaders)
ui <- fluidPage(
withSpinner(tableOutput('data')),
withSpinner(plotOutput('plot'))
)
server <- function(input, output) {
rv <- reactiveValues(a = NULL)
output$data <- renderTable({
#Some long calculation here, using Sys.sleep for simplicity
Sys.sleep(2)
rv$a <- 1
head(mtcars)
})
output$plot <- renderPlot({
req(rv$a)
#Some long calculation here, using Sys.sleep for simplicity
Sys.sleep(2)
plot(rnorm(100), rnorm(100))
})
}
shinyApp(ui, server)
This works fine but it shows 2 loaders, one for table and other one for plot. I want to combine these 2 loaders and show only 1 loading animation which covers the entire page combining table and plot. Also loading should end only after all the calculation is done i.e after plot calculation.
I have tried putting table and plot in a div and use spinner on div but it did not work and gave a blank page.
ui <- fluidPage(
withSpinner(div(
tableOutput('data'),
plotOutput('plot')
))
)
Does anybody have a solution to this? Is this possible using some different package?
You can use the tagList() function, which creates a list of the tags allowing the shinycssloaders package to wrap all the inputs within it in one go.
So the ui will look like the following:
ui <- fluidPage(
withSpinner(tagList(tableOutput('data'),
plotOutput('plot')))
)
Just some extra information for the animation. You can all change the style of the animation such as the the type, color, size , etc by adding these as arguments to the withSpinner(). Check the withSpinner() RDocumentation.

Shiny print reactive df to console (glimpse(myreactive_df) for debugging purposes?

I'm trying to debug my Shiny app and would like to view a reactive dataframe with e.g. glimpse(df).
Initially I tried to create a breakpoint and then view the environment by my reactive df is a value not an object when used inside server.r. I also tried browser() but was not sure what it would do.
I did some searching on SO and tried various things using sink(), renderPrint() but had no success.
How can I print the contents of glimpse(some_reactive_df()) to the console when I run my app?
Calling print() from within the reactive({}) expression will do that.
library(shiny)
library(dplyr)
shinyApp(
ui <- fluidPage(
selectizeInput("cyl_select", "Choose ya mtcars$cyl ", choices = unique(mtcars$cyl)),
tableOutput("checker") # a needed output in ui.R, doesn't have to be table
),
server <- function(input, output) {
d <- reactive({
d <- dplyr::filter(mtcars, cyl %in% input$cyl_select)
print(glimpse(d)) # print from within
return(d)
})
output$checker <- renderTable({
glimpse(d()) # something that relies on the reactive, same thing here for simplicty
})
})
Assuming you provide Shiny a reason to run (and re-run) your reactive of interest, by having it be involved with a rendering in server() and linked output in ui(). This is usually the case for my debugging scenarios but it won't work unless the reactive is being used elsewhere in app.R.

Resources