我正在开发一个Shiny App设计,我想知道如何在Shiny R中的点图上点击一个点。我想设计一个应用程序,如果用户在点图上选择一个点,他们可以看到对应表(3列)。该图是p值分布。以下是我的代码:
ui:
library(shiny)
library(shinythemes)
library(pander)
fluidPage(theme = shinytheme("flatly"),
titlePanel(h3("Chi-Squared Goodness-fit-Test and Simulation ")),
fluidRow(
column(3, offset = 0.5,wellPanel(
sliderInput("n", "Number of Samples:", min = 10, max = 1000, value = 50 ,
step = 1),
sliderInput("n2", "The number of Categories:", min = 1, max = 8, value = 5 ,
step = 1) ,
sliderInput("n3", "The number of Simulation:", min = 1, max = 1000, value = 5 ,
step = 1),
submitButton("Submit")
)),
column(7,align="center", tableOutput("values")),
column(5,offset=1, align="center",
plotOutput("plot1", click=" Click" ,width = 600, height = 430)
)
)
)
server:
library(shiny)
library(shinythemes)
library(pander)
function(input, output) {
output$plot1 <- renderPlot({
num_of_samples = input$n
nn= input$n2
ss= input$n3
pp=numeric(0)
for(i in 1:ss){
x <- sample(1:nn,num_of_samples,replace=T)
nulls=1/(1:nn)
total=table(x)
expected=nulls*total
a <- chisq.test(table(x), correct= FALSE, rescale.p=TRUE )
pp[i]=a$p.value
}
if (ss<=50) {stripchart(pp,method = "stack",offset = 0.8, at = .15, pch = 19,
main="P-value Distribution of Chi-Squared Test", xlab="P Value")}
else {hist(pp,breaks=5,main="P-value Distribution of Chi-Squared Test", xlab="P Value")}
})
sliderValues <- reactive({
num_of_samples = input$n
nn= input$n2
# pp=numeric(0)
x <- sample(1:nn,num_of_samples,replace=T)
# Compose data frame
xx=cbind(paste0(LETTERS[1:nn]),table(x ),round(rep(num_of_samples/nn,nn),2))
xx=as.data.frame(xx)
colnames(xx)=c("Categories","Observed Value","Expected Value")
xx
})
output$values <- renderTable({
sliderValues()},
align="c"
)
}