The code below takes a CSV file from the local computer and displays it on the main panel. Unfortunately, the code does not run, which means it does not display the intended CSV results while choosing options like "set1," "set2," and so on. I am new in R shiny.. Could anyone please assist me in resolving the problem?
library(here)
library(shiny)
set1_path <- here("set1.csv")
set2_path <-here("set2.csv")
set3_path <-here("set3.csv")
set4_path <- here("set4.csv")
set1<- read.csv(set1_path)
set2 <- read.csv(set2_path)
set3 <- read.csv(set3_path)
set4 <- read.csv(set4_path)
options(shiny.maxRequestSize=30*1024^2)
shinyApp(
ui = tagList(
navbarPage(
theme = "spacelab",
tabPanel("report extracting",
sidebarPanel(
fileInput("file1", "Select datasets:",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv"),
),
# tags$hr(),
# checkboxInput("header", "Header", TRUE),
# textInput("txt", "Study info:", "Study name read"),
# sliderInput("slider", "Tables to read:", 1, 100, 30),
# tags$h5("Prepare extraction"),
selectInput("pdfExtract1", "Pick a Domain", choices = c("domain1", "domain2", "domain3", "domain4")),
tableOutput("preview"),
actionButton("pdfExtract", "Extract", class = "btn-primary"),
# actionButton("dataset", "Extract", class = "btn-primary")
#Reading extracted datasets
# Input: Choose dataset ----
selectInput("dataset", "Choose a dataset:",
choices = c("SelectDataSet ", "set1", "set2", "set3", "set4")),
# Button
downloadButton("downloadData", "Download")
),
mainPanel(
tableOutput("table"),
# tableOutput("contents"),
tabsetPanel(
tabPanel("PDF File select",
h4("Domains"),
tableOutput("table"),
h3("Extracting..."),
# selectInput("pdfExtract1", "Pick a Domain", choices = c("domain1", "domain2", "domain3", "domain4")),
# tableOutput("preview"),
# actionButton("pdfExtract", "Extract", class = "btn-primary")
# downloadButton("download", "Download .tsv")
),
tabPanel("Raw data", "TBD"),
tabPanel("Summary data", "TBD")
)
)
), # end of first tabpanel
tabPanel("calculation",
sidebarPanel(
fileInput("file2", "Select datasets:",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
textInput("txt2", "domain info:", "report"),
sliderInput("slider", "Tables to read:", 1, 100, 30),
tags$h5("calculation"),
actionButton("dataset2", "Extract", class = "btn-primary")
),
mainPanel(
tableOutput("contents2"),
tabsetPanel(
tabPanel("Datasets",
h4("Domains"),
tableOutput("table2"),
h3("Summarizing...")
)
)
)
),
tabPanel("study compare",
sidebarPanel(
fileInput("file3", "compare:"),
textInput("txt3", "Study info:", "Study name read"),
tags$h5("calculation"),
actionButton("action2", "Compare", class = "btn-primary")
),
mainPanel(
tabsetPanel(
tabPanel("PDF File select",
h4("Domains"),
tableOutput("table3"),
h3("Comparing..."),
),
tabPanel("Summary data", "TBD")
)
)
)
)
),
server = function(input, output,session) {
# output$contents <- renderTable({
# # input$file1 will be NULL initially. After the user selects
# # and uploads a file, it will be a data frame with 'name',
# # 'size', 'type', and 'datapath' columns. The 'datapath'
# # column will contain the local filenames where the data can
# # be found.
# inFile <- input$file1
#
# if (is.null(inFile))
# return(NULL)
#
# read.csv(inFile$datapath, header = input$header)
# })
# output$contents2 <- renderTable({
# # input$file1 will be NULL initially. After the user selects
# # and uploads a file, it will be a data frame with 'name',
# # 'size', 'type', and 'datapath' columns. The 'datapath'
# # column will contain the local filenames where the data can
# # be found.
# inFile <- input$file2
#
# if (is.null(inFile))
# return(NULL)
# read.csv(inFile$datapath, header = input$header)
# })
# output$txtout <- renderText({
# paste(input$txt, input$slider, format(input$date), sep = ", ")
# })
output$table <- renderTable({
df <- c("datasetsx")
})
output$table2 <- renderTable({
df <- c("datasetsy")
})
output$table3 <- renderTable({
df <- c("datasetsz")
})
observeEvent(input$dataset, {
source("domain.R", local = TRUE)
})
observeEvent(input$dataset2, {
source("calculation.R", local = TRUE)
})
#Domain level Selection
observeEvent(input$pdfExtract, {
if(input$pdfExtract1 == "PP"){
source("domain1.R", local = TRUE)
}
else if(input$pdfExtract1 == "MA"){
source("domain2.R", local = TRUE)
}
else if (input$pdfExtract1 =="CL"){
source("domain3.R", local =TRUE)
}
else{
source("domain4.R", local =TRUE)
}
})
# Reactive value for selected dataset ----
datasetInput <- reactive({
switch(input$dataset,
"set1" = set2,
"set2" = set2,
"set3" = set3,
"set4" = set4)
})
# Table of selected dataset ----
output$table <- renderTable({
datasetInput()
})
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
paste(input$dataset, ".csv", sep = "")
},
content = function(file) {
write.csv(datasetInput(), file, row.names = FALSE)
}
)
}
)
I have made a minimum example from your Code. You have to work on a few things. At first you need an eventReactive:
datasetInput <- eventReactive( input$pdfExtract, {
switch(input$dataset,
"set1" = set1
) })
If the user picks a set, nothing is happening, until the user hits the "pdfExtract-button". Then a datasetinput is made. Which we call here to view in our main panel:
# Table of selected dataset ----
output$table <- renderTable({
datasetInput()
})
The whole program:
library(shiny)
set1 <- structure(list(This = c(1L, 7L, 3L), is_the = c(5L, 8L, 2L),
header = c(9L, 5L, 4L)), class = "data.frame", row.names = c(NA,
-3L))
options(shiny.maxRequestSize=30*1024^2)
ui <- fluidPage(
theme = "spacelab",
selectInput(inputId = "dataset", "Choose a dataset:",
choices = c("SelectDataSet ", "set1")),
actionButton(inputId = "pdfExtract", "Extract", class = "btn-primary"),
mainPanel(
tableOutput(outputId = "table"),
)
)
server = function(input, output,session) {
# Reactive value for selected dataset ----
datasetInput <- eventReactive( input$pdfExtract, {
switch(input$dataset,
"set1" = set1
) })
# Table of selected dataset ----
output$table <- renderTable({
datasetInput()
})
}
shinyApp(ui, server)
Additionally you have output$table <- renderTable which you call twice in server, i guess thats why R does not load any tables in your code.