Click a point on Dot-plot in Shiny R - shiny-reactivity

I am working on a Shiny App design and I wonder how to click a point on a dot-plot in Shiny R. I want to design an app that if the users select a point on the dot-plot, they can see the correspond table (3 columns ). The plot is a p-value distribution. The following is my code:
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"
)
}

Related

How to change color of mark on topoplot interactively?

I want to create interactive line- and topoplot depending on menu. I figured out how to make red the line chosen in menu, but it doesn't work for topoplot marks (black circles inside topoplot). I can change it manually (cmap[][4] = RGB{N0f8}(1.0,0.0,0.0)), but how to do that interactively?
f = Figure(backgroundcolor = RGBf(0.98, 0.98, 0.98), resolution = (1500, 700))
ax = Axis(f[1:3, 1], xlabel = "Time [s]", ylabel = "Voltage amplitude [µV]")
N = 1:length(pos) #1:4
hidespines!(ax, :t, :r)
GLMakie.xlims!(-0.3, 1.2)
hlines!(0, color = :gray, linewidth = 1)
vlines!(0, color = :gray, linewidth = 1)
times = range(-0.3, length=size(dat_e,2), step=1 ./ 128)
lines = Dict()
for i in N
mean_trial = mean(dat_e[i,:,:],dims=2)[:,1]
line = lines!(times, mean_trial, color = "black")
lines[i] = line
end
hidedecorations!(ax, label = false, ticks = false, ticklabels = false)
topo_axis = Axis(f[2, 2], width = 178, height = 178, aspect = DataAspect())
Makie.xlims!(low = -0.2, high = 1.2)
Makie.ylims!(low = -0.2, high = 1.2)
topoMatrix = eegHeadMatrix(pos[N], (0.5, 0.5), 0.5)
cmap = Observable(collect(ColorScheme(range(colorant"black", colorant"black", length=30))))
#cmap[][4] = RGB{N0f8}(1.0,0.0,0.0)
topo = eeg_topoplot!(topo_axis, N, # averaging all trial of 30 participants on Xth msec
raw.ch_names[1:30];
positions=pos, # produced automatically from ch_names
interpolation=NullInterpolator(),
enlarge=1,
#colorrange = (0, 1), # add the 0 for the white-first color
colormap = cmap[],
label_text=false)
hidedecorations!(current_axis())
hidespines!(current_axis())
num_prev = 0
menu = Menu(f[3, 2], options = raw.ch_names[1:30], default = nothing)#, default = "second")
on(menu.selection) do selected
if selected != nothing
num = findall(x->x==menu.selection[], raw.ch_names[1:30])[]
if num_prev != 0
lines[num_prev].color = "black"
cmap[][num] = RGB{N0f8}(1.0,0.0,0.0)
end
lines[num].color = "red"
cmap[][num] = RGB{N0f8}(1.0,0.0,0.0)
num_prev = num
end
end
notify(menu.selection)
#print(cmap[])
f
We solved this by putting this string at the end of the menu.selection section:
notify(lines)
It works, because lines() automatically creates Observable.

How to arrange text annotations in julia makie in one row at the top?

I want to put text annotations along a line on the top. If I write same numbers (no matter which) in y array text will be aligned across center.
let
f = Figure()
ch = ["Ch2", "Ch3", "Ch17", "Ch18", "Ch19"]
x = Array(0:100:400)
y = [100, 100, 100, 100, 100]
ax = Axis(f[1, 1])
text!(x, y, text = ch, align = (:center, :center),
offset = (0, 0),
color = :black)
f
end
but if I change one item in y array
y = [10, 100, 100, 100, 100]
this happens:
How could I put "Ch2" to the same position on the top with other annotations?
Use ylims!(ax,0,110); to configure your axis.
let
f = Figure(;ylim=[0,110])
ch = ["Ch2", "Ch3", "Ch17", "Ch18", "Ch19"]
x = Array(0:100:400)
y = [100, 100, 100, 100, 100]
ax = Axis(f[1, 1]);
text!(x, y, text = ch, align = (:center, :center),
offset = (0, 0),color = :black)
ylims!(ax,0,110)
f
end

Why my Haskell game sometimes responds differently to the same arguments? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am working on a simple Gtk/Cairo game:
https://github.com/bigos/cairo-example/blob/9ac6f39d53cd27f36cad53a265e05f3e45564a37/src/Main.hs
I have noticed a problem where sometimes my snake doesn't eat food. I have figured out how to log subsequent versions of global state.
I have this debuggator function, which tests versions dumped by the logger.
debuggator =
let m1 = Model {debugData = "", eaten = 5, foodItems = [(6,2)], gameField = Move, snakeLength = 0, heading = HeadingUp, height = 400, lastKey = 65362, scale = 25, snake = [(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0),(4,0)], tickInterval = 500.0, seed = 36, width = 600}
m2 = Model {debugData = "", eaten = 5, foodItems = [(6,2)], gameField = Move, snakeLength = 0, heading = HeadingUp, height = 400, lastKey = 65362, scale = 25, snake = [(6,2),(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0)], tickInterval = 500.0, seed = 36, width = 600}
m3 = Model {debugData = "", eaten = 5, foodItems = [(6,2)], gameField = Move, snakeLength = 0, heading = HeadingRight, height = 400, lastKey = 65363, scale = 25, snake = [(6,1),(6,2),(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0)], tickInterval = 500.0, seed = 37, width = 600}
ma = updateGlobalModel Tick m1
mb = updateGlobalModel Tick ma
in
[ma,mb]
which returns the following:
λ> debuggator
[Model {debugData = "", eaten = 5, foodItems = [(6,2)], gameField = Move, snakeLength = 0, heading = HeadingUp, height = 400, lastKey = 65362, scale = 25, snake = [(6,2),(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0)], tickInterval = 500.0, seed = 36, width = 600},
Model {debugData = "(\"** eaten **\",(6,2),[(6,2)])", eaten = 6, foodItems = [(2,5),(0,2),(10,4)], gameField = Move, snakeLength = 3, heading = HeadingUp, height = 400, lastKey = 65362, scale = 25, snake = [(6,1),(6,2),(6,3),(6,4),(5,4),(4,4),(3,4),(2,4),(1,4),(0,4),(-1,4),(-2,4),(-2,3),(-2,2),(-2,1),(-2,0),(-1,0),(0,0),(1,0),(2,0),(3,0)], tickInterval = 500.0, seed = 36, width = 600}]
In this example ma corresponds to m2 which values was copied from the real game. Processing ma leads to mb, which shows that snake has eaten the food, but in real game progressing from m2 to m3 did not lead to snake eating food. the snake head has moved over the food item and the list of food items did not change. In the repl example mb has 3 food items because the last food item was eaten and 3 more were generated.
Why does function respond differently to the same arguments? does calling a pure function from IO have some unexpected effects?
Function that checks id the snake has eaten food is at line 110:
foodEaten :: Model -> Bool
foodEaten model =
any id (map (\c -> (fst c)==cx && (snd c)==cy) (foodItems model))
where hsm = head (snake model)
cx = fst hsm
cy = snd hsm
That in turn is called by cook function at line 293:
cook :: Model -> Model
cook model =
if foodEaten model
then model { gameField = detectCollision model
, snakeLength = (snakeLength model) +3
, foodItems = filter (\c -> not (foodUnderHead c model)) (foodItems model)
, debugData = (debugData model) ++ (show ("** eaten **" :: String, head (snake model), (foodItems model)))
, eaten = (eaten model) + 1 }
else model { gameField = detectCollision model
, snakeLength = shrink (snakeLength model)
, debugData = "" }
For some reason the snake does not detect if the food was eaten. One commenter has suggested to check if my code is thread safe.
Debuggator function at line 273 tries to illustrate simple case that always works. function cook that fails in some situations is called indirectly at main function line line 295. There a timer events are defined which call updateGlobalModel Tick
_ <- GI.GLib.timeoutAdd GI.GLib.Constants.PRIORITY_DEFAULT 500 ( modifyIORef' globalModel (updateGlobalModel Tick) >>
Gtk.widgetQueueDraw canvas >> return True)
Debuggator function illustrates how I can call it in REPL. However, as someone suggested that could be a thread problem. My Haskell skills are still poor and I do not know how to investigate it properly.

Builiding a selection from a Dataframe

i want to build a selection from a dataframe based on double search-arguments. In detail:
I have build a search within a given Dataframe based on one value. This value can be given multiple times. The difference can be found in the row of the value. Here is a example:
TECHNIKART FSZ BEZEICHNUNG FIRMA PRODUKT_SPANNUNG KLIMA EW_KW MENGE_KW LEISTUNG_KW Ü
I get this Dataframe from following search in a bigger Dataframe:
key = '71E'
try:
geraet_dat2 = geraet_dat[geraet_dat.FSZ == key]
lang = len(geraet_dat2.index)
print(geraet_dat2)
My idea is now to have a if-else-query based on the length of the Dataframe. If the Dataframe has the lenght 1 i will print the values of the needed cells. If the Dataframe is longer than 1 the following popup appears:
def abfrage_selektion():
popup_selektion = Tk()
frame_selek = Frame(popup_selektion)
frame_selek.pack()
popup_selektion.configure(bg="Snow2")
popup_selektion.geometry('300x250')
popup_selektion.title('Bitte Gerät auswählen!')
LABEL_SELEKTION = LabelFrame(popup_selektion, text="Bitte Gerät auswählen: ")
LABEL_SELEKTION.place(x = 2, y = 2, width = 296, height = 246)
INFO = ttk.Label(LABEL_SELEKTION, justify=CENTER, text="Diese FSZ ist mehrfach verfügbar! \nBitte das entsprechende Gerät wählen!")
INFO.place(x = 50, y = 5)
SEPTR_1 = ttk.Separator(LABEL_SELEKTION, orient=HORIZONTAL)
SEPTR_1.place(x = 5, y = 40, width = 280)
GERAET = ttk.LabelFrame(LABEL_SELEKTION, text="Geräte: ")
GERAET.place(x = 15, y = 50, width = 260, height = 120)
LISTE = Listbox(GERAET, yscrollcommand=True)
LISTE.insert(1, '1')
LISTE.insert(2, '2')
LISTE.insert(3, '3')
LISTE.insert(4, '4')
LISTE.place(x = 5, y = 2, width = 240, height = 90)
SEPTR_2 = ttk.Separator(LABEL_SELEKTION, orient=HORIZONTAL)
SEPTR_2.place(x = 5, y = 180, width = 280)
BUTTON_OK = ttk.Button(LABEL_SELEKTION, text="OK")
BUTTON_OK.place(x = 5, y = 190, width = 280, height = 30)
that far everything is nice an clear for me.
But now the point i dont know how to solve:
In the popup i want a selection from the given Dataframe with more than one Dataset to select the wanted Data. The criteria for the selection should be 'BEZEICHNUNG' and 'PRODUKT_SPANNUNG'.
Do you have any ideas how i can do this? I searched in the web but didnt find a good solution for that.
Thank you for you help!

Ordered Probit in Jags Using scaled inverse wishart

I am trying to use the following code (adapted from the code given in Gelman and Hill's Book) to estimate a varying coefficient/intercept ordered probit model in Jags. However, it is giving me a "Observed node inconsistent with unobserved parents at initialization.Try setting appropriate initial values". Where am I going wrong? Could somebody please help me? Thanks in advance !!
rm(list=ls(all=TRUE));
options(warn=-1)
library(mvtnorm)
library(arm)
library(foreign)
library("R2jags")
library(MCMCpack)
set.seed(1)
standardizeCols = function( dataMat ) {
zDataMat = dataMat
for ( colIdx in 1:NCOL( dataMat ) ) {
mCol = mean( dataMat[,colIdx] )
sdCol = sd( dataMat[,colIdx] )
zDataMat[,colIdx] = ( dataMat[,colIdx] - mCol ) / sdCol
}
return( zDataMat )
}
keep<-1
nobs = 150;
nis<-sample(1:40,nobs,replace=T) # number obs per subject
id<-rep(1:nobs,nis)
N<-length(id)
corr_beta = 0.6;
Sigma_beta = matrix(c(1, corr_beta, corr_beta, corr_beta,
corr_beta, 1, corr_beta, corr_beta,
corr_beta, corr_beta, 1, corr_beta,
corr_beta, corr_beta, corr_beta, 1), ncol=4);
betas <- rmvnorm(n=N, mean=c(-1.45, 0.90, 0.25, -2.3), sigma=Sigma_beta);
#Generate the data
x3 = matrix(0, nrow=N,ncol=3);
y3 = matrix(0, nrow=N,ncol=1);
for (i in 1:N) {
error_v = rnorm(1,0,1);
x3[i,1] = rnorm(1,0,1);
x3[i,2] = rnorm(1,0,1);
x3[i,3] = rnorm(1,0,1);
y3[i,1] = betas[id[i], 1] + betas[id[i], 2]*x3[i,1] + betas[id[i], 3]*x3[i,2] + betas[id[i], 4]*x3[i,3] + error_v;
}
cutoff=c(-100, 0, 1.5, 2.4, 100)
k=length(cutoff)-1;
Y3<-cut(y3, br = cutoff, right=TRUE, include.lowest = TRUE, labels = FALSE)
Y3=Y3
X3=x3
m1=max(Y3)
y = as.vector( Y3 )
n = length(y)
J<-length(unique(id))
X = cbind(1, standardizeCols( X3 ))
nPred = NCOL(X)
subjects<-as.vector(as.numeric(id))
K=nPred
W <- diag (K)
# MCMC settings
ni <- 5000; nb <- 2500; nt <- 6; nc <- 3
tau1u=c(0,1,2)
jags_data <- list ("n", "J", "K", "y", "subjects", "X", "W", "m1")
inits <- function (){
list (B.raw=array(rnorm(J*K),c(J,K)), mu.raw=rnorm(K), sigma.y=runif(1), Tau.B.raw=rwish(K+1,diag(K)), xi=runif(K))
}
params <- c ("B", "mu", "sigma.B", "rho.B", "tau1u")
cat("model {
for (i in 1:n){
y.hat[i] <- inprod(B[subjects[i],],X[i,])
y[i] ~ dcat(p[i,])
estar[i]~dnorm (y.hat[i], tau.y);
for (j in 1:(m1-1)) {
Q1[i,j]<-pnorm(tau1[j]-estar[i],0,1)
}
p[i,1] <- Q1[i,1]
for(j in 2:(m1-1)) {
p[i,j] <- Q1[i,j] - Q1[i,j-1]
}
p[i,m1] <- 1 - Q1[i,m1-1]
}
tau.y <- pow(sigma.y, -2)
sigma.y ~ dunif (0, 100)
# thresholds (unordered priors)
for(j in 1:(m1-1)){
tau1u[j] ~ dnorm(0,.01)
}
# ordered thresholds
tau1 <- sort(tau1u)
for (j in 1:J){
for (k in 1:K){
B[j,k] <- xi[k]*B.raw[j,k]
}
B.raw[j,1:K] ~ dmnorm (mu.raw[], Tau.B.raw[,])
}
for (k in 1:K){
mu[k] <- xi[k]*mu.raw[k]
mu.raw[k] ~ dnorm (0, .0001)
xi[k] ~ dunif (0, 100)
}
Tau.B.raw[1:K,1:K] ~ dwish (W[,], df)
df <- K+1
Sigma.B.raw[1:K,1:K] <- inverse(Tau.B.raw[,])
for (k in 1:K){
for (k.prime in 1:K){
rho.B[k,k.prime] <- Sigma.B.raw[k,k.prime]/sqrt(Sigma.B.raw[k,k]*Sigma.B.raw[k.prime,k.prime])
}
sigma.B[k] <- abs(xi[k])*sqrt(Sigma.B.raw[k,k])
}
}", fill=TRUE, file="wishart2.txt")
# Start Gibbs sampler
outj <- jags(jags_data, inits=inits, parameters.to.save=params, model.file="wishart2.txt", n.thin=nt, n.chains=nc, n.burnin=nb, n.iter=ni)
Your initial values function returns random numbers from normal and uniform distributions, which it appears are not close enough to sensible values to allow a non-0 posterior value to be calculated. I think you need to choose your initial values more carefully, and perhaps based on the values generated in the data, to ensure that the model compiles. Do Gelman and Hill give initial values for their model that you could start with?
Update: you could also try removing your 'inits=inits' argument to allow JAGS to select its own initial values, which works for most (although not all) models. I dont use R2JAGS though so I'm not sure if this is allowed for the jags function (but it is for rjags and runjags).

Resources