How do I resolve this R shiny server looping if condition issue? - r

Context:
I am trying to make a shiny feature where the user can upload txt files and view the content of the files as a table. To account for the different types of delimiters I have placed radio buttons to act as delimiter options. The radio buttons will depend on the file type uploaded.
Issue:
The user would not be able to change the delimiter options and display the respective table, because the server keeps checking the file type and changing the radio buttons and resetting the default button.
Attempted solutions:
Setup a radiobutton for the user to select the file type which changes the delimited buttons instead of using an if condition of file type to determine the delimiter radiobuttons. Solution is not user friendly since the program should be able to identify and modify the radiobuttons in accordance to the file type.
Setup a go button to perform the if conditions for file type instead of relying on the server looping. Couldn't implement it properly
Question: Can anyone suggest solutions that are user friendly? Is there a feature in R shiny that already solves this issue?
UI
library(shinyWidgets)
library(DT)
library(shiny)
ui <- fluidPage(
titlePanel(title=div(img(src="ODClogo.png", height = 50), "OutDeCo")),
#navbarPage is top menu bar
navbarPage("",
#tabPanel is each tab in the navbarPage
# Assess DE tab
tabPanel(
title="Assess DE",
dropdown(
# title of sidepanel
tags$h3("Options"),
# inputs in the sidepanel
fileInput("DEFile", "Choose DE File",
accept = c(
".csv",
".tsv",
".txt"
)
),
# button for selecting delimiter, default is nothing until file is selected and handled in server side
radioButtons(inputId = 'sepButton', label = 'Delimiter Selector', choices = c(Default=''), selected = ''),
# side panel characteristics
style = "gradient", icon = icon("cog"),
status = "primary", width = "300px",
animate = animateOptions(
enter = animations$fading_entrances$fadeInLeftBig,
exit = animations$fading_exits$fadeOutLeftBig
)
),
navlistPanel(
tabPanel(
title="Cluster Genes",
"Cluster genes Page",
# Navigation Bar for types of plots inside cluster
tabsetPanel(
tabPanel(
title="View file",
mainPanel(
uiOutput("UIDEContent")
)
),
tabPanel(
title="Plot 2"
),
tabPanel(
title="Plot 3"
)
),
),
),
)
),
)
Server
server <- function(input, output, session) {
output$distPlot <- renderPlot({
hist(rnorm(input$obs), col = 'darkgray', border = 'white')
})
# reactive converts the upload file into a reactive expression known as data
data <- reactive({
# DEFile from fileInput() function
ServerDEFile <- input$DEFile
# extensions tool for format validation
extDEFile <- tools::file_ext(ServerDEFile$datapath)
# file format checking
req(ServerDEFile)
validate(need(extDEFile == c("csv", "tsv", "txt"), "Please upload a csv, tsv or txt file."))
# convert data into file format
if(is.null(extDEFile)){return()}
if (extDEFile == "txt") {
choice <-c(Comma=",", Semicolon=";", Tab="\t", Space=" ")
updateRadioButtons(session, "sepButton",
label = paste("Delimiters for", extDEFile, "file"),
choices = choice,
)
}
else if (extDEFile == "tsv") {
choice <- (Tab="\t")
updateRadioButtons(session, "sepButton",
label = paste("Delimiter: Tab"),
choices = choice
)
}
else {
choice <- (Comma=",")
updateRadioButtons(session, "sepButton",
label = paste("Delimiter: Comma"),
choices = choice
)
}
read.table(file=ServerDEFile$datapath, sep=input$sepButton)
})
# creates reactive table called DEFileContent
output$DEFileContent <- renderTable({
if(is.null(data())){return ()}
data()
})
# handles rendering of reactive object on tb on ui
output$UIDEContent <- renderUI({
tableOutput("DEFileContent")
})
}

Use reactive object just to create a data frame, and use an observer to update radio button. Try this
library(shinyWidgets)
library(DT)
library(shiny)
ui <- fluidPage(
titlePanel(title=div(img(src="ODClogo.png", height = 50), "OutDeCo")),
#navbarPage is top menu bar
navbarPage("",
#tabPanel is each tab in the navbarPage
# Assess DE tab
tabPanel(
title="Assess DE",
dropdown(
# title of sidepanel
tags$h3("Options"),
# inputs in the sidepanel
fileInput("DEFile", "Choose DE File",
accept = c(
".csv",
".tsv",
".txt"
)
),
# button for selecting delimiter, default is nothing until file is selected and handled in server side
radioButtons(inputId = 'sepButton', label = 'Delimiter Selector', choices = c(Default=''), selected = ''),
# side panel characteristics
style = "gradient", icon = icon("cog"),
status = "primary", width = "300px",
animate = animateOptions(
enter = animations$fading_entrances$fadeInLeftBig,
exit = animations$fading_exits$fadeOutLeftBig
)
),
navlistPanel(
tabPanel(
title="Cluster Genes",
"Cluster genes Page",
# Navigation Bar for types of plots inside cluster
tabsetPanel(
tabPanel(
title="View file",
mainPanel(
uiOutput("UIDEContent")
)
),
tabPanel(
title="Plot 2"
),
tabPanel(
title="Plot 3"
)
),
),
),
)
),
)
server <- function(input, output, session) {
output$distPlot <- renderPlot({
hist(rnorm(input$obs), col = 'darkgray', border = 'white')
})
observe({
# DEFile from fileInput() function
ServerDEFile <- req(input$DEFile)
# extensions tool for format validation
extDEFile <- tools::file_ext(ServerDEFile$datapath)
if(is.null(input$DEFile)){return()
}else{
if (extDEFile == "txt") {
label = paste("Delimiters for", extDEFile, "file")
choice <-c(Comma=",", Semicolon=";", Tab="\t", Space=" ")
}else if (extDEFile == "tsv") {
label = paste("Delimiter: Tab")
choice <- (Tab="\t")
}else {
label = paste("Delimiter: Comma")
choice <- (Comma=",")
}
updateRadioButtons(session, "sepButton", label = label, choices = choice)
}
})
# reactive converts the upload file into a reactive expression known as data
data <- reactive({
# DEFile from fileInput() function
ServerDEFile <- input$DEFile
# extensions tool for format validation
extDEFile <- tools::file_ext(ServerDEFile$datapath)
# file format checking
req(ServerDEFile)
validate(need(extDEFile == c("csv", "tsv", "txt"), "Please upload a csv, tsv or txt file."))
# convert data into file format
if(is.null(extDEFile)){return()}
read.table(file=ServerDEFile$datapath, sep=input$sepButton)
})
# creates reactive table called DEFileContent
output$DEFileContent <- renderTable({
if(is.null(data())){return ()}
data()
})
# handles rendering of reactive object on tb on ui
output$UIDEContent <- renderUI({
tableOutput("DEFileContent")
})
}
shinyApp(ui = ui, server = server)

Related

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)

fileInput function not responding in r Shiny

I am new to R and R shiny, and have been working on putting together a statistics application that will allow the user to import files, and then run different statistics programs on the data. The fileData function had been working fine for me until recently, and now whenever I attempt to upload a file, nothing opens. I have tried everything I can think of to get it to run, but it appears the file won't attach to the function. Any help will be very much appreciated!
library(shiny)
library(shinyFiles)
library(dplyr)
library(shinythemes)
ui <- fluidPage(theme = shinytheme("cosmo"),
# Application title
titlePanel("Stats"),
# Sidebar
sidebarLayout(
sidebarPanel(
tabsetPanel(type = "tab",
tabPanel("SCI",
fileInput("file1", "Insert File", multiple = TRUE, accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")),
selectInput("statChoice", "Choose Stats", c("None" = "None", "ANOVA 0 w/in 1 btw" = "A1btw", "ANOVA 0 w/in 2 btw" = "A2btw")),
conditionalPanel("statChoice == 'A1btw'",
uiOutput("ind1"),
uiOutput("dep1")),
conditionalPanel("statChoice == 'A2btw'",
uiOutput("ind1"),
uiOutput("ind2"),
uiOutput("dep1")),
)
)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(type = "tab",
tabPanel("Data",
dataTableOutput("fileData")),
tabPanel("Summary Statistics"),
tabPanel("Graphs"))
)
)
)
server <- function(input, output) {
fileData <- eventReactive(input$file1,{
read.csv(input$file1$dataPath, header = TRUE, sep = ",", dec = ".")
})
output$fileData <- renderDataTable(
fileData()
)
vars <- reactive({
names(fileData())
})
output$ind1 <- renderUI({
selectInput("var1", "Independent 1", choices = vars())
})
output$ind2 <- renderUI({
selectInput("var2", "Independent 2", choices = vars())
})
output$dep1 <- renderUI({
selectInput("var3", "Dependent 1", choices = vars())
})
}
shinyApp(ui = ui, server = server)
Tricky because Shiny doesn't give any warning about this :
shiny app will not work if the same "output" is used two times in Ui.R.
Everything looks OK, except the double use of uiOutput("dep1") and uiOutput("ind1") :
conditionalPanel("statChoice == 'A1btw'",
uiOutput("ind1"), # Used once
uiOutput("dep1")), # Used once
conditionalPanel("statChoice == 'A2btw'",
uiOutput("ind1"), # Used twice
uiOutput("ind2"),
uiOutput("dep1")), # Used twice
You should use an output only once.

How to create a popup window for user to input information in R shiny?

Here is an example. What I want is that users can run the demo as many time as they want by DEMO button. However when they click Browse for uploading local data (not reset button as I demonstrated in the example), I would popup a window to let users input their name and state in two input boxes. In the below example, by click RESET, a single popup box will launch (may be not a proper way).
library(shiny)
library(shinyWidgets)
if (interactive()) {
# Display an important message that can be dismissed only by clicking the
# dismiss button.
shinyApp(
ui <- fluidPage(
tabsetPanel(
##tabPanel-Input
tabPanel("Input", fluid = TRUE,
# tab title ----
titlePanel("Upload data"),
# sidebar layout with input and output tables ----
sidebarLayout(
# sidebar panel for inputs ----
sidebarPanel(
#show ct demo
actionBttn("runexample", "DEMO", style="simple", size="sm", color = "primary"),
# input1: Select a file ----
fileInput("file1", "Count matrix File (.xlsx)",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
#action run
actionBttn("runbutton", "GO", style="simple", size="sm", color = "primary"),
actionBttn("reset", "RESET", style="simple", size="sm", color = "warning"),
verbatimTextOutput(outputId = "reset"),
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Data file ----
span(textOutput("nrows"),style="color:blue"),
span(textOutput("ncols"),style="color:blue"),
tableOutput("matrix"),
)
)
)
)
),
server = function(input, output, session) {
###display demo count matrix
observeEvent(input$runexample, {
#ngenes
output$nrows <- renderText({paste("Number of genes: ", dim(mtcars)[1], " [First 10 rows displayed]")})
#nsamples
output$ncols<- renderText({paste("Number of genes: ", (dim(mtcars)[2]), " [First 10 rows displayed]")})
#display 10rows count matrix
output$matrix <- renderTable({
mtcars
})
}
)
observeEvent(input$reset, {
inputSweetAlert(
session = session, inputId = "mytext", input = "text",
title = "This is a free program, please leave your email:"
)
})
output$text <- renderPrint(input$mytext)
}
)
}
Here is an example using the modal dialog option provided by shiny. I trimmed your example down to the bits that mattered:
library(shiny)
if (interactive()) {
shinyApp(
ui <- fluidPage(
actionButton("reset", "RESET", style="simple", size="sm", color = "warning"),
verbatimTextOutput(outputId = "text")
),
server = function(input, output, session) {
l <- reactiveValues()
observeEvent(input$reset, {
# display a modal dialog with a header, textinput and action buttons
showModal(modalDialog(
tags$h2('Please enter your personal information'),
textInput('name', 'Name'),
textInput('state', 'State'),
footer=tagList(
actionButton('submit', 'Submit'),
modalButton('cancel')
)
))
})
# only store the information if the user clicks submit
observeEvent(input$submit, {
removeModal()
l$name <- input$name
l$state <- input$state
})
# display whatever is listed in l
output$text <- renderPrint({
if (is.null(l$name)) return(NULL)
paste('Name:', l$name, 'and state:', l$state)
})
}
)
}

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)

shinyfiles and renderUI don't work properly

I'm trying to use the shinyFiles library in my shinyApp, in order to give the user the possibility to select a group of files or a directory.
My idea is to use a uiOutput that changes depending on a checkbox selection.
Here I report the code, that maybe is more explicative than words
UtilityUI <- fluidPage(
titlePanel("page1"),
fluidRow(
column(2,
wellPanel(
tags$p("Check the box below if you want to choose an entire directory"),
checkboxInput(inputId = 'directory_flag', label = 'Directory path?', value = FALSE),
uiOutput("input_selection_ui")
)
),
column(8
#...
)
)
)
UtilityServer <- function(input, output, session) {
output$input_selection_ui <- renderUI({
if(input$directory_flag == TRUE) {
shinyDirButton(id = "infiles", label = "Choose directory", title = "Choose a directory")
} else {
shinyFilesButton(id = "infiles", label = "Choose file(s)", title = "Choose one or more files", multiple = TRUE)
}
})
shinyFileChoose(input, 'infiles', roots=getVolumes(), session=session, restrictions=system.file(package='base'))
shinyDirChoose(input, 'infiles', roots=getVolumes(), session=session, restrictions=system.file(package='base'))
}
shinyApp(UtilityUI, UtilityServer)
The problem borns when the "shinyFiles" button is pressed: the popup window doesn't load the roots, in both cases (shinyDirButton and shinyFilesButton).
If I don't use the uiOutput function everything works well... But in that case I cannot change my UI dinamically...
Thanks a lot for your replies,
Inzirio
It seems I can't get it to work either with renderUI(). Instead I implemented the same behavior using conditionalPanel() to show alternative buttons. This seems to work. Here is the code:
ui <- shinyUI(fluidPage(
checkboxInput(
inputId = 'directory_flag',
label = 'Directory path?',
value = FALSE
),
conditionalPanel(
"input.directory_flag == 0",
shinyFilesButton(
id = "infile",
label = "Choose file(s)",
title = "Choose one or more files",
multiple = TRUE
)
),
conditionalPanel(
"input.directory_flag == 1",
shinyDirButton(id = "indir", label = "Choose directory", title = "Choose a directory")
)
))
server <- shinyServer(function(input, output, session) {
shinyFileChoose(
input,
'infile',
roots = getVolumes(),
session = session,
restrictions = system.file(package = 'base')
)
shinyDirChoose(
input,
'indir',
roots = getVolumes(),
session = session,
restrictions = system.file(package = 'base')
)
})
shinyApp(ui, server)

Resources