Hi i want to create a shiny app which will display the datatables all together (if i choose) based on a sliderinput. I displayed two as an example but as you can see they are static and not connected with the slider, I also included an example with plots in my app to show what exactly i want to achieve but im not sure how to do it with datatables.
ui.r
library(rhandsontable)
library(magrittr)
library(DT)
library(data.table)
library(kableExtra)
library(shinyBS)
shinyUI(pageWithSidebar(
headerPanel("Dynamic number of plots"),
sidebarPanel(
sliderInput("n", "Number of plots", value=1, min=1, max=5),
uiOutput("sli")
),
mainPanel(
# This is the dynamic UI for the plots
uiOutput("plots"),
dataTableOutput("hist1"),
dataTableOutput("hist2")
)
))
server.r
max_plots <- 5
shinyServer(function(input, output) {
#CORRECT PART FOR EXAMPLE
# Insert the right number of plot output objects into the web page
output$plots <- renderUI({
plot_output_list <- lapply(1:input$n, function(i) {
plotname <- paste("plot", i, sep="")
plotOutput(plotname, height = 280, width = 250)
})
# Convert the list to a tagList - this is necessary for the list of items
# to display properly.
do.call(tagList, plot_output_list)
})
# Call renderPlot for each one. Plots are only actually generated when they
# are visible on the web page.
for (i in 1:max_plots) {
# Need local so that each item gets its own number. Without it, the value
# of i in the renderPlot() will be the same across all instances, because
# of when the expression is evaluated.
local({
my_i <- i
plotname <- paste("plot", my_i, sep="")
output[[plotname]] <- renderPlot({
plot(1:my_i, 1:my_i,
xlim = c(1, max_plots),
ylim = c(1, max_plots),
main = paste("1:", my_i, ". n is ", input$n, sep = "")
)
})
})
}
### PART NEEDS FIXING
output$sli <- renderUI({
sliderInput("n", "Number of tables", value=1, min=1,step = 1, max=2)
})
DF = data.frame(
Variables = c("Revenue Growth", "Change in Gross Margin", "Change in Operating Margin"),
Lower.Bound = rep("", 3),
Upper.Bound = rep("", 3),
row.names = NULL,
stringsAsFactors = FALSE
)
#function simulating history table
make_history<-function() {
prev_rev_lb = sprintf( "%.1f%%", -5 )
prev_rev_ub = sprintf( "%.1f%%", 10 )
prev_gm_lb = format( -1, nsmall = 1 )
prev_gm_ub = format( 3, nsmall = 1 )
prev_om_lb = format( -2, nsmall = 1 )
prev_om_ub = format( 5, nsmall = 1 )
tbl3 <- data.table(Variables = c("Revenue Growth", "Change in Gross Margin", "Change in Operating Margin"),
`Lower Bound` = c( prev_rev_lb, prev_gm_lb, prev_om_lb ),
`Upper Bound` = c( prev_rev_ub, prev_gm_ub, prev_om_ub )
)
tbl3
}
History <- reactiveValues(DF1 = data.frame(), DF2 = data.frame(), DF3 = data.frame())
showHistory<-function(DF2, DF3) {
History$DF2 <- DF2
History$DF3 <- DF3
output$hist1<-renderDataTable({
if (nrow(History$DF2)!=0) {
History$DF2
}
# FIXME: else?
}, selection="none", options=list(paging=F, ordering=F, searching=F, bLengthChange=F, bFilter=F,bInfo=F)
)
output$hist2<-renderDataTable({
if(nrow(History$DF3)!=0) {
History$DF3
}
# FIXME: else?
}, selection="none", options=list(paging=F, ordering=F, searching=F, bLengthChange=F, bFilter=F,bInfo=F)
)
}
# VV: TODO: get history from the DB
df1 <- make_history()
df2 <- make_history()
showHistory(df1, df2)
})