How to add a button that removes input rows? - r

I have some code to program a small shiny app for which I need to add input rows and, if needed, delete them again. I have figured out how to add inputs, however I can't figure out how to delete them.
Here is a MWE of my code. There is no output since I cannot share the excel sheet behind the code from which the output is generated. However, it should suffice for simply helping me find the correct code to remove the added rows with full input (except the first row):
library(shiny)
GeographyList <- c("Africa","Asia Pacific","Europe","Global", "United States","Latin America & Caribbean")
RegionList <- c("Emerging",
"Developed")
ClassList <- c("1",
"2",
"3")
# Define UI for app that draws a plot ----
ui <- fluidPage(
fluidRow(
mainPanel(
uiOutput("inputwidgets"),
actionButton("number",
"Add Row"),
# Input: Click to update the Output
actionButton("update", "Update View"),
# Output: Plot ----
h4("Allocation"),
plotOutput("distPlot")
)
)
)
# Define server logic required to call the functions required ----
server <- function(input, output, session) {
# Get new input by clicking Add Row
observeEvent(input$number, {
output$inputwidgets = renderUI({
input_list <- lapply(1:input$number, function(i) {
# for each dynamically generated input, give a different name
fluidRow(
column(2,
selectInput(paste0("Geography", i),
label = paste0("Geography", i),
choices = GeographyList,
multiple = FALSE,
selected = NA)
),
column(3,
selectInput(paste0("Region", i),
label = paste0("Region", i),
choices = RegionList,
multiple = FALSE,
selected = NA)
),
column(4,
selectInput(paste0("Class", i),
label = paste0("Class", i),
choices = ClassList,
multiple = FALSE,
selected = NA)
),
column(3,
# Input: Specify the amount ----
numericInput(paste0("amount",i), label="Amount", 0)
))
})
do.call(tagList, input_list)
})
})
output$distPlot <- renderPlot({
if (input$update == 0)
return()
isolate(input$number)
isolate(input$amount)
slices <- c(input$amount1,input$amount2,input$amount3,input$amount4)
pie(slices)
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
Appreciate any tips since I am very new to shiny! Thanks in advance.

You could build your lapply loop based on a reactiveValue which is triggered by your add button and a delete button:
1. Edit: using sapply in output$distPlot according to lapply rows
2. Edit: using existing input values
library(shiny)
GeographyList <- c("Africa","Asia Pacific","Europe","Global", "United States","Latin America & Caribbean")
RegionList <- c("Emerging",
"Developed")
ClassList <- c("1",
"2",
"3")
# Define UI for app that draws a plot ----
ui <- fluidPage(
fluidRow(
mainPanel(
uiOutput("inputwidgets"),
actionButton("number",
"Add Row"),
actionButton("delete_number",
"Delete Row"),
# Input: Click to update the Output
actionButton("update", "Update View"),
# Output: Plot ----
h4("Allocation"),
plotOutput("distPlot")
)
)
)
# Define server logic required to call the functions required ----
server <- function(input, output, session) {
reac <- reactiveValues()
observeEvent(c(input$number,input$delete_number), {
# you need to add 1 to not start with 0
add <- input$number+1
# restriction for delete_number > number
delete <- if(input$delete_number > input$number) add else input$delete_number
calc <- add - delete
reac$calc <- if(calc > 0) 1:calc else 1
})
# Get new input by clicking Add Row
observe({
req(reac$calc)
output$inputwidgets = renderUI({
input_list <- lapply(reac$calc, function(i) {
Geography <- input[[paste0("Geography",i)]]
Region <- input[[paste0("Region",i)]]
Class <- input[[paste0("Class",i)]]
amount <- input[[paste0("amount",i)]]
# for each dynamically generated input, give a different name
fluidRow(
column(2,
selectInput(paste0("Geography", i),
label = paste0("Geography", i),
choices = GeographyList,
multiple = FALSE,
selected = if(!is.null(Geography)) Geography
)
),
column(3,
selectInput(paste0("Region", i),
label = paste0("Region", i),
choices = RegionList,
multiple = FALSE,
selected = if(!is.null(Region)) Region
)
),
column(4,
selectInput(paste0("Class", i),
label = paste0("Class", i),
choices = ClassList,
multiple = FALSE,
selected = if(!is.null(Class)) Class
)
),
column(3,
# Input: Specify the amount ----
numericInput(
paste0("amount",i),
label="Amount",
value = if(!is.null(amount)) amount else 0
)
))
})
do.call(tagList, input_list)
})
})
output$distPlot <- renderPlot({
req(reac$calc, input$update)
slices <- sapply(reac$calc, function(i) {
c(input[[paste0("amount",i)]])
})
pie(slices)
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)

Related

Delete map after pressing reset button on shiny

I am not able to delete the generated map after I press the reset button on shiny, could you help me to insert the code to delete the map made after pressing the button? For both selectInput works normally, only the map that is not deleted from the screen.
library(shiny)
library(shinythemes)
library(lubridate)
library(googleway)
set_key("API KEY")
df1<- structure(
list(
Marketname = c("Market1","Market1", "Market2","Market2", "Market3", "Market3", "Market4", "Market4"),
Latitude = c(-22.900200453490385, -22.900200453490385,-22.89279876292728,-22.89279876292728,-22.89107669207457,-22.89107669207457,-22.91668421655409,-22.91668421655409),
Longitude = c(-48.448779371935494,-48.448779371935494, -48.45043377250408,-48.45043377250408,-48.44108027972275,-48.44108027972275,-48.43786997555729,-48.43786997555729)),
row.names = c(NA, 8L), class = "data.frame")
ui <- fluidPage(
shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
br(),
tabPanel("Rota",
sidebarLayout(
sidebarPanel(
selectizeInput("market1", label = h5("Choose starting point:"), choices = NULL,
multiple = TRUE,
options = list(maxItems = 1)),
selectizeInput("market2", label = h5("Choose destination point:"), choices = NULL,
multiple = TRUE,
options = list(maxItems = 1)),
actionButton(inputId = "getRoute", label = "Get route"),
actionButton(inputId = "reset", label = "Reset")),
mainPanel(
tabsetPanel(
tabPanel("Route",google_mapOutput(outputId = "mapWarsaw"),
)
))
))))
server <- function(input, output,session) {
observe({
updateSelectizeInput(session, "market1",
choices = unique(df1$Marketname)
)
})
observe({
excludeOption <- NULL
if (!is.null(input$market1)) {
excludeOption <- input$market1
}
updateSelectizeInput(session, "market2",
choices = unique(df1$Marketname[df1$Marketname != excludeOption])
)
})
observeEvent(input$getRoute, {
origin <- df1[df1$Marketname == input$market1, c("Latitude", "Longitude")][1, ]
destination <- df1[df1$Marketname == input$market2, c("Latitude", "Longitude")][1, ]
route <- google_directions(origin = origin,
destination = destination,
mode = "driving")
df_routes <- data.frame(polyline = direction_polyline(route))
df_way <- cbind(
route$routes$legs[[1]]$end_location,
data.frame(address = route$routes$legs[[1]]$end_address)
)
m3<-google_map() %>%
add_polylines(data = df_routes, polyline = "polyline", stroke_weight = 4)
output$mapWarsaw <- renderGoogle_map({
m3
})
})
observeEvent(input$reset, {
updateSelectInput(session, "market1", selected = "")
updateSelectInput(session, "market2", selected = "")
})
}
shinyApp(ui = ui, server = server)
Here I pressed reset, the selectInput was cleared, but the map was not, so I would like to insert some code that cleared the screen. Of course, after inserting the selectInput options again, the map was generated normally on the screen.
I recommend using a reactiveVal to store whether you want to plot or not, then req(.) that value in your plot.
A simple example:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
numericInput("n", "N", value = 2),
actionButton("toggle", "Toggle")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output, session) {
doplot <- reactiveVal(TRUE)
observeEvent(input$toggle, {
doplot(!isTRUE(doplot()))
})
output$plot <- renderPlot({
req(doplot())
plot(runif(input$n))
})
}
shinyApp(ui, server)
doplot defaults to TRUE, so it starts plotting immediately (assuming input$n has a value), and every time input$n is changed, a new plot is rendered;
when you click on toggle, the doplot is inverted, and the dependent output$plot will "fail" the req(doplot()) requirement and clear the plot;
this could be improved to give a clearer indication that the empty plot is because the user toggled the button ... in this case, I should likely change the style of the button to clearly indicate the state, but that's aesthetics and may or may not apply to your app
Another similar method would be to store the data in a reactiveVal, and require it, perhaps like this:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
numericInput("n", "N", value = 2),
actionButton("toggle", "Toggle")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output, session) {
mydat <- reactiveVal(NULL)
observeEvent(input$toggle, {
req(input$n)
if (is.null(mydat())) {
mydat(runif(input$n))
} else mydat(NULL)
})
output$plot <- renderPlot({
plot(req(mydat()))
})
}
shinyApp(ui, server)
This second method won't work as well if you need to preserve the data.

Are there any specific resources to use for creating a shiny app that uploads a file and plots a selected column?

library(shiny)
library(plotly)
library(ggplot2)
library(tidyverse)
library(DT)
if (!require("okcupiddata")) install.packages("okcupiddata")
library(okcupiddata)
D=sample_n(profiles, 5000)
write.csv(D, file="~/Downloads/OKCupid.csv", row.names = FALSE)
ui <- fluidPage(
# Application title
titlePanel(title = "Uploading Your File"),
sidebarLayout(
sidebarPanel(
width = 2,
## Create a file upload control
fileInput(inputId = "file",
label = "Choose Your File:",
accept = c(".txt", ".csv")),
## Use html tag hr (horizontal rule) to make a horizontal separator
hr(),
## Make a h5 heading
h5("Max file size is 2M"),
## Create a checkbox that can be used to specify logical values.
checkboxInput(inputId = "header",
label = "Header",
value = TRUE),
## Create a set of radio buttons used to select an item from a list.
radioButtons(inputId = "sep",
label = "Separator",
choices = c(Comma = ",", Space = " ", Tab = "\t")),
uiOutput("variable")
),
mainPanel(
tabsetPanel(
tabPanel("Table", tableOutput("table")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Plot", plotlyOutput("plot", height = "700px"))
)
)
)
)
server <- function(input, output, session) {
myData <- reactive({
f = input$file
if (is.null(f)){
return(NULL)
} else {
read.table(f$datapath, header = input$header, sep = input$sep)
}
})
#A. Create a drop-down menu to choose a variable
output$variable <- renderUI({
})
#B. Display the whole table
output$table <- renderTable({
})
#C. Summarize the whole table
output$summary <- renderPrint({
})
#D. Plot only the selected variable.
# The code needs to handle both a categorical and numeric variables
output$plot <- renderPlotly({
})
}
shinyApp(ui = ui, server = server)
I'm stuck on A, B, C, and D. I know to use selectInput() to create a drop down menu, a data frame() function to render a table, a summary() function to render a summary, and a ggplot() function to render both a numeric and categorical plot.I don't know how to correctly reference the selected file and then reference the column from said file. Any ideas?
The answer completes A, B, C and D. You haven't really shared what kind of plot you need but based on class of the column selected this displays the plot.
library(shiny)
library(plotly)
library(tidyverse)
library(DT)
ui <- fluidPage(
# Application title
titlePanel(title = "Uploading Your File"),
sidebarLayout(
sidebarPanel(
width = 2,
## Create a file upload control
fileInput(inputId = "file",
label = "Choose Your File:",
accept = c(".txt", ".csv")),
## Use html tag hr (horizontal rule) to make a horizontal separator
hr(),
## Make a h5 heading
h5("Max file size is 2M"),
## Create a checkbox that can be used to specify logical values.
checkboxInput(inputId = "header",
label = "Header",
value = TRUE),
## Create a set of radio buttons used to select an item from a list.
radioButtons(inputId = "sep",
label = "Separator",
choices = c(Comma = ",", Space = " ", Tab = "\t")),
uiOutput("variable")
),
mainPanel(
tabsetPanel(
tabPanel("Table", tableOutput("table")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Plot", plotlyOutput("plot", height = "700px"))
)
)
)
)
server <- function(input, output, session) {
myData <- reactive({
f = input$file
if (is.null(f)){
return(NULL)
} else {
read.table(f$datapath, header = input$header, sep = input$sep)
}
})
#A. Create a drop-down menu to choose a variable
output$variable <- renderUI({
selectInput('dd', 'Select dropdown', names(myData()))
})
#B. Display the whole table
output$table <- renderTable({
myData()
})
#C. Summarize the whole table
output$summary <- renderPrint({
summary(myData())
})
#D. Plot only the selected variable.
# The code needs to handle both a categorical and numeric variables
output$plot <- renderPlotly({
if(is.numeric(myData()[[input$dd]]))
plt <- ggplot(myData(), aes(.data[[input$dd]])) + geom_histogram()
else
plt <- ggplot(myData(), aes(.data[[input$dd]])) + geom_bar()
ggplotly(plt)
})
}
shinyApp(ui = ui, server = server)

R Shiny: Print input from a selectInput function created dynamically from prior inputs

I am struggling to print the output of various selectInput options on my 'Example_2' tab. These fields themselves have been created within the server based on prior inputs from 'Example_1' tab.
Please see below:
library(shinythemes)
library(shiny)
rm(list = ls())
ui <- navbarPage('Example',id = "inTabset",
tabPanel(title = "Example_1", value = "Example_1",
fluidPage(
tags$b( h4("Example_1", align = "left")),
theme = shinytheme("paper"),
fluidRow(
column(6,checkboxGroupInput("checkGroup", label ="",
choices = c(1,2,3,4,5,6,7,8),
selected = c(1,4,7)) )
),
br()
),
hr(),
verbatimTextOutput("example1")
),
tabPanel(title = "Example_2", value = "Example_2",
fluidPage(
tags$b( h4("Example_2", align = "left")),
br(),
fluidRow(
column(4, uiOutput("VarsInput")),
fluidRow(verbatimTextOutput("dataInfo")),
br(),
hr())
)
))
server <- function(input, output, session) {
output$example1 = renderPrint(input$checkGroup)
### output$example2 = ????
### i.e what data (a,b,c,d,e or f) has been chosen from the selectInput below?
K <- reactive({
length(input$checkGroup)
})
output$VarsInput <- renderUI({
NoV = K()
C = sapply(1:(ceiling(NoV)), function(i){paste0(input$checkGroup[i])})
output = tagList()
for(i in seq_along(1:ceiling(NoV))){
output[[i]] = tagList()
output[[i]][[1]] = selectInput(C[i], C[i], c("",c("a","b","c","d","e","f")))
}
output
})
}
shinyApp(ui, server)
In ui I added verbatimTextOutput for your example2.
When dynamically creating outputs, I believe you just need output[[i]] in your for loop.
For name of these selectInput widgets, added "item" instead of just having the id be a number.
Then, you can access the selected values for these inputs through input[[paste0("item", i)]] where i is matched to your checkboxes.
Edit (12/27/20) Based on comment, with varying checkboxes and inputs, you will want to store both the input name (or index) and choice. So, you could make a reactive data frame to store these, instead of just storing the value selected. Also, you need to check if the dynamically created input exists (or is.null) before storing the value. Additionally, when you create your new dynamic inputs, you can check with the index to provide an accurate default/selected value. See if this works for you.
library(shinythemes)
library(shiny)
ui <- navbarPage('Example',id = "inTabset",
tabPanel(title = "Example_1", value = "Example_1",
fluidPage(
tags$b( h4("Example_1", align = "left")),
theme = shinytheme("paper"),
fluidRow(
column(6,checkboxGroupInput("checkGroup", label ="",
choices = c(1,2,3,4,5,6,7,8),
selected = c(1,4,7)) )
),
br()
),
hr(),
verbatimTextOutput("example1")
),
tabPanel(title = "Example_2", value = "Example_2",
fluidPage(
tags$b( h4("Example_2", align = "left")),
br(),
fluidRow(
column(4, uiOutput("VarsInput")),
fluidRow(verbatimTextOutput("dataInfo")),
br(),
hr(),
verbatimTextOutput("example2"))
)
))
server <- function(input, output, session) {
rv <- reactiveValues(df = NULL)
observe({
rv$df <- data.frame(
index = as.numeric(),
choice = as.character()
)
for (i in input$checkGroup) {
the_item <- input[[paste0("item", i)]]
rv$df <- isolate(rbind(rv$df, data.frame(index = i, choice = ifelse(is.null(the_item), "", the_item))))
}
})
output$example1 = renderPrint(input$checkGroup)
output$example2 <- renderPrint(
for (i in input$checkGroup) {
print(input[[paste0("item", i)]])
}
)
K <- reactive({
length(input$checkGroup)
})
output$VarsInput <- renderUI({
NoV = K()
C = sapply(1:(ceiling(NoV)), function(i){paste0(input$checkGroup[i])})
output = tagList()
for(i in seq_along(1:ceiling(NoV))){
output[[i]] = tagList()
output[[i]] = selectInput(paste0("item", C[i]), C[i], c("",c("a","b","c","d","e","f")),
selected = isolate(rv$df$choice[rv$df$index == C[i]]))
}
output
})
}
shinyApp(ui, server)

Subset data in R Shiny using Multiple Variables

I am new to R Shiny. I am attempting to create an app that allows a user to subset a data.frame based on multiple variables and then see the resulting data.
Here is a small example data set:
iter,wave,apples
1,1,600
1,1,500
1,1,400
1,2,300
1,2,200
1,2,100
2,1,1000
2,1,1100
2,1,1200
2,2,1300
2,2,1400
2,2,1500
3,1,1100
3,1,2200
3,1,3300
3,2,4400
3,2,5500
3,2,6600
I would like the user to be able to specify the value of iter and of wave and see the resulting data.
Here is my attempt at the Shiny code. I realize I must be making several silly mistakes.
Edit
Here is my revised code. The end result now comes pretty close to what I want. The sidebar is still not being displayed perfectly.
library(shiny)
setwd('C:/Users/mark_/Documents/simple_RShiny_files/explore')
apple.data <- read.csv('subset_data_based_on_multiple_variables.csv',
header = TRUE, stringsAsFactors = FALSE)
ui <- fluidPage(
titlePanel("Subsetting Apple Dataset"),
sidebarLayout(
sidebarPanel(
uiOutput("codePanel")
),
mainPanel(
tableOutput("view")
)
),
selectInput("codeInput", inputId ="data1", label = "Choose Iter", choices = unique(apple.data$iter)),
selectInput("codeInput", inputId ="data2", label = "Choose Wave", choices = unique(apple.data$wave))
)
server <- function(input, output) {
output$codePanel <- renderUI({
})
dataset <- reactive({
subset(apple.data, (iter == input$data1 & wave == input$data2))
})
output$view <- renderTable(dataset())
}
shinyApp(ui = ui, server = server)
The output
The problem is that both selectInputs have the same inputId. This works:
library(shiny)
apple.data <- data.frame(
iter = c(1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,
2L,3L,3L,3L,3L,3L,3L),
wave = c(1L,1L,1L,2L,2L,2L,1L,1L,1L,2L,2L,
2L,1L,1L,1L,2L,2L,2L),
apples = c(600L,500L,400L,300L,200L,100L,1000L,
1100L,1200L,1300L,1400L,1500L,1100L,2200L,3300L,4400L,
5500L,6600L)
)
ui <- fluidPage(
titlePanel("Subsetting Apple Dataset"),
sidebarLayout(
sidebarPanel(
selectInput("codeInput1", label = "Choose Iter", choices = unique(apple.data$iter)),
selectInput("codeInput2", label = "Choose Wave", choices = unique(apple.data$wave))
),
mainPanel(
tableOutput("view")
)
)
)
server <- function(input, output) {
dataset <- reactive({
return(subset(apple.data, (iter == input$codeInput1 & wave == input$codeInput2)))
})
output$view <- renderTable(dataset())
}
shinyApp(ui = ui, server = server)

Plot data after browsing input files with Shiny

I am trying to build a Shiny App that does the following:
1) Browse a value file that looks like
Sample x y
A 1 3
B 2 1
C 3 6
D 4 4
2) Browse a second info file,
Sample Country Status
A US OK
B UK OK
C UK NOPE
D US OK
3) When I press a Submit button,
4) I merge the two files by the Sample column,
5) And make a scatter plot with ggplot, with a dropdown menu that allows me to color the points according to the names of the columns from the info file.
With the code below, I am facing two problems: (i) after I load my files and press the Submit button, nothing happens, and (ii) how could I mention in my selectInput block for my dropdown menu the number of possible choices (assuming I do not know them in advance)?
library(shiny)
library(ggplot2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput(
inputId = "user_value_file",
label = "Choose a file with numeric values"
),
fileInput(
inputId = "user_info_file",
label = "Choose a file with sample info"
),
actionButton(
inputId = "my_button",
label = "Submit"
)
),
mainPanel(
# browse sample annotation file
selectInput(
inputId = "info_col",
label = "Choose an info to color",
choices = c("Country", "Status") # I am cheating here because I know in advance what are the colnames of the info file
),
# outputs
plotOutput(
outputId = "my_scatter_plot"
)
)
)
)
server <- function(input, output) {
output$contents <- renderTable(
{
valueFile <- input$user_value_file
if (is.null(valueFile))
return(NULL)
infoFile <- input$user_info_file
if (is.null(infoFile))
return(NULL)
}
)
randomVals <- eventReactive(
input$goButton,
{
my_val <- read.table(valueFile$datapath, header = T, sep = "\t")
my_info <- read.table(infoFile$datapath, header = T, sep = "\t")
df <- merge(my_val, my_info, by="Sample")
output$my_scatter_plot <- renderPlot(
{
ggplot(df, aes_string(x=df$x, y=df$y, color=input$info_col)) +
geom_point()
}
)
}
)
}
shinyApp(ui = ui, server = server)
A few things noted to get it working:
the inputID in the layout needs to match the server renderTable parameters (e.g., input$goButton should be input$my_button)
renderPlot was moved from eventReactive, and calls randomVals to get the data frame df
To get choices from the user info file, added session to server function and updateSelectInput (note depending on that file structure you probably want to do something like remove first column name or other changes)
Otherwise left things as they are. Please let me know if this has the behavior you were looking for.
library(shiny)
library(ggplot2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput(
inputId = "user_value_file",
label = "Choose a file with numeric values"
),
fileInput(
inputId = "user_info_file",
label = "Choose a file with sample info"
),
actionButton(
inputId = "my_button",
label = "Submit"
)
),
mainPanel(
# browse sample annotation file
selectInput(
inputId = "info_col",
label = "Choose an info to color",
choices = NULL # Get colnames from user_info_file later on
),
# outputs
plotOutput(
outputId = "my_scatter_plot"
)
)
)
)
server <- function(input, output, session) {
output$contents <- renderTable({
valueFile <- input$user_value_file
if (is.null(valueFile))
return(NULL)
infoFile <- input$user_info_file
if (is.null(infoFile))
return(NULL)
})
randomVals <- eventReactive(input$my_button, {
my_val <- read.table(input$user_value_file$datapath, header = T, sep = "\t")
my_info <- read.table(input$user_info_file$datapath, header = T, sep = "\t")
updateSelectInput(session, "info_col", "Choose an info to color", choices = names(my_info)[-1])
merge(my_val, my_info, by="Sample")
})
output$my_scatter_plot <- renderPlot({
df <- randomVals()
ggplot(df, aes_string(x=df$x, y=df$y, color=input$info_col)) +
geom_point()
})
}
shinyApp(ui, server)

Resources