Hi,
I wish to have a shiny app created where I will plot two dataframes in ggplot, then do some simple stats calcs on a variable and plot a summary table underneath it. If I leave the table calculations out then I can plot the line plots just fine and have them change based on the $input function. The problem comes when I try to have the table update based on the $input value from the ui.
Looking through past examples on Stack Overflow and YouTube, it looks like I have to define the variable in the server section before calling the output$plot and rendertable functions.
Also from those examples my understanding is that a reactive input is what I'm after, but I still get the Operation not allowed without an active reactive context error and am going in circles trying to get it to work.
library(shiny)
library(ggplot2)
# Define UI for application
ui <- fluidPage(
titlePanel("Random Dataset"),
sidebarLayout(
sidebarPanel(
h4("Input Data"),
sliderInput("pbins", "Multiplier:", min = 1,max = 5, value = 2)
),
# Show a plot of the generated distribution
mainPanel(
h4("Distribution"),
plotOutput("distPlot")
h4("Table"),
tableOutput("table")
)
)
)
# Define server logic required
server <- function(input, output) {
i <- 100
x <- runif(i)
y <- runif(i)
# z is a function of y and the slider input (1 to 5)
z <- reactive ({(y*input$pbins)})
output$distPlot <- renderPlot({
df <- data.frame("x" = x, "y" = y, "condition" = "nominal")
df2 <- data.frame("x" = x, "z" = z(), "condition" = "varied")
ggplot()+
geom_line(data = df, aes(x = x, y = y), colour = "blue") +
geom_line(data = df2, aes(x = x, y = z), colour = "red")
})
m_z <- mean(z())
sd_z <- sd(z())
results <- data.frame("0", "0")
results[1,1] = format(m_z, digits = 3)
results[1,2] = format(sd_z, digits = 4)
colnames(results) <- c("Mean z", "SD z")
rownames(results) <- c("Stats")
output$table <- renderTable(results)
}
shinyApp(ui = ui, server = server)