checkboxInput in R shiny - r

I have a question about the checkboxInput in R shiny. When it is checked, the scatter plot should be colorful while when it is unchecked, the plot should be black. I have tried several methods, but it keeps colorful no matter whether it is checked or not. Could you please help me with fix the code? Thanks so much.
library(shiny)
library(dplyr)
library(ggplot2)
# Start a 'Shiny' part
shinyServer(function(input, output, session) {
# Create a new reactive variable
newVar <- reactive({
newData <- msleep %>% filter(vore == input$vore)
})
# Create a scatter plot
output$sleepPlot <- renderPlot({
newDat <- newVar()
g <- ggplot(newDat, aes(x = bodywt, y = sleep_total))
g + geom_point(size = input$size, aes(col = conservation))
})
# Create text info
output$info <- renderText({
newDat <- newVar()
paste("The average body weight for order", input$vore, "is", round(mean(newDat$bodywt, na.rm = TRUE), 2),
"and the average total sleep time is", round(mean(newDat$sleep_total, na.rm = TRUE), 2), sep = " ")
})
# Create output of observations
output$table <- renderTable({
newDat <- newVar()
newDat
})
})
library(ggplot2)
shinyUI(fluidPage(
# Application title
titlePanel("Investigation of Mammal Sleep Data"),
# Sidebar with options for the data set
sidebarLayout(
sidebarPanel(
h3("Select the mammal's biological order:"),
selectizeInput("vore", "Vore", selected = "omni",
choices = levels(as.factor(msleep$vore))),
br(),
sliderInput("size", "Size of Points on Graph",
min = 1, max = 10, value = 5, step = 1),
checkboxInput("conservation", h4("Color Code Conservation Status", style = "color:red;"))
),
# Show output
mainPanel(
plotOutput("sleepPlot"),
textOutput("info"),
tableOutput("table")
)
)
))

Try this
# Create a scatter plot
output$sleepPlot <- renderPlot({
newDat <- newVar()
colorme <- unique(newVar()$conservation)
ncolor <- length(colorme)
if (!input$conservation) {
mycolor <- c(rep("black",ncolor))
mylabels <- c(rep(" ",ncolor))
}
g <- ggplot(newDat, aes(x = bodywt, y = sleep_total)) +
geom_point(size = input$size, aes(col = conservation)) +
{if (!input$conservation) scale_color_manual(name=" ", values=mycolor, labels=mylabels)} +
{ if (!input$conservation) guides(color='none')}
g
})
You can adjust, as necessary.

Related

How to create a clickable histogram in Shiny?

I want to create a clickable histogram in shiny but I don't know if it is possible.
Some months ago I saw a clickable volcano plot which gives you a table of what you click.
Source: https://2-bitbio.com/2017/12/clickable-volcano-plots-in-shiny.html
The closest post that I found about creating clickable histograms is this one Click to get coordinates from multiple histogram in shiny
However, I don't want to get the coordinates. I want the rownames of the dataframe.
Having this dataframe, can I get the rownames everytime I click a bar from the histogram?
mtcars <- mtcars %>%
select("hp")
mtcars <- as.matrix(mtcars)
One example (but not clickable) in shiny:
library(shiny)
library(ggplot2)
library(scales)
library(dplyr)
ui <- fluidPage(
titlePanel("Histogram"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
plotOutput("hist"),
)
)
)
mtcars <- mtcars %>%
select("hp")
mtcars <- as.matrix(mtcars)
server <- function(input, output) {
output$hist <- renderPlot({
pp <- qplot(mtcars, geom = "histogram", bins = 10, xlab="values",
ylab="Frequency", main="Histogram",
fill=I("red"), col=I("black"), alpha=I(0.4))
pp + scale_x_continuous(breaks=pretty(mtcars, n=10))
})
}
shinyApp(ui = ui, server = server)
Does anyone know how to do it?
Thanks very much in advance!
Regards
This is a great question, and what makes it challenging is that the qplot/ggplot charts are static images. The below app.r is an example of how I would do it. I'd love to see other approaches.
In essence:
Create a sequence of numbers that will be used both as the breaks in your histogram and as intervals in your dataframe. I based these on user inputs, but you could hardcode them.
Assign a "bin" value to each row in the dataframe based on the interval in which the value falls.
Record the x-coordinate from the user's click event and assign that a "bin" value based on the same set of intervals.
Subset your dataframe and retain only those records where the "bin" value of the data matches the "bin" value of the x-coordinate from the user's click event.
Otherwise, if you're willing to go the d3 route, you could explore something like this posted by R Views.
#Load libraries ----------------------------------------------------
library(shiny)
library(ggplot2)
library(scales)
library(dplyr)
# Prepare data -----------------------------------------------------
df <- mtcars
df <- cbind(model = rownames(df), data.frame(df, row.names = NULL)) # setting the rownames as the first column
dm <- df$hp %>% as.matrix()
# UI function ------------------------------------------------------
ui <- fluidPage(
titlePanel("Histogram"),
sidebarLayout(
sidebarPanel(
tags$h5("I added the below text output only to demonstrate shiny's way for tracking user interaction on static plots. You can click, double-click, or click & drag (i.e. brushing). These functions are AWESOME when exploring scatterplots."),
tags$h3("Chart click and brushing"),
verbatimTextOutput("info"),
tags$h5("Now I'm applying the below UI inputs to the `vec` and `breaks` arguments in `findInterval()` and `qplot()` respectively; I'm using `findInterval()` to bin the values in the dataframe AND to bin the x-value of the user's click event input on the chart. Then we can return the dataframe rows with the same bin values as the x-value of the click input."),
sliderInput("seq_from_to"
, label = h3("Sequence 'From' and 'To'")
, min = 0
, max = 500
, value = c(50, 350)
),
sliderInput("seq_by"
, label = h3("Sequence 'By'")
, min = 25
, max = 200
, value = 50
, step = 5)
),
mainPanel(
plotOutput("hist",
click = "plot_click",
dblclick = "plot_dblclick",
hover = "plot_hover",
brush = "plot_brush"),
dataTableOutput("table")
)
)
)
# Server function --------------------------------------------------
server <- function(input, output) {
# Render Histogram Plot
output$hist <- renderPlot({
# Using the same `qplot` function but inserting the user inputs to set the breaks values in the plot
pp <- qplot(dm
, geom = "histogram"
, breaks = seq(from = input$seq_from_to[1], to = input$seq_from_to[2], by = input$seq_by)
, xlab = "values"
, ylab = "Frequency"
, main = "Histogram"
, fill = I("red")
, col = I("black")
, alpha = I(0.4)
)
# Also using the user inputs to set the breaks values for the x-axis
pp + scale_x_continuous(breaks = seq(from = input$seq_from_to[1], to = input$seq_from_to[2], by = input$seq_by))
})
# This is purely explanatory to help show how shiny can read user interaction on qplot/ggplot objects
# It's taken from the Shiny docs here: https://shiny.rstudio.com/articles/plot-interaction.html
output$info <- renderText({
# Retain the x and y coords of the user click event data
xy_str <- function(e) {
if(is.null(e)) return("NULL\n")
paste0("x=", round(e$x, 1), " y=", round(e$y, 1), "\n")
}
# Retain the x and y range coords of click & drag (brush) data
xy_range_str <- function(e) {
if(is.null(e)) return("NULL\n")
paste0("xmin=", round(e$xmin, 1), " xmax=", round(e$xmax, 1),
" ymin=", round(e$ymin, 1), " ymax=", round(e$ymax, 1))
}
# Paste this together so we can read it in the UI function for demo purposes
paste0(
"click: ", xy_str(input$plot_click),
"dblclick: ", xy_str(input$plot_dblclick),
"hover: ", xy_str(input$plot_hover),
"brush: ", xy_range_str(input$plot_brush)
)
})
# Back to the story. Set a listener to trigger when one of the following is updated:
toListen <- reactive({list(
input$plot_click # user clicks on the plot
, input$seq_from_to # user updates the range slider
, input$seq_by # user updates the number input
)
})
# When one of those events are triggered, update the datatable output
observeEvent(toListen(), {
# Save the user click event data
click_data <- input$plot_click
print(click_data) # during your app preview, you can watch the R Console to see what click data is accessible
# Assign bin values to each row using the intervals that are set by the user input
df$bin <- findInterval(dm, vec = seq(from = input$seq_from_to[1], to = input$seq_from_to[2], by = input$seq_by))
# Similarly assign a bin value to the click event based on what interval the x values falls within
click_data$x_bin <- findInterval(click_data$x, vec = seq(from = input$seq_from_to[1], to = input$seq_from_to[2], by = input$seq_by))
# Lastly, subset the df to only those records within the same interval as the click event x-value
df_results <- subset(df, bin == click_data$x_bin)
# Select what values to view in the table
df_results <- df_results %>% select(model, hp)
# And push these back out to the UI
output$table <- renderDataTable(df_results,
options = list(
pageLength = 5
)
)
})
}
shinyApp(ui = ui, server = server)
Well, someone answered. Since I took the time to put it together, here is another potential solution.
library(shiny)
library(ggplot2)
library(scales)
library(dplyr)
library(DescTools) # added for Closest()
ui <- fluidPage(
titlePanel("Histogram"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
plotOutput("hist", click = 'plot_click'), # added plot_click
verbatimTextOutput("x_value"), # added queues for interactivity
verbatimTextOutput("selected_rows") # added table for bin values
)
)
)
# this can be a dataframe or matrix for qplot or ggplot
# (not sure if there was another reason you had this code?)
# mtcars <- mtcars %>%
# select("hp") # if you only want hp
# mtcars <- as.matrix(mtcars) # I suggest making row names a column
# to keep 2 columns
pp <- ggplot(mtcars) +
geom_histogram(aes(x = hp),
bins = 10,
fill = "red",
color = "black",
alpha = .4) +
labs(x = "values",
y = "Frequency",
title = "Histogram")
# extract data from plot to find where each value falls within the histogram bins
# I kept the pkg name, function in more than one library
bd <- ggplot_build(ggplot2::last_plot())$data[[1]]
# add the assigned bin number to the mtcars frame; used for filtering matches
mtcars$bins <- lapply(mtcars$hp,
function(y) {
which(bd$x == Closest(bd$x, y))
}) %>% unlist()
server <- function(input, output) {
output$hist <- renderPlot({
# moved the plot outside of server, so that global variables could be created
# pp <- qplot(mtcars[,"hp"], geom = "histogram", bins = 10, xlab="values",
# ylab = "Frequency", main = "Histogram",
# fill = I("red"), col = I("black"), alpha = I(0.4))
# scale_x_continuous(breaks=pretty(mtcars, n=10)) # can't use this
pp
})
# # Print the name of the x value # added all that's below with server()
output$x_value <- renderPrint({
if (is.null(input$plot_click$x)) return()
# find the closest bin center to show where the user clicked on the histogram
cBin <- which(bd$x == Closest(bd$x, input$plot_click$x))
paste0("You selected bin ", cBin) # print out selected value based on bin center
})
# Print the rows of the data frame which match the x value
output$selected_rows <- renderPrint({
if (is.null(input$plot_click$x)) return()
# find the closest bin center to show where the user clicked on the histogram
cBin <- which(bd$x == Closest(bd$x, input$plot_click$x))
mtcars %>% filter(bins == cBin)
# mtcars
})
}
shinyApp(ui = ui, server = server)
Just in case someone ends in this post looking a way to include brushedPoints... inspired on this post, I found a way to do it!
Code:
#Load libraries ----------------------------------------------------
library(shiny)
library(ggplot2)
library(scales)
library(dplyr)
# Prepare data -----------------------------------------------------
df <- mtcars
df <- cbind(model = rownames(df), data.frame(df, row.names = NULL)) # setting the rownames as the first column
breaks_data = pretty(mtcars$hp, n=10)
my_breaks = seq(min(breaks_data), to=max(breaks_data), by=30)
# UI function ------------------------------------------------------
ui <- fluidPage(
titlePanel("Histogram"),
sidebarLayout(
sidebarPanel(
actionButton("draw_plot", "Draw the plot")
),
mainPanel(
plotOutput("hist",
brush = brushOpts("plot_brush", resetOnNew = T, direction = "x")),
dataTableOutput("table"),
)
)
)
# Server function --------------------------------------------------
server <- function(input, output) {
observeEvent(input$plot_brush, {
info_plot <- brushedPoints(df, input$plot_brush)
output$table <- renderDataTable(info_plot)
})
# If the user didn't choose to see the plot, it won't appear.
output$hist <- renderPlot({
df %>% ggplot(aes(hp)) +
geom_histogram(alpha=I(0.4), col = I("black"), fill = I("red"), bins=10) +
labs(x = "values",
y = "Frequency",
title = "Histogram") +
scale_x_continuous(breaks = my_breaks)
})
}
shinyApp(ui = ui, server = server)
How to do a scatterplot with hover
library(shiny)
library(tidyverse)
ui <- fluidPage(
titlePanel("hover tooltips demo"),
mainPanel(
plotOutput("plot1", hover = hoverOpts(id = "plot_hover", delay = 100, delayType = "debounce")),
uiOutput("hover_info") # , style = "pointer-events: none")
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
mtcars %>%
ggplot(aes(mpg, hp)) +
geom_point()
})
output$hover_info <- renderUI({
hover <- input$plot_hover
point <- shiny::nearPoints(mtcars,
coordinfo = hover,
xvar = 'mpg',
yvar = 'hp',
threshold = 20,
maxpoints = 1,
addDist = TRUE)
if (nrow(point) == 0) return(NULL)
style <- paste0("position:absolute; z-index:100; background-color: #3c8dbc; color: #ffffff;",
"font-weight: normal; font-size: 11pt;",
"left:", hover$coords_css$x + 5, "px;",
"top:", hover$coords_css$y + 5, "px;")
wellPanel(
style = style,
p(HTML(paste0("Some info about car: <br/>MPG ", point$mpg, "<br/>HP ", point$hp)))
)
})
}
shinyApp(ui = ui, server = server)

alignment of ggplots in a modularized shiny app

I have a modularized shiny app that displays in real time 4 variables. The monitor module takes as an input one big data frame, and displays the signal assigned. The 4 modules are stacked together in the ui:
tabBox(id = "monitoring_tabBox",
height = monitor_height_px,
width = "500px",
tabPanel(id = "layout1",
title = "Layout 1",
monitorModuleUI("sbto2"),
monitorModuleUI("icp"),
monitorModuleUI("map"),
monitorModuleUI("ptio2")
)
),
The problem is the following: the plots are not perfectly aligned across modules. Mainly because the y ticks values have different ranges (see how the icp and the ptio2 are aligned because both signals have two digits, without decimals)
I've seen several techniques to align ggplots but you need to take as an input the 4 plots, and then merge them all in a single plot / render. I would like to avoid this step to keep the modularized structure of the app.
Is there any way I can align those plots without having to marge them together? (i.e by constraining the length of the y ticks)
Thank you in advance !
Screenshot:
screenshot showing the 4 modules and the misalignment issue
Reproducible example:
library(shiny)
library(dplyr)
library(ggplot2)
library(tidyr)
# Sample Data
df <- data.frame(timestamp = seq(as.POSIXct("2020-06-01 10:00:00"), as.POSIXct("2020-06-01 12:00:00"), "1 min"),
sbto2 = round(10000*rnorm(121, 0, 2), 1),
map = round(100*rnorm(121, 0, 2), 1),
icp = round(10*rnorm(121, 0, 1.5), 1),
ptio2 = round(1000*rnorm(121, 0, 1.2), 1))
# monitorModule
monitorModuleUI <- function(id){
ns <- NS(id)
fluidRow(
column(8,
plotOutput(ns("monitoring_plot"),
height = "150px")
),
column(2,
uiOutput(ns("monitoring_text"))
)
)
}
monitorModule <- function(input, output, server, variable_name, df){
output$monitoring_plot <- renderPlot({
p()
})
p <- reactive({
df %>%
gather("var", "value",seq(2,5)) %>%
filter(var == variable_name) %>%
ggplot(aes(x = timestamp, y = value)) + geom_line() -> p
return(p)
})
output$monitoring_text <- renderUI({
value <- p()$data$value[nrow(p()$data)]
h1(strong(paste(value)), style = "font-size:90;")
})
}
# APP
ui <- shinyServer(fluidPage(
monitorModuleUI("sbto2"),
monitorModuleUI("icp"),
monitorModuleUI("ptio2"),
monitorModuleUI("map")
))
server <- shinyServer(function(input, output, session){
callModule(monitorModule, "sbto2", "sbto2", df)
callModule(monitorModule, "icp", "icp", df)
callModule(monitorModule, "ptio2", "ptio2", df)
callModule(monitorModule, "map", "map", df)
})
shinyApp(ui=ui,server=server)
One alternative would be to return a reactive plot from each module and then to organize them with the package {patchwork}.
Here's an example:
library(shiny)
library(dplyr)
library(ggplot2)
library(tidyr)
library(patchwork)
# Sample Data
df <- data.frame(timestamp = seq(as.POSIXct("2020-06-01 10:00:00"), as.POSIXct("2020-06-01 12:00:00"), "1 min"),
sbto2 = round(10000*rnorm(121, 0, 2), 1),
map = round(100*rnorm(121, 0, 2), 1),
icp = round(10*rnorm(121, 0, 1.5), 1),
ptio2 = round(1000*rnorm(121, 0, 1.2), 1))
# monitorModule
monitorModuleUI <- function(id){
# ns <- NS(id)
# plotOutput(ns("monitoring_plot"),
# height = "150px")
}
monitorModule <- function(input, output, server, variable_name, df){
test <- reactive({
df %>%
gather("var", "value",seq(2,5)) %>%
filter(var == variable_name) %>%
ggplot(aes(x = timestamp, y = value)) + geom_line() -> p
return(p)
})
}
# APP
ui <- fluidPage(
monitorModuleUI("sbto2"),
monitorModuleUI("icp"),
monitorModuleUI("ptio2"),
monitorModuleUI("map"),
plotOutput("all_plots")
)
server <- function(input, output, session){
plot_1 <- callModule(monitorModule, "sbto2", "sbto2", df)
plot_2 <- callModule(monitorModule, "icp", "icp", df)
plot_3 <- callModule(monitorModule, "ptio2", "ptio2", df)
plot_4 <- callModule(monitorModule, "map", "map", df)
output$all_plots <- renderPlot({
plot_1() / plot_2() / plot_3() / plot_4()
})
}
shinyApp(ui=ui,server=server)

Adding multiple reactive geom_lines to Shiny figure

I'd like to include the reactive outputs of two data sets as different geom_lines in the same ggplotly figure. The code runs as expected when only one reactive data.frame is included as a geom_line. Why not two?
ui <- fluidPage(
sidebarLayout(
selectInput("Var1",
label = "Variable", #DATA CHOICE 1
selected = 10,
choices = c(10:100)),
selectInput("Var1",
label = "Variable2", #DATA CHOICE 2
selected = 10,
choices = c(10:100))
# Show a plot of the generated distribution
),
mainPanel(
plotlyOutput('plot') #Draw figure
)
)
server <- function(input, output) {
out <- reactive({
data.frame(x = rnorm(input$Var1), #Build data set 1
y = 1:input$Var1)
})
out2 <- reactive({
data.frame(x = rnorm(input$Var2), #Build data set 2
y = 1:input$Var2)
})
output$plot <- renderPlotly({
p <- ggplot() +
geom_line(data = out(), aes(x = x, y = y)) #Add both data sets in one ggplot
geom_line(data = out2(), aes(x = x, y = y), color = "red")
ggplotly(p)
})
}
# Run the application
shinyApp(ui = ui, server = server)
When you put the data into long format and give each group a group identifier it seems to work. Note that you should be able to change sliderInput back to selectInput - this was one of the entries I toggled during testing, but the choice of UI widget should not matter.
This works -- code can be simplified inside the reactive from here:
library(plotly)
ui <- fluidPage(
sidebarLayout(
sliderInput("Var1",
label = "Variable", #DATA CHOICE 1
min=10, max=100, value=10),
sliderInput("Var2",
label = "Variable2", #DATA CHOICE 2
min=10, max=100, value=10),
),
mainPanel(
plotlyOutput('plot') #Draw figure
)
)
server <- function(input, output) {
out <- reactive({
x1 <- rnorm(input$Var1)
y1 <- seq(1:input$Var1)
x2 <- rnorm(input$Var2)
y2 <- seq(1:input$Var2)
xx <- c(x1,x2)
yy <- c(y1,y2)
gg <- c( rep(1,length(y1)), rep(2,length(y2)) )
df <- data.frame(cbind(xx,yy,gg))
df
})
output$plot <- renderPlotly({
p <- ggplot() +
geom_line(data=out(), aes(x = xx, y = yy, group=gg, colour=gg))
ggplotly(p)
})
}
shinyApp(ui = ui, server = server)

bars missing when using shiny to create ggplot bar chart

I used shiny and created a app.R file to hope to build a bar chart with ggplot. I also used checkboxGroupInput to create a 2 check boxes to control the condition. While the total number of bars should be 28 after all boxes are checked, but the maximum seemed to allow only 17 bars for some reason. So some bars (row of data) are missing. The missing bars don't seems to have a pattern. Can someone please help ?
dataset:https://drive.google.com/open?id=1fUQk_vMJWPwWnIMbXvyd5ro_HBk-DBfc
my code:
midterm <- read.csv('midterm-results.csv')
library(dplyr)
library(tidyr)
# get column number for response time
k <- c(33:88)
v <- c()
for (i in k){
if (i%%2 == 1){
v <- c(v,i)
}
}
#average response time by question
time <- midterm[ , v]
new.col.name <- gsub('_.*', "", colnames(time))
colnames(time) <- new.col.name
avg.time <- data.frame(apply(time, 2, mean))
avg.time$question <- rownames(avg.time)
colnames(avg.time) <- c('response_time', 'question')
rownames(avg.time) <- NULL
avg.time$question <- factor(avg.time$question,
levels = c('Q1','Q2','Q3','Q4','Q5','Q6','Q7','Q8.9',
'Q10','Q11','Q12.13','Q14','Q15','Q16','Q17',
'Q18','Q19','Q20','Q21','Q22','Q23','Q24','Q25',
'Q26','Q27','Q28','Q29','Q30'))
avg.time$question_type <- c(1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,0,0,1,1,0,0)
# I did this manually because the there when data was imported into the midterm.csv,
# q8 & 9, q12 &13 were accidentally merged (28 v.s 30 question)
avg.time$question_type <- ifelse(avg.time$question_type == 1,
'googleable', 'not googleable')
avg.time$question_type <- factor(avg.time$question_type,
levels = c('googleable', 'not googleable'))
library(shiny)
library(ggplot2)
ui <- fluidPage(
checkboxGroupInput(inputId = "type",
label = "select question type",
choices = levels(avg.time$question_type),
selected = TRUE),
plotOutput('bar')
)
server <- function(input, output) {
output$bar <- renderPlot({
ggplot(avg.time[avg.time$question_type==input$type, ],
aes(x=question, response_time)) +
geom_bar(aes(fill = question_type), stat='identity', width = 0.5)
}, height =500, width = 1000)
}
shinyApp(ui = ui, server = server)
library(shiny)
library(ggplot2)
ui <- fluidPage(
checkboxGroupInput(inputId = "type", label = "select question type",
choices = levels(avg.time$question_type), selected = TRUE),
plotOutput('bar')
)
server <- function(input, output) {
data <- reactive(avg.time[avg.time$question_type %in% input$type, ])
output$bar <- renderPlot({
ggplot(data(),
aes(x=question, response_time)) + geom_bar(stat='identity', width = 0.5,
aes(fill = question_type))
}, height =500, width = 1000)
}
shinyApp(ui = ui, server = server)
of course you can use avg.time[avg.time$question_type %in% input$type, ] inside ggplot2 but reactivity is better.

Imputing into a custom function for a shiny app. R

Hello i am trying to make an app in shiny that will take 3 vectors from a data frame and two entered inputs. I would like the five entries to pass threw the function I have maid below and give the output that the function is maid to give. This would be 4 plots set in a grid (two heat maps, one point plot and a simivariogram). I feel that I have the bulk of the app together but I am having trouble getting my five inputs from the ui to pass threw my function and give out the plots. The function works great in R without shiny i just want it to work as an app now. Also I am pretty new to shiny so i might be missing something simple.
UI:
library(shiny)
library(shinydashboard)
library(ggplot2)
library(leaflet)
library(data.table)
library(dplyr)
################
# App interface
ui <- fluidPage(
# App csv input
headerPanel("Kriging"),
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
# App button selection for var, lat, lon
fluidRow(
column(6,radioButtons("xaxisGrp","Var:", c("1"="1","2"="2"))),
column(6,checkboxGroupInput("yaxisGrp","Lat/Lon:", c("1"="1","2"="2")))
),
# App buttons comma and quote
radioButtons('sep', 'Separator',
c(Comma=',', Semicolon=';',Tab='\t'), ','),
radioButtons('quote', 'Quote',
c(None='','Double Quote'='"','Single Quote'="'"),'"'),
uiOutput("choose_columns")
),
mainPanel(
tabsetPanel(
tabPanel("Plot",plotOutput("plot")),
tabPanel("Data", tableOutput('contents'))
)
)
# App sliders for values of definition
,
sliderInput(inputId = "num",
label = "choose x",
value = 0.1, min = 0.01, max = 1),
sliderInput(inputId = "num",
label = "choose y",
value = 0.1, min = 0.01, max = 1)
#initiating kriging
, actionButton("btn", "Krige")
)
##################################################
Server:
I have boxed off my function but I need help passing my arguments into the function.
server <- function(input, output, session) {
dsnames <- c()
data_set <- reactive({
inFile <- input$file1
if (is.null(inFile))
return()
data_set<-read.csv(inFile$datapath, header=input$header,
sep=input$sep, quote=input$quote)
})
output$contents <- renderTable({data_set()})
#controlling our buttons
observe({
dsnames <- names(data_set())
cb_options <- list()
cb_options[ dsnames] <- dsnames
updateRadioButtons(session, "xaxisGrp",
label = "Var",
choices = cb_options,
selected = "")
updateCheckboxGroupInput(session, "yaxisGrp",
label = "Lat/Lon",
choices = cb_options,
selected = "")
})
output$choose_dataset <- renderUI({
selectInput("dataset", "Data set", as.list(data_sets))
})
##############################################################
#My function
kri <- function(var, lat, lon, defx, defy){
options(warn = -1)
#internal function for kriging
kri3 <- function(var, lat, lon, defx, defy){
#making a data frame out of the given vector
spdf <- data.frame(var,lat,lon)
#makeing spatial point data frame coords
sp::coordinates(spdf) <- ~ lon + lat
bbox <- sp::bbox(spdf)
#variogram stuff
lzn.vgm <- gstat::variogram(var ~ 1, spdf)
lzn.fit1 <- gstat::fit.variogram(lzn.vgm, model = gstat::vgm(1, "Sph", 900, 1))
lzn.fit = automap::autofitVariogram(var ~ 1,
spdf,
model = c("Sph", "Exp", "Gau", "Ste"),
kappa = c(0.05, seq(0.2, 2, 0.1), 5, 10),
fix.values = c(NA, NA, NA),
start_vals = c(NA,NA,NA),
verbose = T)
#making our grid
cs <- c(defx, defy)
bb <- sp::bbox(spdf)
cc <- bb[,1] + (cs/2)
cd <- ceiling(diff(t(bb))/cs)
gold_grd <- sp::GridTopology(cellcentre.offset = cc, cellsize = cs, cells.dim = cd)
gold_grd
p4s <- sp::CRS(sp::proj4string(spdf))
gold_sg <- sp::SpatialGrid(gold_grd, proj4string = p4s)
summary(gold_sg)
#kringing and auto kriging
lzn.kriged <- as.data.frame(gstat::krige(var ~ 1, spdf, gold_sg , model=lzn.fit1))
lzn.Akriged <- automap::autoKrige(var ~ 1, spdf, gold_sg)
lzn.Akriged.pred <- lzn.Akriged$krige_output$var1.pred
lzn.Akriged.var <- lzn.Akriged$krige_output$var1.var
#making a data frame to use in return
kriw <- data.frame(lzn.kriged, lzn.Akriged.var, lzn.Akriged.pred)
return(kriw)
}
kriw <- kri3(var, lat, lon, defx, defy)
#internal function for maping
Kmap <- function(var, lat, lon, kriw){
#making a data spatial point data frame for out variogram plot
spdf <- data.frame(var,lat,lon)
#makeing spatial point data frame coords
sp::coordinates(spdf) <- ~ lon + lat
bbox <- sp::bbox(spdf)
#variogram stuff
lzn.vgm <- gstat::variogram(var ~ 1, spdf)
lzn.fit = automap::autofitVariogram(var ~ 1,
spdf,
model = c("Sph", "Exp", "Gau", "Ste"),
kappa = c(0.05, seq(0.2, 2, 0.1), 5, 10),
fix.values = c(NA, NA, NA),
start_vals = c(NA,NA,NA),
verbose = T)
varplot <- plot(lzn.vgm, lzn.fit$var_model, main = "Fitted variogram")
#making a dataframe for ggplot
kriw <- as.data.frame(kriw)
#making a maps
bbox1 <- ggmap::make_bbox(lon, lat, f = 1.4)
map <- ggmap::get_map(bbox1)
#making a heat map
M1 <- ggmap::ggmap(map) +
ggplot2::geom_tile(data = kriw, ggplot2::aes(x = lon,
y = lat, alpha = var1.pred), fill = "red") + ggplot2::ggtitle("Prediction Heat Map")
M2 <- ggmap::ggmap(map) +
ggplot2::geom_tile(data = kriw, ggplot2::aes(x = lon,
y = lat, alpha = var1.var), fill = "red") + ggplot2::ggtitle("Variance Heat Map")
var2 <- data.frame(var, lat, lon)
Dplot <- ggmap::ggmap(map) + ggplot2::geom_point(data = var2, ggplot2::aes(size=var, color=var, alpha=var)) +
ggplot2::coord_equal() + ggplot2::ggtitle("Desnisty map") + ggplot2::theme_bw()
#Placing both heat maps together
heat <- gridExtra::grid.arrange(M1,M2,varplot,Dplot, ncol=2)
return(heat)
}
#mapping output
Kmap(var, lat, lon, kriw)
options(warn = 0)
}
###############################################
# end of my fucntion
}
shinyApp(ui = ui, server = server)
What I get
What i want in the plot box
It appears you need a observe to tie this all together. Try this at the end of your server function.
observeEvent(
# react to button press
input$btn,
{
# to show the input values
str(input$xaxisGrp)
str(input$yaxisGrp)
# you have defined num for both x and y
# so I think you will want to change the
# inputId to numX and numY in ui
str(input$num)
# translate all the inputs into
# suitable arguments for kri
# send the output from kri
output$plot <- renderPlot({
kri(...allyourtranslatedargument...)
})
}
)

Resources