I try to launch a dataframe in a shiny dashboard within a for-loop.
My purpose is to update the values in the dataframe every iteration and show this within the shiny dashboard.
For some reason the shiny dashboard is not launched at all.
Although if I select the line shinyApp(ui, server)
and press Enter, the dashboard launches correctly.
What can be the reason why the dashboard is not launching within the loop? Thanks a lot!
library(shiny)
library(shinydashboard)
library(DT)
# my dataframe:
letter <- c("a","b")
number <- c(1,2)
df <- data.frame(letter, number)
# sidebar
sidebar <- dashboardSidebar()
# body
body <- dashboardBody(
fluidRow(
box(width = 6,
DT::dataTableOutput("mytable")
)
)
)
# ui
ui <- dashboardPage(dashboardHeader(title = "hello world"),
sidebar,
body
)
# server logic
server <- function(input, output) {
output$mytable = DT::renderDataTable({
df
})
}
#launch shiny app:
for (t in 1:3) {
df[1,2] <- t
shinyApp(ui, server)
}
EDIT1
I have tried the replace the server-code with the code below although this does not give a dataframe in Shiny:
# server logic
server <- function(input, output) {
# my dataframe:
df <- reactive(
for (t in 1:300) {
data.frame(
letter=c("a","b"),
number=c(1,t)
)}
)
output$mytable <- DT::renderDataTable({
df()
})
}