Rshiny won't accept any inputID except "file" - shiny

no idea what the problem is here. Any time I give an inputID to fileInput other than "file", I'll get an error saying "Error: '[inputID here]' not found."
ui <- fluidPage(
titlePanel("BF591 Final"),
sidebarPanel(
fileInput("file1", "Upload metadata in CSV or TSV format.", accept = c(".csv", ".tsv"))
),
mainPanel(
tableOutput("sample_info_tb")
)
)
server <- function(input, output) {
load_meta <- reactive({
if(is.null(file1)){return()}
metadata_df <- read.csv(input$file1$datapath, row.names=1)
return(metadata_df)
})
options(shiny.maxRequestSize=30*1024^2)
output$sample_info_tb <- renderTable({
req(input$file1)
load_meta()
})
}
shinyApp(ui = ui, server = server)

Related

R Shiny -- Error in readLines: 'con' is not a connection

I am trying to run the following code, but keep getting this error:
Error in readLines: 'con' is not a connection
The following is my program:
library(shiny)
library(shinyFiles)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
width = 2,
fileInput("file1", "Choose the first HTML File", accept = ".html")
),
mainPanel(
p("File Name"),
verbatimTextOutput("file_name"),
htmlOutput(outputId = 'result')
)
)
)
server <- function(input, output) {
file <- reactive({input$file1})
req(file)
if(is.null( reactive(input$file1$datapath))) return(NULL)
rawHTML <- reactive(paste(readLines(input$file1$datapath), collapse="\n"))
output$file_name <- renderText({
print(rawHTML)
})
output$result <- renderUI({
getPage<-function() {
return(includeHTML( input$file1$datapath))
}
output$inc<-renderUI({getPage()})
})
}
runApp(list(ui = ui, server = server), launch.browser =T)
I want to read the html code from an external file, and save the code as a string. Is there a solution for this?

Hovering on icon should display text

Is there a way to add a icon next to "Country" and when the user hover on it, it should show some text
library(shiny)
ui <- fluidPage(
selectInput("Sel","Sel",choices = 1:100),
htmlOutput("Sd")
)
server <- function(input, output, session) {
output$Sd <- renderUI({
"Country"
})
}
shinyApp(ui, server)
library(shiny)
library(shinyBS)
ui <- fluidPage(
selectInput("Sel","Sel",choices = 1:100),
htmlOutput("Sd")
)
server <- function(input, output, session) {
output$Sd <- renderUI({
tags$span(
"Country ",
tipify(
icon("bar-chart"),
"Hello, I am the tooltip!"
)
)
})
}
shinyApp(ui, server)
With a bit of HTML and CSS
Then you can use CSS to customize the hovering text
library(shiny)
ui <- fluidPage(
# Add CSS
tags$head(
tags$style(HTML("
#an_icon .text {
position:relative;
bottom:30px;
left:0px;
visibility:hidden;
}
#an_icon:hover .text {
visibility:visible;
}
"))
),
selectInput("Sel","Sel",choices = 1:100),
htmlOutput("Sd"),
# HTML for the icon
tags$div(id = 'an_icon',
icon("bar-chart"),
tags$span(class = "text", tags$p("text")))
)
server <- function(input, output, session) {
output$Sd <- renderUI({
"Country"
})
}
shinyApp(ui, server)

disable/enable selectInput and fileInput upon the selection of Advanced checkboxInput

I have a Shiny code as like this
library(datasets)
ui <-fluidPage(
titlePanel("Telephones by region"),
sidebarLayout(
sidebarPanel(
selectInput("region", "Region:",
choices=colnames(WorldPhones)), checkboxInput(inputId = "Adv",
label = strong("Advanced"),
value = FALSE),fileInput("file1", "Choose CSV File",
multiple = FALSE,accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")),
hr(),
helpText("Data from AT&T (1961) The World's Telephones.")),
mainPanel(
plotOutput("phonePlot") )))
server <- function(input, output) {
output$phonePlot <- renderPlot({
barplot(WorldPhones[,input$region]*1000,
main=input$region,
ylab="Number of Telephones",
xlab="Year")})}
shinyApp(ui, server)
I need to implement following modifications
How to disable/enable selectInput and fileInput upon the selection of Advanced checkboxInput. If user choose advanced, the selectInput must be disable (vice versa)
How to use if function for fileInput from user input (Asia,Africa….ect one per line )
To enable/disable the inputs you can use package shinyjs.
Something like this should work:
library(datasets)
library(shiny)
ui <-fluidPage(
shinyjs::useShinyjs(),
titlePanel("Telephones by region"),
sidebarLayout(
sidebarPanel(
selectInput("region", "Region:",
choices=colnames(WorldPhones)),
checkboxInput(inputId = "Adv",
label = strong("Advanced"),
value = FALSE),
fileInput("file1", "Choose CSV File",
multiple = FALSE,accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")),
hr(),
helpText("Data from AT&T (1961) The World's Telephones.")),
mainPanel(
plotOutput("phonePlot") )))
server <- function(input, output) {
observe({
if((input$Adv == TRUE)) {
shinyjs::disable("region")
shinyjs::disable("file1")
} else {
shinyjs::enable("region")
shinyjs::enable("file1")
}
})
output$phonePlot <- renderPlot({
barplot(WorldPhones[,input$region]*1000,
main=input$region,
ylab="Number of Telephones",
xlab="Year")})}
shinyApp(ui, server)

Shiny failed to connect to ODBC

I am trying to have Shiny connects to Teradata.
Below is the code I have but I always get "ERROR: [on_request_read] connection reset by peer" after I choose the indicator and click the action button. Appreciated any input for this. Thanks.
ui <- shinyUI(fluidPage(
titlePanel("Generic grapher"),
sidebarLayout(
sidebarPanel(
numericInput("wafer", label = h3("Select Indicator:"),
value = NULL),
actionButton("do", "An action button")
),
mainPanel(
verbatimTextOutput("value"),
verbatimTextOutput("que"),
verbatimTextOutput("wq_print"),
dataTableOutput(outputId="pos")
)
)
)
)
library(markdown)
library(RODBC)
library(DBI)
library(sqldf)
ch<-odbcConnect("xxx", uid=" ",pwd=" ")
wq = data.frame()
server <- shinyServer(function(input, output){
values <- reactiveValues()
values$df <- data.frame()
d <- eventReactive(input$do, { input$wafer })
output$value <- renderPrint({ d() })
a <- reactive({ paste("SELECT * FROM dwname.tablename WHERE indicator_x = ", d(), sep="") })
output$que <- renderPrint({ a() })
observe({
if (!is.null(d())) {
wq <- reactive({ sqlQuery( a() ) })
output$wq_print <- renderPrint({ print(str(wq())) })
values$df <- rbind(isolate(values$df), wq())
}
})
output$pos <- renderDataTable({ values$df })
})
shinyApp(ui, server)

how to show contents of uploaded csv in shiny

My ui.R
library(shiny)
library(stats)
library(caret)
shinyUI(fluidPage(
titlePanel("Predicting Resources for Vessel"),
title = "Resource Prediction",
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose a Import BAPLE(.CSV) file to upload:",
accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")),
fileInput("file2", "Choose a Export BAPLE(.csv) file to upload:",
accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")),
fileInput("file3", "Choose a Import/Export containers yard location(.CSV) file to upload:",
accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")),
tags$hr(),
h4("Manual Input:"),
numericInput("Restow_40","Total Restows for 40ft Container:", 0, min = 0, max = 999999, step = 1),
textInput("Berth","Vessel Berth Location (CB3/CB4)"),
actionButton("submit", "Submit")
),
mainPanel(
tabsetPanel(
tabPanel("Raw Data", dataTableOutput("data")),
tabPanel("Output", verbatimTextOutput("pred_output"))
)
)
)
))
This is my server.r file
library(shiny)
library(stats)
#library(caret)
library(mlr)
library(data.table)
shinyServer(function(input, output) {
######################### Reading the required files ###################################
import_baple <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
read.csv(inFile$datapath)
})
export_baple <- reactive({
inFile <- input$file2
if (is.null(inFile)) return(NULL)
read.csv(inFile$datapath)
})
import_export_yard <- reactive({
inFile <- input$file3
if (is.null(inFile)) return(NULL)
read.csv(inFile$datapath)
})
output$data <- renderDataTable({
import_baple()
})
output$data <- renderDataTable({
export_baple()
})
output$data <- renderDataTable({
import_export_yard()
})
})
I want all the three files when uploaded to be displayed in Raw Data tab. When I upload first two files no content is displayed in the Raw Tab, but when I upload the third file content is displayed in the tab. I am not getting where I am doing it wrong.
Each input/output element needs a unique identifier, otherwise Shiny doesn't know which of the elements with the given identifier to use. So where you have:
tabPanel("Raw Data", dataTableOutput("data"))
In your UI and:
output$data <- renderDataTable({
import_baple()
})
output$data <- renderDataTable({
export_baple()
})
output$data <- renderDataTable({
import_export_yard()
})
In your server, what you actually need is more like:
# UI
tabPanel("Raw Data",
dataTableOutput("import_baple_data"),
dataTableOutput("explort_baple_data"),
dataTableOutput("import_export_data")
)
# Server
output$import_baple_data <- renderDataTable({
import_baple()
})
output$export_baple_data <- renderDataTable({
export_baple()
})
output$import_export_data <- renderDataTable({
import_export_yard()
})