Skip to content

Instantly share code, notes, and snippets.

@Nicolabo
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicolabo/8c7cf201ec542c0af874 to your computer and use it in GitHub Desktop.
Save Nicolabo/8c7cf201ec542c0af874 to your computer and use it in GitHub Desktop.
choices <- c("Value1", "Value2",
"Value3", "Value4",
"Value5", "Value6",
"Value7", "Value8",
"Value9", "Value10",
"Value11", "Value12")
levele <- c("AT1","AT2","AT3","AT4","AT5","RT1","AT6","AT7","AT8","AT9","AT10","RT2")
number_value1 <- sample(1:100,22)
number_value2 <- sample(1:100,22)
number_value3 <- sample(1:100,50)
number_value4 <- sample(1:100,50)
df1 <- data.frame(product = c(rep("Product1",12),rep("Product2",10)), name = c(choices,choices[1:10]),
short_name = c(levele,levele[1:10]),number = number_value1, number2 = number_value2)
df2 <- data.frame(product = c(rep("Product1",30),rep("Product2",20)), name = c(rep(choices,2),choices[1:6],rep(choices[1:10],2)),
short_name = c(rep(levele,2),levele[1:6],rep(levele[1:10],2)),number = number_value3, number2 = number_value4)
df1$name <- factor(df1$name, levels = choices)
df1$short_name <- factor(df1$short_name, levels = levele)
df2$name <- factor(df2$name, levels = choices)
df2$short_name <- factor(df2$short_name, levels = levele)
axis_vars_y <- c("number","number2")
axis_vars_x <- c("name","number","number2")
library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output,session) {
datasetInput <- reactive({
switch(input$dataset,
df2 = df2,
df1 = df1)
})
axis_vara_y <- reactive({
switch(input$yvar,
number = 4,
number2 = 5)
})
output$slider <- renderUI({
sliderInput("inslider",h5(""), min = round(min(datasetInput()[,axis_vara_y()]),0)-1,
max = round(max(datasetInput()[,axis_vara_y()]),0)+1,
value = c(round(min(datasetInput()[,axis_vara_y()]),0)-1,
round(max(datasetInput()[,axis_vara_y()]),0)+1),
step = 0.5)
})
output$test_name_detail <- renderUI({
checkboxGroupInput("intest_name_detail",label = "",
choices = levels(droplevels(datasetInput()[datasetInput()$product == input$product,][["name"]])),
selected = c(choices[1], choices[2]))
})
#------------------------------------------------------------------------------------------------------------------------
data <- reactive({
filteredData <- datasetInput()
axisData <- axis_vara_y()
if(!is.null(input$inslider)){
if(input$radio == "All values"){
filteredData <- filteredData %>%
filter(product == input$product,
filteredData[,axisData] >= input$inslider[1],
filteredData[,axisData] <= input$inslider[2])
}
else {
filteredData <- filteredData %>%
filter(
name %in% input$intest_name_detail,
# name %in% input$checkGroup,
product == input$product,
filteredData[,axisData] >= input$inslider[1],
filteredData[,axisData] <= input$inslider[2])
}
}
return(filteredData)
})
data_point <- reactive({
data() %>%
mutate(id = row_number())
})
#------------------------------------------------------------------------------------------------------------------------
dotpoint_vis <- reactive({
xvar_name <- names(axis_vars_x)[axis_vars_x == input$xvar]
yvar_name <- names(axis_vars_y)[axis_vars_y == input$yvar]
xvar <- prop("x",as.symbol(input$xvar))
yvar <- prop("y",as.symbol(input$yvar))
plot <- data_point %>%
ggvis(x = xvar,y = yvar) %>%
layer_points(size := 120,fill = ~short_name) %>%
add_axis("x", title = xvar_name) %>%
add_axis("y", title = yvar_name) %>%
set_options(width = 750, height = 500)
})
dotpoint_vis %>% bind_shiny("plot")
})
library(ggvis)
library(markdown)
library(shiny)
library(dplyr)
library(magrittr)
shinyUI(
fluidPage(
h3("Title"),
fluidRow(
column(3,
wellPanel(
selectInput("product", h3("Select product"),
choices = list("Product1","Product2"),
selected = "Product1"),
radioButtons("radio",h5("Select"),choices=list("All values","Selected values"),
selected="All values"),
conditionalPanel(
condition = "input.radio != 'All values'",
uiOutput("test_name_detail")
),
hr(),
radioButtons("dataset", label = h5("Choose Dataframe"),
choices = list("2 Level" = "df1", "3 Level" = "df2")
),
hr(),
h5("Choice"),
selectInput("xvar", h6(""),
axis_vars_x,
selected = "value"),
selectInput("yvar", h6(""),
axis_vars_y,
selected = "number2"),
hr(),
uiOutput("slider")
)
),
column(9,
ggvisOutput("plot")
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment