I'd like to implement a cross-talk functionality between a table and plot in both directions:
select the row in the table which will be reflected in the plot
select a dot in the plot which will be reflected in the table. Same idea as here.
I've managed to implement a script, which works beautifully if I make scatter plot with ggplot() and table (both objects cross-talk!). However, when used EnhancedVolcano() and table I got the following error:
Error in EnhancedVolcano(toptable = data_shared, lab = "disp", x = "qsec", :
qsec is not numeric!
If I replace data_shared variable with df_orig, no error is raised, but there is no cross-talking between objects :(
Does this mean that SharedData$new() doesn't recognize numeric values as numeric? How to fix this error?
Any help is highly appreciated.
Thank you
Toy example:
library(plotly) # '4.9.1'
library(DT) # '0.11'
library(crosstalk) # ‘1.0.0’
library(EnhancedVolcano) # ‘1.4.0’
# Input
data1 = mtcars #dim(data1) # 32 11
data_shared = SharedData$new(data1) #, key = c("qsec", "hp"))
# df_orig = data_shared$origData()
# V-Plot
vp =EnhancedVolcano( toptable = data_shared,
lab = 'disp',
x = 'qsec',
y = 'hp',
xlab ='testX',
ylab = 'testY')
bscols(
ggplotly(vp + aes(x= qsec, y= -log10(hp/1000))),
datatable(data_shared, style="bootstrap", class="compact", width="100%",
options=list(deferRender=FALSE, dom='t')))
Same script, which works with ggplot():
data1 = mtcars #dim(data1) # 32 11
data_shared = SharedData$new(data1)
vp = ggplot(data = data_shared, mapping = aes(qsec, hp)) +
geom_point()
bscols(
ggplotly(vp) ,
datatable(data_shared, style="bootstrap", class="compact", width="100%",
options=list(deferRender=FALSE, dom='t')))
Note: Related (same) question was posted at BioStars, and the package author posted an answer, with author's permission copying an answer here:
Hi,
Thanks - that's very useful code and I may add it to the main package vignette, eventually.
I tried it here on my computer and I was able to get it working in my browser, but some components of the original plot seem to have been lost. I think that you just need to convert your column, 'qsec', to numerical values.
Re-using an example from my Vignette, here is a perfectly reproducible example:
library("pasilla")
pasCts <- system.file("extdata", "pasilla_gene_counts.tsv",
package="pasilla", mustWork=TRUE)
pasAnno <- system.file("extdata", "pasilla_sample_annotation.csv",
package="pasilla", mustWork=TRUE)
cts <- as.matrix(read.csv(pasCts,sep="\t",row.names="gene_id"))
coldata <- read.csv(pasAnno, row.names=1)
coldata <- coldata[,c("condition","type")]
rownames(coldata) <- sub("fb", "", rownames(coldata))
cts <- cts[, rownames(coldata)]
library("DESeq2")
dds <- DESeqDataSetFromMatrix(countData = cts,
colData = coldata,
design = ~ condition)
featureData <- data.frame(gene=rownames(cts))
mcols(dds) <- DataFrame(mcols(dds), featureData)
dds <- DESeq(dds)
res <- results(dds)
library(EnhancedVolcano)
p1 <- EnhancedVolcano(res,
lab = rownames(res),
x = "log2FoldChange",
y = "pvalue",
pCutoff = 10e-4,
FCcutoff = 2,
xlim = c(-5.5, 5.5),
ylim = c(0, -log10(10e-12)),
pointSize = c(ifelse(res$log2FoldChange>2, 8, 1)),
labSize = 4.0,
shape = c(6, 6, 19, 16),
title = "DESeq2 results",
subtitle = "Differential expression",
caption = "FC cutoff, 1.333; p-value cutoff, 10e-4",
legendPosition = "right",
legendLabSize = 14,
col = c("grey30", "forestgreen", "royalblue", "red2"),
colAlpha = 0.9,
drawConnectors = TRUE,
hline = c(10e-8),
widthConnectors = 0.5)
p1 <- p1 +
ggplot2::coord_cartesian(xlim=c(-6, 6)) +
ggplot2::scale_x_continuous(
breaks=seq(-6,6, 1))
library(plotly)
library(DT)
library(crosstalk)
bscols(
ggplotly(p1 + aes(x= log2FoldChange, y= -log10(pvalue))),
datatable(
data.frame(res),
style="bootstrap",
class="compact", width="100%",
options=list(deferRender=FALSE, dom='t')))
Unfortunately, plotly and/or bscols don't like the use of bquote(), so, one cannot have the fancy axes names that I use in EnhancedVolcano:
... + xlab(bquote(~Log[2] ~ "fold change")) + ylab(bquote(~-Log[10] ~ italic(P)))
When i try to add these, it throws an error.
Kevin
tried to modify few things in volcano function, got following error:
Error in as.data.frame.default(toptable) :
cannot coerce class ‘c("SharedData", "R6")’ to a data.frame
not sure yet, how to fix it.
Related
A new self-learner for Shiny apps, still trying to explore the structure of Shiny apps. I have a piece of code which I want to convert into Shiny apps. I wonder if someone can walk me through how the process goes. My goal is to make the figure title/subtitle dynamic based on inputs (i.e., lnHR0, p_ctl0, tfu) to function. Thanks!!!
R code:
library(tidyverse)
library(ggplot2)
WR_sim_OC <- function(n_trt, n_ctl, lnHR0, p_trt0, med_ctl, tfu, enroll, dur_boost){
#n_trt=60: number of subjects (treatment)
#n_ctl=30: number of subjects (control)
#lnHR0=log(1): Overall Survival Log Hazard Ratio
#p_trt0 = seq(0.46, 0.66, 0.01): Response Rate (Treatment)
#med_ctl=21.8: Median Overall Survival (Control)
#tfu=9: Minimum Follow-up Time
#enroll=19: Enrollment Time
#dur_boost=0: Durability Boost (percentage)
## insert real simulation code here ##
## Fake Results
results <- tibble(ORR_trt = p_trt0, avg_HR = rep(0.774, times = 4), maturity = rep(36.7, times = 4), ORR = c(20.71, 38.87, 60.61, 78.95),
WR = c(46.8, 56.0, 64.3, 72.8), WO = c(46.0, 55.0, 63.7, 71.9), OS = rep(55.0, times = 4))
## Create Operating Characteristic Figure
dat <- results %>% pivot_longer(c(ORR, WR, WO, OS), names_to = 'Method', values_to = 'POS')
out1 <- ggplot(data = dat, aes(x = ORR_trt, y = POS, group = Method)) +
geom_line(aes(color = Method), size = 1) +
geom_point(aes(color = Method), size = 2.5) +
theme(legend.position = 'bottom') +
labs(title = 'HR=1.0 Treatment vs. Control', subtitle = 'ORR in Control Arm=46%, 9mo follow-up', color = 'Method') +
ylab('Probability of Incorrect Go') +
xlab('ORR in Treatment Arm') +
ylim(0, 100)
## Output OC Table + Figure to shiny app
list(results, out1)
}
WR_sim_OC(n_trt=60, n_ctl=30, lnHR0=log(1), p_ctl0=0.46, p_trt0 = seq(0.46, 0.66, 0.06), med_ctl=21.8, tfu=9, enroll=19, dur_boost=0)
I tried writing the ui.R as follows (suppose the suv_plot is the output name), which I know is wrong. The server.R part is too hard for me... Can someone help?
fluidPage(
numericInput("lnHRO",
label = h3("ln(HRO)"),
value = log(1)),
numericInput("pctl",
label = h3("Response Rate (Control)"),
value = 0.46),
numericInput("tfu",
label = h3("Minimum Follow-up Time (Month)"),
value = 9),
hr(),
plotOutput("suv_plot")
)
My first suggestion is just to look at tutorials on shiny, they give a great overview on how to start a project: https://shiny.rstudio.com/tutorial/
I didn't know a thing about programming a few years back, so I understand it can be hard figuring out where to start, so I wanted to give you an idea of how to implement a function, and use shiny inputs to make the resulting table/plot be dynamic.
I switched up your code to be easier to reproduce for myself. I hope this gives you the starting point you need:
library(tidyverse)
library(ggplot2)
library(shiny)
WR_sim_OC <- function(MPG, CYL, DISP){
results <- mtcars%>% #Function to make a table
filter(cyl > CYL,
mpg > MPG,
disp > DISP)
out1 <- ggplot(data = results, aes(x = mpg, y = disp, group = cyl)) +
geom_line(aes(color = hp), size = 1) #Function to make a plot
list(results, out1) #List to create table and function
}
ui <- fluidPage(
numericInput("MilesPerGallon", "mpg", value = 15),
numericInput("Cylinders", "cyl", value = 4),
numericInput("Displacement", "disp", value = 200),
tableOutput("TABLE"),
plotOutput("PLOT")
)
server <- function(input, output, session) {
output$TABLE<-renderTable({
req(input$MilesPerGallon, input$Cylinders, input$Displacement) #Requires all three inputs before it makes the table
WR_sim_OC(input$MilesPerGallon, input$Cylinders, input$Displacement)[1] #Only pulling the table from the function
})
output$PLOT<-renderPlot({
req(input$MilesPerGallon, input$Cylinders, input$Displacement) #Requires all three inputs before it makes the plot
WR_sim_OC(input$MilesPerGallon, input$Cylinders, input$Displacement)[2] #Only pulling the plot from the function
})
}
shinyApp(ui, server)
Essentially on the server side where you render the plot or table, you use those inputs from the ui as the dynamic points in your function. I used req() for both of the renderTable and renderPlot to make sure the inputs are filled out before it makes the table plot. Best of luck!
I have the following data frame:
df.test <- data.frame(
id = c("EIF3H", "USP9X", "USP44", "USP51", "USP15",
"USP48", "USP47", "USP43", "USPL1", "UCHL5", "USP50", "USP7",
"UCHL1", "USP11", "USP26", "PAN2", "VCPIP1", "USP46", "USP29",
"USP22", "USP49", "ZRANB1", "OTUD4", "OTUD7B", "USP54", "PSMD14",
"USP20", "USP6", "OTUD3", "USP39", "UCHL3", "USP19", "USP21",
"USP30", "TNFAIP3", "USP17L2", "USP32", "JOSD2", "PSMD7", "ATXN3L",
"SENP2", "STAMBPL1", "USP37", "USP35", "USP3", "ALG13", "USP45",
"Control", "USP9Y", "ATXN3", "OTUD6A", "USP42", "USP12", "MPND",
"USP40", "OTUD1", "USP31", "USP8", "USP13", "USP53", "USP34",
"USP17L5", "MYSM1", "USP36", "OTUD7A", "USP10", "USP2", "USP18",
"OTUB1", "EIF3F", "USP1", "USP14", "COPS5", "USP24", "USP4",
"CYLD", "COPS6", "STAMBP", "USP5", "OTUD6B", "BAP1", "USP25",
"YOD1", "USP28", "USP38", "USP41", "JOSD1", "UCK2", "USP16",
"USP27X", "BRCC3", "USP33", "OTUD5", "OTUB2"),
log.score = c(4.22265293851218, 3.03983376346562,
2.4139305569695, 2.32586482009754, 2.30391458369018, 2.19017103893211,
2.10803347738743, 2.10011933499842, 1.82596928196197, 1.79890343496053,
1.78330640083025, 1.58384231036782, 1.4480988629484, 1.4331502122056,
1.41965675282741, 1.37552194849409, 1.37548070593268, 1.3126672736385,
1.27123241483349, 1.25213781606166, 1.1643918571801, 1.14738583497561,
1.0423927129399, 1.03157776352028, 1.0279685056071, 0.953426802337995,
0.94104282122269, 0.929925173732472, 0.886424283199432, 0.886123467368948,
0.815961921373111, 0.811437095842094, 0.767054687254773,
0.754314635766764, 0.750654863646671, 0.728646377897516,
0.707899061519581, 0.703532261199885, 0.692546751828376,
0.684554481775416, 0.652104306506768, 0.642046105413661,
0.630116510664521, 0.62908000782908, 0.619354680809075, 0.614876544107784,
0.61293067306798, 0.606898831140113, 0.603504247802433, 0.578642901486857,
0.576246380387172, 0.549612309171809, 0.53101794103743, 0.513442014568548,
0.506304999011214, 0.492144128304169, 0.462596515841992,
0.454185884038717, 0.450163300207299, 0.434529992991809,
0.429725658566606, 0.42864060724616, 0.419896514762075, 0.409715596281838,
0.365946146577929, 0.363963683646553, 0.357614629472314,
0.352851847129221, 0.343470593766502, 0.313051079788499,
0.304614649499993, 0.291604597354374, 0.287030586811975,
0.272263598289704, 0.27175988000523, 0.265200170411153, 0.264528852761016,
0.244704590019742, 0.179680291853473, 0.154102353851514,
0.147800680553723, 0.127575655021633, 0.126051956011554,
0.1207205737776, 0.118712371231544, 0.11046860245595, 0.0939775902962627,
0.0673791277640148, 0.066320409857141, 0.0582650179118847,
0.0548860857591892, 0.0374554663486737, 0.0147532091971383,
0.0134163514896924),
neg.rank = 1:94)
From this data frame I made this plot:
library(ggplot2)
x <- "neg.rank"
p <- ggplot(df.test, aes_string(x = x, y = df.test$log.score)) +
geom_point()
I want to add labels to the top10 ids and I tried the following:
library(ggrepel)
library(dplyr)
p + geom_label_repel(data = df.test[df.test[[x]] %in% 1:10, ], aes_string(x = x, y = df$log.score, label = df.test$id))
But this gives me a More than one expression parsed error:
More than one expression parsed
Backtrace:
█
1. ├─ggrepel::geom_label_repel(...)
2. │ └─ggplot2::layer(...)
3. └─ggplot2::aes_string(x = x, y = df$log.score, label = df.test$id)
4. └─base::lapply(...)
5. └─ggplot2:::FUN(X[[i]], ...)
6. └─rlang::parse_expr(x)
I have no clue what is wrong with the code.
It is not working as you are inserting the vectors directly into your aes_string.
If you want yours to be working you need to be strict with your aes_string and really should only use strings:
p +
geom_label_repel(
data = df.test[df.test[[x]] %in% 1:10, ],
aes_string(x = x, y = "log.score", label = "id"),
)
I also added a "cleaner" solution. I changed your subsetting logic to use dplyr, as you are already loading the package anyway and changed all your aes_string() to aes().
library(ggplot2)
library(ggrepel)
library(dplyr)
ggplot(df.test, aes(x = neg.rank, y = log.score)) +
geom_point() +
geom_label_repel(
data = df.test %>% slice_min(neg.rank, n = 10),
aes(label = id),
max.overlaps = 10,
xlim = c(10, NA),
ylim = c(3, NA),
direction = "x"
)
Cheers
Hannes
I'm creating a Shiny app in Rstudio and I had trouble inserting an input$char into a ggplot boxplot, where input$char was the variable for y-axis, and the x-axis was not an input variable, but instead from the data frame. The issue was that the data wouldn't appear in the graph, but the input function of the dropdown menu still changed the y-axis in the graph when the app was loaded.
Example code:
g = reactive({ggplot(data=iris, aes(x=Species, y=input$ybox))+ theme(legend.position = "top")})
output$Boxplot = renderPlot(g() + geom_boxplot(aes(fill=Species)) + scale_fill_brewer(palette
= "Set2"))
I found through another post on here that aes_string() could fix the problem, and it did. Where aes is in ggplot, I fixed the error with aes_string() and let x="Species" in the function....
except now all of my other variables are not recognized in the rest of my shiny app! Where ever there is Species or another input$char variable, they are not recognized and I receive an error. Either object 'Species' not found, or unused arguments 'input$char;,...,'Species'.
Example code that gives error:
selection = reactive({iris[, aes(c(input$xcol, input$ycol, 'Species'))]})
fit = reactive({knn3(Species ~ ., data=selection(), k = input$K)})
output$KNNplot = renderPlot({decisionplot(fit(), selection(), class=Species,main=paste('Decision Boundary for KNN (',input$K,')', sep=' '))})
Is there any way I can have both a solution to the ggplot error and for the rest of my app to still work?
Edit (Reproducible code):
library(shiny)
library(shinyWidgets)
library(lattice)
library(ggplot2)
library(RColorBrewer)
library(nnet)
library(naivebayes)
library(e1071)
library(randomForest)
library(MASS)
# function for decisionplot.R
decisionplot <- function(model, data, class = NULL, predict_type = "class",
resolution = 100, showgrid = TRUE, ...) {
if(!is.null(class)) cl <- data[,class] else cl <- 1
data <- data[,1:2]
k <- length(unique(cl))
plot(data, col = as.integer(cl)+1L, pch = as.integer(cl)+1L, ...)
# make grid
r <- sapply(data, range, na.rm = TRUE)
xs <- seq(r[1,1], r[2,1], length.out = resolution)
ys <- seq(r[1,2], r[2,2], length.out = resolution)
g <- cbind(rep(xs, each=resolution), rep(ys, time = resolution))
colnames(g) <- colnames(r)
g <- as.data.frame(g)
### guess how to get class labels from predict
### (unfortunately not very consistent between models)
p <- predict(model, g, type = predict_type)
if(is.list(p)) p <- p$class
p <- as.factor(p)
if(showgrid) points(g, col = as.integer(p)+1L, pch = ".")
z <- matrix(as.integer(p), nrow = resolution, byrow = TRUE)
contour(xs, ys, z, add = TRUE, drawlabels = FALSE,
lwd = 2, levels = (1:(k-1))+.5)
invisible(z)
}
ui = fluidPage(
navlistPanel(
tabPanel("Descriptive Statistics",
fluidRow(
column(6,
wellPanel(selectInput('ybox', 'Y Variable', names(iris)[1:4], selected = names(iris)[[2]])),
plotOutput('Boxplot'))),
fluidRow(
column(12,
wellPanel(selectInput('xdens', 'X Variable', names(iris)[1:4], selected= names(iris)[2])),
plotOutput("Densplot")))),
tabPanel("Naive Bayes Classifier",
wellPanel(selectInput('xcol5', 'X Variable', names(iris)[1:4], selected = names(iris)[[1]]),
selectInput('ycol5', 'Y Variable', names(iris)[1:4], selected = names(iris)[[2]])),
plotOutput('NBplot'))
))
server = function(input, output) {
# Boxplot
g = reactive({ggplot(data=iris, aes_(x='Species', y=iris[[as.name(input$ybox)]]))+ theme(legend.position = "top")})
output$Boxplot = renderPlot(g() + geom_boxplot(aes(fill=Species)) + scale_fill_brewer(palette = "Set2") + ylab(input$ybox))
# Densityplot
c = reactive({ggplot(iris, aes_(iris[[as.name(input$xdens)]]))})
mu = reactive({ddply(iris, "Species", summarise, grp.mean=mean(c()))})
dc = reactive({c() + geom_density(kernel="gaussian", aes(color=Species, fill=Species), alpha=0.4) +
geom_vline(data=mu(), aes(xintercept=mu()$grp.mean, color=Species),linetype="dashed") +
theme(legend.position = "top") + xlab(input$xdens)})
output$Densplot = renderPlot({dc() + scale_color_brewer(palette="Dark2") + scale_fill_brewer(palette = "Dark2")})
#example model
selection5 = reactive({iris[, c(input$xcol5, input$ycol5, 'Species')]})
fit5 = reactive({naive_bayes(Species ~ ., data=selection5(), usekernel = T)})
output$NBplot = renderPlot({decisionplot(fit5(), selection5(), class = "Species",
main = "Decision Boundary for Naive Bayes Classifier")})
}
shinyApp(ui = ui, server = server)
Your selection5 was an issue. The following code gives a reactive data frame.
#example model
selection5 <- reactive({
# df <- iris[, c(input$xcol5, input$ycol5, 'Species')] ## this call does not work
df <- data.frame(x=iris[[input$xcol5]], y=iris[[input$ycol5]], Species=iris[, "Species"])
})
For this particular shiny example I am trying to apply a circular model and display and summarize it within the ggplot and a summary table. This is straightforward up until trying to add in reactive 'brushplot' capabilities. Each of the data points represent a date and the point of the selective graph is to be able to discard undesirable dates. As far as I've figured out, this requires the filtering and model fitting to be within a renderPlot which then leads to complications (unable to find the data/model) trying to call the filtered data and the circular model's statistical outputs outside the function and/or within another reactive function. This yields the Error: object 'k_circ.lm' not found So my questions are:
How can I read the filtered data from the renderPlot function
to the summarytable matrix?
How could I similarly add the fitted model values and residuals from k_circ.lm?
Is there a better or simpler way to arrange app to avoid this?
Alternatative code lines are commented out for a working (if poorly formatted) summary table.
library(dplyr) # For data manipulation
library(ggplot2) # For drawing plots
library(shiny) # For running the app
library(plotly) # For data manipulation
library(circular) # For Circular regressions
library(gridExtra)
# Define UI ----
ui <- fluidPage(
# App title ----
titlePanel("Circular Brushplot Demo"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
sidebarPanel(
actionButton("exclude_toggle", "Toggle points"),
actionButton("exclude_reset", "Reset")
),
# Main panel for displaying outputs ----
mainPanel(
#reactive plot output with point and 'brush' selection
fluidRow(plotOutput("k", height = 400,
click = "k_click",
brush = brushOpts(
id = "k_brush" ))),
plotOutput("s", height = 400)
)
)
)
# Define server logic
server <- function(input, output) {
psideg <- c(356,97,211,232,343,292,157,302,335,302,324,85,324,340,157,238,254,146,232,122,329)
thetadeg <- c(119,162,221,259,270,29,97,292,40,313,94,45,47,108,221,270,119,248,270,45,23)
## Data in radians then to "circular format"
psirad <- psideg*2*pi/360
thetarad <- thetadeg*2*pi/360
cpsirad <- circular(psirad)
cthetarad <- circular(thetarad)
cdat <- data.frame(cpsirad, cthetarad)
###### reactive brush plot ########
# For storing which rows have been excluded
vals <- reactiveValues(
keeprows = rep(TRUE, nrow(cdat)))
output$k <- renderPlot({
# Plot the kept and excluded points as two separate data sets
keep <- cdat[ vals$keeprows, , drop = FALSE]
exclude <- cdat[!vals$keeprows, , drop = FALSE]
## Fits circular model specifically for 'keeprows' of selected data
k_circlm <- lm.circular(type = "c-c", y = keep$cthetarad, x = keep$cpsirad, order = 1)
k_circlm
ggplot(keep, aes(cthetarad, cpsirad)) +
geom_point(aes(cthetarad, cpsirad, colour = keep$Vmag, size = 5))+
scale_colour_gradient(low ="blue", high = "red")+
geom_smooth(method = lm, fullrange = TRUE, color = "black") +
geom_point(data = exclude, shape = 13, size = 5, fill = NA, color = "black", alpha = 0.25) +
annotate("text", x = min(keep$cthetarad), y = Inf, hjust = .1, vjust = 1,
label = paste0("p value 1 = ", round(k_circlm$p.values[1], 2)), size = 7)+
annotate("text", x = min(keep$cthetarad), y = Inf, hjust = .1, vjust = 2.5,
label = paste0("p value 2 = ", round(k_circlm$p.values[2], 2)), size = 7)+
annotate("text", x = min(keep$cthetarad), y = Inf, hjust = .1, vjust = 4,
label = paste0("rho = ", round(k_circlm$rho, 2)), size = 7)+
xlab("Lighthouse Direction (radians)")+ ylab("ADCP site direction (radians)")+
theme(axis.title.x = element_text(size = 20), axis.title.y = element_text(size = 20))
})
# Toggle points that are clicked
observeEvent(input$k_click, {
res <- nearPoints(cdat, input$k_click, allRows = TRUE)
vals$keeprows <- xor(vals$keeprows, res$selected_)})
# Toggle points that are brushed, when button is clicked
observeEvent(input$exclude_toggle, {
res <- brushedPoints(cdat, input$k_brush, allRows = TRUE)
vals$keeprows <- xor(vals$keeprows, res$selected_)})
# Reset all points
observeEvent(input$exclude_reset, {
vals$keeprows <- rep(TRUE, nrow(cdat))})
output$s <- renderPlot({
# Create Summary table
summarytable <- data.frame(matrix(ncol = 4, nrow = nrow(keep)))
colnames(summarytable) <- c( "Psi_dir", "Theta_dir", "Fitted_values", "Residuals")
# Un-comment lines below to read from non-reactive data for working summary table
#summarytable$Psi_dir <- round(cdat$cpsirad, 2)
#summarytable$Theta_dir <- round(cdat$cthetarad, 2)
# attempting to pull from circlm within render plot
# comment out for summarytable to work
summarytable$Psi_dir <- round(keep$cpsirad, 2)
summarytable$Theta_dir <- round(keep$cthetarad, 2)
summarytable$Fitted_values <- round(k_circ.lm$fitted)
summarytable$Residuals <- round(k_circ.lm$residuals)
# outputing table with minimal formatting
summarytable <-na.omit(summarytable)
t <- tableGrob(summarytable)
Q <- grid.arrange(t, nrow = 1)
Q
}
)
}
shinyApp(ui = ui, server = server)
Here's a few ideas - but there are multiple approaches to handling this, and you probably want to restructure your server function a bit more after working with this further.
First, you probably want a reactive expression that will update your model based on vals$keeprows as this changes with your clicks. Then, you can access the model results from this expression from both your plot and data table.
Here is an example:
fit_model <- reactive({
## Keep and exclude based on reactive value keeprows
keep = cdat[ vals$keeprows, , drop = FALSE]
exclude = cdat[!vals$keeprows, , drop = FALSE]
## Fits circular model specifically for 'keeprows' of selected data
k_circlm <- lm.circular(type = "c-c", y = keep$cthetarad, x = keep$cpsirad, order = 1)
## Returns list of items including what to keep, exclude, and model
list(k_circlm = k_circlm, keep = keep, exclude = exclude)
})
It will return a list that you can access from the plot:
output$k <- renderPlot({
exclude <- fit_model()[["exclude"]]
keep <- fit_model()[["keep"]]
k_circlm <- fit_model()[["k_circlm"]]
ggplot(keep, aes(cthetarad, cpsirad)) +
...
And can access the same from your table (though you have as renderPlot?):
output$s <- renderPlot({
keep = fit_model()[["keep"]]
k_circ.lm <- fit_model()[["k_circlm"]]
# Create Summary table
summarytable <- data.frame(matrix(ncol = 4, nrow = nrow(keep)))
...
Note that because the table length changes with rows kept, you might want to use nrow(keep) as I have above, rather than nrow(cdat), unless I am mistaken.
I also loaded gridExtra library for testing this.
I suspect there are a number of other improvements you could consider, but thought this might help you get to a functional state first.
I have made a loop for making multiply plots, however i have no way of saving them, my code looks like this:
#----------------------------------------------------------------------------------------#
# RING data: Mikkel
#----------------------------------------------------------------------------------------#
# Set working directory
setwd()
#### Read data & Converting factors ####
dat <- read.table("Complete RING.txt", header =TRUE)
str(dat)
dat$Vial <- as.factor(dat$Vial)
dat$Line <- as.factor(dat$Line)
dat$Fly <- as.factor(dat$Fly)
dat$Temp <- as.factor(dat$Temp)
str(dat)
datSUM <- summaryBy(X0.5_sec+X1_sec+X1.5_sec+X2_sec+X2.5_sec+X3_sec~Vial_nr+Concentration+Sex+Line+Vial+Temp,data=dat, FUN=sum)
fl<-levels(datSUM$Line)
colors = c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3")
meltet <- melt(datSUM, id=c("Concentration","Sex","Line","Vial", "Temp", "Vial_nr"))
levels(meltet$variable) <- c('0,5 sec', '1 sec', '1,5 sec', '2 sec', '2,5 sec', '3 sec')
meltet20 <- subset(meltet, Line=="20")
meltet20$variable <- as.factor(meltet20$variable)
AllConcentrations <- levels(meltet20$Concentration)
for (i in AllConcentrations) {
meltet.i <- meltet20[meltet20$Concentration ==i,]
quartz()
print(dotplot(value~variable|Temp, group=Sex, data = meltet.i ,xlab="Time", ylab="Total height pr vial [mm above buttom]", main=paste('Line 20 concentration ', meltet.i$Concentration[1]),
key = list(points = list(col = colors[1:2], pch = c(1, 2)),
text = list(c("Female", "Male")),
space = "top"), col = colors, pch =c(1, 2))) }
I have tried with the quartz.save function, but that just overwrites the files. Im using a mac if that makes any difference.
When I want to save multiple plots in a loop I tend to do something like...
for(i in AllConcentrations){
meltet.i <- meltet20[meltet20$Concentration ==i,]
pdf(paste("my_filename", i, ".pdf", sep = ""))
dotplot(value~variable|Temp, group=Sex, data = meltet.i ,xlab="Time", ylab="Total height pr vial [mm above buttom]", main=paste('Line 20 concentration ', meltet.i$Concentration[1]),
key = list(points = list(col = colors[1:2], pch = c(1, 2)),
text = list(c("Female", "Male")),
space = "top"), col = colors, pch =c(1, 2))
dev.off()
}
This will create a pdf file for every level in AllConcentrations and save it in your working directory. It will paste together my_filename, the number of the iteration i, and then .pdf together to make each file unique. Of course, you will want to adjust height and width in the pdf function.