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.
Related
I want to create a shiny app that allows me to upload a file then make some change on the data uploaded (very important step) and then finally use esquisse to visualize the data.
My code doesn't work. can someone see the problem?
library(esquisse)
library(shiny)
ui <- fluidPage(
titlePanel("Use esquisse as a Shiny module"),
sidebarLayout(
sidebarPanel(
fileInput("file", "Data", buttonLabel = "Upload..."),
),
mainPanel(
tabsetPanel(
tabPanel(
title = "Data analysis",
esquisse_ui(
id = "esquisse",
header = FALSE # dont display gadget title
)),))))
server <- function(input, output, session) {
data_r = reactive({
req(input$file)
dt = read.csv(input$file$datapath, header = T, sep = ",")
dt
})
esquisse_res <- callModule(
module = esquisse_server,
id = "esquisse",
data = data_r()
)
}
shinyApp(ui, server)
I'm building a simple website using Shiny,that allow users to uplaod a csv,xls ... file within Getting the data tab and view it in another tab named Viewing the data and then plot that data in another tab visualizing the data . for instance i want just to render a table based on the data picked ,
Here's a snippet of what i tried :
ui :
ui <- fluidPage(
sidebarLayout(
sidebarPanel("APRIORI INPUTS",id="panelTitle"),
mainPanel(
tabsetPanel(
tabPanel(title = "Getting the data",icon = icon("database"),
tags$div(id="uploadFiles",
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))
)
),
tabPanel(title = "Viewing the data",icon = icon("eye"),
tableOutput("Viewing_the_data")),
tabPanel(title = "visualizing the data",icon = icon("chart-bar"),
tableOutput("visualizing_the_data"))
)
),
)
)
for the server logic :
server :
server <- function(input, output){
output$Viewing_the_data <- renderTable({
req(input$uploadFiles)
read.csv(input$selection$datapath)
})
}
shinyApp(ui = ui, server = server)
I tried that but doesn't work ...
PS : i tried that with shinydashboard and it works perfectly as that : r shiny - display data frame after uploading
Any suggestions or advice would be appreciated. Thanks.
I've tried to adapt the example I mentioned with your app, removing some of the complexity to make it a simpler app but still have the tabbed structure and it works when I run it. I can choose the file in one tab, select how many rows to show in the sidebar and show the data in another tab:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel("APRIORI INPUTS",id="panelTitle",
numericInput("n", "Rows", value = 5, min = 1, step = 1)),
mainPanel(
tabsetPanel(
tabPanel(title = "Getting the data", fileInput("file", NULL, accept = c(".csv", ".tsv")), icon = icon("database")),
tabPanel(title = "Viewing the data", tableOutput("head"), icon = icon("eye")))
)
)
)
server <- function(input, output, session) {
data <- reactive({
req(input$file)
ext <- tools::file_ext(input$file$name)
switch(ext,
csv = vroom::vroom(input$file$datapath, delim = ","),
tsv = vroom::vroom(input$file$datapath, delim = "\t"),
validate("Invalid file; Please upload a .csv or .tsv file")
)
})
output$head <- renderTable({
head(data(), input$n)
})
}
# Run the application
shinyApp(ui = ui, server = server)
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)
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)
Want to write an app to dynamically compare diffrent versions of xlsx-files using shiny and comparedf packages.
There was a problem with output of the comparison results in html format.
How to fix it? It is possible to output html with htmlTable package?
library(shiny)
library(compareDF)
library(htmlTable)
ui <- navbarPage("Compare_app",
tabPanel("Compare relults",
sidebarLayout(
sidebarPanel(
fileInput(inputId = "old_file",
label = "Chose old file",
accept = c(".xlsx")),
fileInput(inputId = "new_file",
label = "Chose new file",
accept = c(".xlsx")),
selectInput(inputId = "group_cols",
label = "Group by:",
choices = "",
selected = NULL,
multiple = TRUE)
),
mainPanel(
mainPanel(
htmlOutput(outputId = "compare_html")
)
)
)
)
)
server <- function(input, output, session) {
old_file <- reactive({
old_file_tmp <- read_excel(req(input$old_file$datapath), col_names = TRUE)
})
new_file <- reactive({
old_file_tmp <- read_excel(req(input$new_file$datapath), col_names = TRUE)
})
observeEvent(old_file(), {
updateSelectInput(session, "group_cols", choices = names(old_file()))
})
output$compare_html <- renderUI({
vec <- unlist(strsplit(input$group_cols, ","))
compare_result_tmp <- compareDF::compare_df(df_new = new_file(), df_old = old_file(), group_col = vec)
compare_html <- compare_result_tmp$html_output
})
)
# Run the application
shinyApp(ui = ui, server = server)
This can be trivially implemented by using the native shiny command -
compare_html <- shiny::HTML(compare_result_tmp$html_output)
This should give output in the expected format