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)
Related
As a newcomer to Shiny, I've written some code that I'd like to enhance by enabling users to filter the data from an uploaded table. To achieve this, I've created an input ID named "SelectNames," but I'm struggling to connect it to the table that I generated from the uploaded data. Ultimately, I hope to leverage this input to perform additional processing, so any guidance on how to accomplish this would be greatly appreciated.
## Load the libraries
library(shiny)
library(shinythemes)
library(vroom)
library(dplyr)
library(lubridate)
library(tidyverse)
library(xml2)
library(readxl)
library(gbp)
library(plotly)
library(shinyjs)
library(DT)
library(knitr)
ui = fluidPage(
# Select the Theme
theme = shinytheme('flatly'),
tabsetPanel(type = "tabs",
tabPanel(
titlePanel("generate QR Code"),
sidebarLayout(
sidebarPanel(
fileInput(inputId = "excelfile",
label = "Upload Excel file",
accept = ".xlsx"),
br(),
selectInput(inputId = "SelectNames",
label = "Select Names") ),
mainPanel( type = 'tabs',
tabPanel(title = 'Page1',
h5('Order Details'),
DT::dataTableOutput(outputId = 'Transactions'))
)
)
)
)
)
server = function(input, output, session) {
data_rec = reactive({
# read XML file
excel_file <- read_excel(input$excelfile$datapath)
# Create an empty data frame to store the extracted information
products_df <- data.frame(ProductName = character(),
ActualQuantity = character(),
Order_ID = character(),
Dispatched_To = character(),
stringsAsFactors = FALSE)
names = distinct(products_df$Dispatched_To)
})
output$Transactions = DT::renderDataTable({
DT::datatable(data = data_rec())
})
}
shinyApp(ui = ui, server = server)
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)
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.
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)
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)