How to render a tableoutput in another tab in Shiny? - r

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)

Related

How do I combine Shiny and esquisse together?

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)

how to use data output from reactive function as condition in conditional pannel

I am trying to read data and if the data has observation , the further UI has to expand. But the conditional Pannel is not working .
Here is the part of the UI and server code.
UI Code:
tabPanel("DMC 1.1", fluid = TRUE, useShinyjs(),
column(width=12,wellPanel(div(style = 'height:120px;',
fluidRow(column(width=9,style = "font-size: 12px;",fileInput("analysis_data2", label = "Import data", accept = c(".csv",".sas7bdat",".xls",".xpt"))))
)),
conditionalPanel(condition = "output.dataUpload", wellPanel(fluidRow(
column(width=5,textInput("subset","Subsettign Condition",value="")),
column(width=5,textInput("byvar","By Variable",value="")),
column(width=5,textInput("subgrp","SubGroup Variable",value="")),
column(width=5,textInput("trtvar","Treatment Variable",value="")),
column(width=5,textInput("xaxisvar","X Axis Variable",value="")),
column(width=5,textInput("yaxisvar","Y Axis Variable",value=""))
)))
)
)
Server Code:
#################Tab 2 - DMC 2.0#########################
analysis_d <- reactive({data_read(datain=input$analysis_data2)})
output$dataUpload <- reactive({return(!is.null(isolate(analysis_d())))})
outputOptions(output, 'dataUpload', suspendWhenHidden=FALSE)
This app will make an additional text input files available in the UI if the uploaded csv file contains at least one row:
library(shiny)
ui <- fluidPage(
fileInput("file", "File"),
conditionalPanel(
condition = "output.available",
textInput("additional_input", "additional input")
)
)
server <- function(input, output, session) {
data <- reactive(read.csv(input$file$datapath))
output$available <- reactive(nrow(data()) >= 1)
outputOptions(output, "available", suspendWhenHidden = FALSE)
}
shinyApp(ui, 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.

reactive expression result different

Sorry, I'm new for Shiny. I recently trying to use the reactive program to finish one project. But now I'm facing an issue not able to figure out why this happen.
First, I create a reactive expression which lists all files in one folder.
fullFilenames <- reactive({list.files(workDir, pattern="*.csv.gz", full.names=TRUE)})
Then, I observe a button event to trigger rescan the files in the folder and update the updateCheckboxGroupInput.
I set a breakpoint at "print("File Scaned")".
When we start the app, the Checkbox Group displays all files under folder correctly.
Then I remove/add files under working folder, then click rescan button. But the Checkbox Group does not get updated.
When program stopped at the breakpoint, I checked value "fullFilenames" and "fullFilenames()", and found that fullFilenames been updated, but not fullFilenames(). I trying to understand the logic behind two values are different and find a way how to update the Checkbox Group in a right way.
Can you please give some help on this issue?
Thank you very much.
Refer to below code:
library(shiny)
library(shinydashboard)
workDir<-c("c:/files")
setwd(workDir)
ui <- dashboardPage(
dashboardHeader(title = "Test Tool"),
dashboardSidebar(
sidebarMenu(
menuItem("Setup", tabName = "setup", icon = icon("cogs"))
)
),
dashboardBody(
tabItem(tabName = "setup",
fluidRow(
box(width = 12,
h1("Setup before generate dashboard"),
p(class = "text-muted",
br(),
"Default all files under working directory been selected, you also can choice your desired files.",
br(),
br()
),
fluidRow(
column(12, align="center",
actionButton("rescanFilesBtn","Rescan CSV Files")
)
)
)
),
fluidRow(
column(width = 12,
box(width = NULL, style = "max-height: 500px", status = "info", solidHeader = TRUE,
title = "Change raw CSV files as you like",
textInput("csvFileFilter","Desired CSV Files", placeholder = "File names, separate by semicolon"),
checkboxGroupInput("selectedFiles", "",
choiceValues = NULL,
choiceNames= NULL,
selected = NULL
)
)
)
)
)
)
)
server <- function(input, output, session) {
fullFilenames <- reactive({list.files(workDir, pattern="*.csv.gz", full.names=TRUE)})
observeEvent(input$rescanFilesBtn, {
print("Scan Files")
fullFilenames <- list.files(workDir, pattern="*.csv.gz", full.names=TRUE)
updateCheckboxGroupInput(session, "selectedFiles", choices = fullFilenames)
print("File Scaned")
})
}
shinyApp(ui, server)
I have modified your code so that you get an updated list after clicking the rescan button.
In the ui part I have changed your checkboxGroupInput as follows:
checkboxGroupInput("selectedFiles", "",
choices = list.files(workDir, pattern="*.csv.gz", full.names=TRUE),
selected = NULL)
This is done because the choices cannot be NULL.
I have just commented one line in your server code as your reactive value and local variable had the same name, due to which you were not getting any value for fullFilenames()
This is the modified server:
server <- function(input, output, session) {
fullFilenames <- reactive({list.files(workDir, pattern="*.csv.gz", full.names=TRUE)})
observeEvent(input$rescanFilesBtn, {
print("Scan Files")
# fullFilenames <- list.files(workDir, pattern="*.csv.gz", full.names=TRUE)
updateCheckboxGroupInput(session, "selectedFiles", choices = fullFilenames())
print("File Scaned")
})
}
Hope this helps!

Creating R-shiny app, want to use the names of people in CSV file as choices in selectInput

Not sure if this has been asked before. I am very new to working with RShiny apps, and I would like to use the values from a particular column of a particular CSV file for the choices in my selectInput() select box. Here is my code without the CSV, using some dummy variables.
ui <- shinyUI(fluidPage(
titlePanel(title = h4("PLAYER SELF-CENTERED RATING (PSCR)", align = "center")),
sidebarLayout(
sidebarPanel(
selectInput("selectplayer",
label = h3("Select box"),
choices = list("Choice 1" = 3,
"Choice 2" = 4,
"Choice 3" = 5),
selected = 3)
),
mainPanel(
plotOutput('radarPlot', width = "100%")
)
)
))
quite frankly, I'm fairly lost w.r.t where to begin on this. I also will need to use data from the CSV file to create another dataframe that is plotted in a renderPlot() call in shinyServer, so will need to find a way to get the CSV data into both server and ui. Is this a simple task, or something difficult? any help appreciated!
You can display uiOutput in ui and dynamically generate the ui in server. The code below should give you a hint.
library(shiny)
server <- function(input, session, output) {
# read csv here
datin <- read.table(text = 'Name,Age,Weight
John,10,40
Hary,20,70
Mike,30,80',
header = TRUE, sep =",", stringsAsFactors = FALSE)
output$select_1 = renderUI({
selectInput("select_input","select", choices = datin['Name'])
})
}
ui <- fluidPage(
uiOutput("select_1")
)
shinyApp(ui = ui, server = server)
You can generate dynamic output using uiOutput in the sidebar as shown in the following code:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "R-shiny app"),
dashboardSidebar(
uiOutput("columnNames") # Dynamic generate UI element
),
dashboardBody(
fluidRow(
column(10,
dataTableOutput('dataview')) #Display data in the tabular form
),
fluidRow(column(3, verbatimTextOutput("column_value"))),
hr()
)
)
server <- function(input, output) {
# Read data from .csv file
data=iris # (for understanding I am using iris dataset)
output$column_value <- renderPrint({
output$columnNames <- renderUI({
selectInput("datacolumn", h4("Select Data Column"), colnames(data)) # Dynamically set selectInput
})
output$dataview <- renderDataTable(data,options = list(pageLength = 10)) # Display the iris dataset
})
}
shinyApp(ui, server)

Resources