Hello!
I am making an app to practice Goodness of Fit testing, and I am running into a problem with making a frequency table for the data show up in the main panel. Would you mind taking a look? As I have tried to manipulate the code, I have gotten errors saying that, "Reading from shinyoutput object is not allowed" as well as, "'data' must be 2-dimensional (e.g. data frame or matrix)." I thought I made it 2-d, but I guess not.
library(shiny)
# Define UI for application that draws a histogram, and allows for test givens
ui <- fluidPage(
# Application title
titlePanel("Goodness-of-Fit Testing Hypothesis Practice"),
# Sidebar with a slider input for sample size
sidebarLayout(
sidebarPanel(
radioButtons("distribution", label = h3("Pick the type of distribution you would like to practice"), choices = c("Poisson" = "pois", "Binomial" = "binom", "Uniform" = "unif")),
actionButton(
inputId = "submit_dist",
label = "Press for new distribution"),
numericInput("teststatistic", "What is your calculated chi-squared test statistic?", ""),
actionButton(
inputId = "submit_guess",
label = "Press to submit"),
tags$style("body{background-color: white; color: black}")
),
mainPanel(
plotOutput("distPlot"),
dataTableOutput("table1")
)
))
# Define server logic required to make a histogram
server <- function(input, output){
x_val <- eventReactive(input$submit_dist, {
seed1 <- sample.int(1000000, 1)
set.seed(seed1)
n1 <- 250
lambda1 <- sample.int(10, 1)
size1 <- sample.int(100, 1)
sampleprop <- sample(c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9), 1)
if(input$distribution == "Poisson"){
rpois(n1, lambda1)
}
else if(input$distribution == "Binomial"){
rbinom(n1, size1, sampleprop)
}
else (input$distribution == "Uniform")
{
runif(n1, max = lambda1)
}
})
output$distPlot <- renderPlot({
hist(req(x_val()),
main = "Distribution",
xlab = "Sample Values",
ylab = "Frequency Count",
col = "darkgray",
border = "white") }) #I took the breaks out because it wasn't making a good graph...here's the code I was using "breaks = ceiling(max(req(x_val())))"
#making the table
table1 <- eventReactive(output$distPlot, {
Frequency <- c(rep(NA, ceiling(max(x_val()))))
for(i in 1:ceiling(max(x_val()))){
Frequency[i] <- length(x_val[x_val() <= i & x_val() > (i-1)])
}
expvals <- rep((1/ceiling(max(x_val()))), ceiling(max(x_val())))
expvals <- expvals*n1
data.frame(c(1:length(Frequency)), Frequency, c(1:5))
})
output$table1 <- renderDataTable(req(table1()))
#eventReactive(output$distPlot, {renderDataTable(table1)})
}
# Run the application
shinyApp(ui = ui, server = server)
Thank you for your help!