I'm trying to upload a shiny app on Shiny.io. The app gets deployed and when the link is tried the app gets crashed by throwing an error Disconnected from Server. When I checked in the Logs of the dashboard, it says Error in the server: could not find function "server".
I was not able to find the solution for this. Documents and articles regarding the same shows that the Packages used could be one of the reasons for the error but I'm not able to find the list of packages that will be compatible or not.
These are the list of packages/libraries that are used in my app,
Shiny
Earth
ggplot2
Plot3D
visreg
rgl
zoo
Hmisc
dplyr
shinyBS
shinycssloaders
Thanks in Advance!!
UPDATE
Below are the reproducible ui.R and server.R scripts.
Upon debugging I found that this part of the code is error while deploying.
ui.R
library(shiny)
library(shinyBS)
library(shinycssloaders)
options(shiny.trace=TRUE)
shinyUI(pageWithSidebar(
fluidRow(
column(width = 4,height = 4,img(src='image.png', align = "left", height =
50, width = 200)),
column(8,titlePanel("Analysis"))
),
sidebarPanel(
br(),
fileInput("file1", label = (" Data "),multiple = F),
fluidRow(
column(12,align ="center", actionButton("button", "Analyze",style =
"background-color : skyblue", icon = icon("stats", lib =
"glyphicon"),width = 250 )))
),
mainPanel(
bsAlert("alert"),
br(),
fluidRow(
tabsetPanel(
tabPanel("Table",icon =
icon("table"),withSpinner(dataTableOutput('table'), type =
getOption("spinner.type", default = 8) ))
)
)
)
))
server.R
library(shiny)
library(shiny)
library(earth)
library(ggplot2)
library(plot3D)
library(visreg)
library(rgl)
library(zoo)
library(Hmisc)
library(dplyr)
library(gridExtra)
options(shiny.maxRequestSize=30*1024^2)
options(shiny.trace=TRUE)
if (interactive()){
shinyServer(function(input, output,session) {
dataframe <- reactive( {
### Create a data frame reading data file to be used by other
functions..
inFile <- input$file1
data1 <- read.csv(inFile$datapath, header = TRUE)
})
table1<- eventReactive(input$button, dataframe())
output$table <- renderDataTable({table1()})
})
}
Thanks!
Finally I was able to debug the code and find a solution for the error.
From Server.R delete the statement if (interactive()) and delete session parameter from the shinyServer(function(input,output,session)).
Hence deployed without any error.
Replace the following server.R script and it should work fine.
library(shiny)
library(shiny)
library(earth)
library(ggplot2)
library(plot3D)
library(visreg)
library(rgl)
library(zoo)
library(Hmisc)
library(dplyr)
library(gridExtra)
options(shiny.maxRequestSize=30*1024^2)
options(shiny.trace=TRUE)
shinyServer(function(input, output) {
dataframe <- reactive( {
### Create a data frame reading data file to be used by other functions..
inFile <- input$file1
data1 <- read.csv(inFile$datapath, header = TRUE)
})
table1<- eventReactive(input$button, dataframe())
output$table <- renderDataTable({table1()})
})
Related
I am building a shinyApp to display COVID-19 data. I have a file in long format that displays the day, county, positive cases, recoveries, and deaths. I am attempting to make the app where a user can select a county from a drop down menu and it will display 3 graphs of positives, recoveries, and deaths on the page. The graphs will have x-axis be dates and y-axis as a variable. Attached is the script I have so far. I have tried many different approachers, but I have no idea what to do. I am still learning R and have no prior experience with ShinyApp. Any advice or help would be appreciated. I think I have the ggPlot and output/UI right, the server logic is what is throwing me for a loop. Even just a link to a good guide would be nice. Thanks!
7/23/2020: I have updated the code. I looked in ggplot some. When I run the app, I now have the dropdown menu I wanted, but the graphs are displaying. When I create the ggplot in the console to make sure the code works on its own, I am missing the middle protion of the graph? Any ideas/fixes?
library(shiny)
library(dplyr)
library(tidyr)
library(plotly)
library(ggplot2)
library(rsconnect)
df <- read.csv("C:/Users/Nathan May/Desktop/Research Files (ABI)/Covid/Data For Shiny/Appended_File/Appended_Scraped_Files.csv") #INSERT PATH SINGLE FILE OPTION
datapos <- df[c(2,6,3)]
rsconnect::setAccountInfo(name='nathanjmay', token='A3CF4CC3DE0112B8B9F8D0BA429223D3', secret='TNwC9hxwZt+BffOhFaXD3FQsMg3eQnfaPGr0eE8S')
#UI
ui <- fluidPage(
titlePanel("COVID-19 in Arkansas Counties"),
fluidRow(
column(
width=4,
selectizeInput("County", label=h5("County"), choices= data$Counties, width="100%")
)),
fluidRow(
plotOutput(outputId = "Positive")
),
fluidRow(
plotOutput(outputId = "Recoveries")
),
fluidRow(
plotOutput(outputId = "Deaths")
),)
#SERVER
server= function(input, output) {
data <- reactive({
datapos %>% filter(County == input$County)
#GGPLOT2 for Positive
output$Positive -> renderPlot(ggplot(data=datapos, aes(x=Day, y=Positive)) +
geom_bar(stat="identity"))
#Recoveries
output$Recoveries -> renderplot()
#Deaths
output$Deaths -> renderplot()
})
}
shinyApp(ui=ui, server=server)
You're assigning all reactive expressions to the data object in the server logic, look at where you close the curly bracket. So everything get wrapped into data and nothing about your plotOutput, i.e. output$Positive, output$Recoveries, output$Death are specified in your server logic. Also the way to use reactive() feel a little awkward at first. Here's my super simply app to illustrate what you ought to do wrt to using reactive(). Again notice where you open and close the curly bracket and parentheses.
So the chain of reactions defined here are: input$state >> dat via reactive() >> output$dummy via renderPlot().
library(shiny)
library(dplyr)
library(ggplot2)
#### Fake data
df <- data.frame(state = rep(c("FL", "GA"), each = 2),
x = rnorm(4),
y = rnorm(4))
#### UI
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
selectInput("state", "Choose a state:",
list(`Florida` = "FL",
`Georgia` = "GA")
),
mainPanel(
plotOutput("dummy")
)
)
)
#### Server
server <- function(input, output) {
## Essential dat is the filtered df
dat <- reactive({
df %>%
filter(state == input$state)
})
## Use dat() to access the filtered df instead of dat
output$dummy <- renderPlot({
ggplot(dat()) +
geom_point(aes(x = x, y = y))
})
}
# Run the application
shinyApp(ui = ui, server = server)
I have an interactive visualization that connects to a city government's police data API.
When certain combinations of inputs are selected, my API call comes back empty and I get a nasty red error message (as my plot inputs are unavailable).
Can someone tell me how to display a more informative error message along the lines of, "there are no incidents matching your selection, please try again"? I would like this error message to appear as a showNotification and my ggplot not to render.
Below is an extremely stripped down version of what I am doing. Note how when a combination like "AVONDALE" and "CHEMICAL IRRITANT" is selected, the chart renders, whereas when a combination like "ENGLISH WOODS" and "TASER-BEANBAG-PEPPERBALL-40MM FOAM" is selected, an error message is returned. This error message is what I would like to address with a showNotification alert.
Note that this uses the Socrata API, so the package RSocrata must be installed and loaded.
install.packages("RSocrata")
library(shiny)
library(reshape2)
library(dplyr)
library(plotly)
library(shinythemes)
library(tibble)
library(RSocrata)
# Define UI for application that draws a histogram
ui <- fluidPage(
navbarPage("Example",
theme = shinytheme("united"),
tabPanel("Plot",
sidebarLayout(
sidebarPanel(
# neighborhood selector
selectizeInput("neighbSelect",
"Neighborhoods:",
choices = c("AVONDALE", "CLIFTON", "ENGLISH WOODS"),
multiple = FALSE)),
# incident description selector
selectizeInput("incSelect",
"Incident Type:",
choices = c("CHEMICAL IRRITANT", "TASER-BEANBAG-PEPPERBALL-40MM FOAM"),
multiple = FALSE))
),
# Output plot
mainPanel(
plotlyOutput("plot")
)
)
)
# Define server logic
server <- function(input, output) {
forceInput <- reactive({
forceInput <- read.socrata(paste0("https://data.cincinnati-oh.gov/resource/e2va-wsic.json?$where=sna_neighborhood= '", input$neighbSelect, "' AND incident_description= '", input$incSelect, "'"))
})
# Render plot
output$plot <- renderPlotly({
ggplot(data = forceInput(), aes(x = sna_neighborhood)) +
geom_histogram(stat = "count")
})
}
# Run the application
shinyApp(ui = ui, server = server)
Thank you so much for any help anyone can offer!
Im going to give an example with the shinyalert library to have the popup. Here I added the sample choice TEST to indicate no data:
#install.packages("RSocrata")
library(shiny)
library(reshape2)
library(dplyr)
library(plotly)
library(shinythemes)
library(tibble)
library(RSocrata)
library(shinyalert)
# Define UI for application that draws a histogram
ui <- fluidPage(
useShinyalert(),
navbarPage("Example",
theme = shinytheme("united"),
tabPanel("Plot",
sidebarLayout(
sidebarPanel(
# neighborhood selector
selectizeInput("neighbSelect",
"Neighborhoods:",
choices = c("AVONDALE", "CLIFTON", "ENGLISH WOODS","TEST"),
multiple = FALSE)),
# incident description selector
selectizeInput("incSelect",
"Incident Type:",
choices = c("CHEMICAL IRRITANT", "TASER-BEANBAG-PEPPERBALL-40MM FOAM"),
multiple = FALSE))
),
# Output plot
mainPanel(
plotlyOutput("plot")
)
)
)
# Define server logic
server <- function(input, output,session) {
forceInput <- reactive({
forceInput <- read.socrata(paste0("https://data.cincinnati-oh.gov/resource/e2va-wsic.json?$where=sna_neighborhood= '", input$neighbSelect, "' AND incident_description= '", input$incSelect, "'"))
if(nrow(forceInput)==0){
shinyalert("Oops!", "No data returned", type = "error")
forceInput <- NULL
}
forceInput
})
# Render plot
output$plot <- renderPlotly({
req(forceInput())
ggplot(data = forceInput(), aes(x = sna_neighborhood)) +
geom_histogram(stat = "count")
})
}
# Run the application
shinyApp(ui = ui, server = server)
Back again. Working on a project and I'm stuck. My click isn't working. I've tried every iteration and can't figure it out. Basically I want to select multiple lines in a datatable via a click, at which point I'll do some more filtering. The click I'm having issues with. Here's my code... Do you see anything I'm missing? Thanks.
library(forecast)
library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)
library(scales)
library(DT)
library(forecast)
library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)
library(scales)
library(DT)
source("NEW.R", local = TRUE)
branch1 <- unique(distinctlineitems$BRANCH)
ui <- navbarPage(
theme = shinytheme("cosmo"),
title = "EXPENDITURES",
tabPanel("TAB1",
sidebarLayout(
sidebarPanel(
checkboxGroupInput("branches",label = NULL,choices = branch1 ,selected = NULL),
actionButton('selectallB','Select All'),
textInput("words", "Search"),
h5("Separate keywords with commas."),
plotOutput("plot", width = "100%"),
plotOutput("season", width = "100%")),
# Show a plot of the generated distribution
mainPanel(
fluidRow(csvDownloadUI("dwnld", "DOWNLOAD"), style = "padding:10px"),
DT::dataTableOutput("table")
server <- function(input, output, session) {
branchfilter <- reactive({
filt <- distinctlineitems[distinctlineitems$BRANCH %in% input$branches,]
return(filt)
})
graphids <- reactive({
if(length(input$table_rows_selected) < 1) return(NULL)
id <- input$table_rows_selected
x <- branchfilter()$REMARKS[id]
})
output$table <- renderDataTable({
test <- DT::datatable(branchfilter(),
filter = "top",
rownames = FALSE,
selection = "multiple")
})
Turns out I was able to answer my own question on this one. Because I was trying to test it under a Reactive I was unable to see the output. In order to test, I had to wrap in an observe statement. So easy. After the fact. Thanks tobiaseli_te.
observe(print(graphids()))
I am trying to print dataset values in shiny web app. But I am only able to print data set name using below code. How can I print dataset values?
library(MASS)
library(shinythemes)
library(shiny)
library(ggplot2)
mass.tmp <- data(package = "MASS")[3]
mass.datasets <- as.vector(mass.tmp$results[,3])
ui <- fluidPage(
theme = shinytheme("superhero"),
titlePanel("Linear Regression Modelling"),
sidebarLayout(
sidebarPanel(
selectInput("dsname", "Dataset:",choices = c(mass.datasets))
,
uiOutput("x_axis")
# ,
# textOutput("txt"),
# tableOutput("tab")
),
mainPanel(
tags$br(),
tags$br()
)
)
)
server <- function(input, output) {
num_ds <- function(ds)
{
nums <- sapply(ds,is.numeric)
num_ds <- ds[,nums]
return(num_ds)
}
ds_ext <- reactive({ num_ds(input$dsname) })
output$x_axis <- renderUI({
col_opts <- get(ds_ext())
selectInput("x_axis2", "Independent Variable:", choices = names(col_opts))
})
}
shinyApp(ui = ui, server = server)
Actually I am trying to solve error in above code "Incorrect number of dimensions". I have written function which would return data frame with only numeric variables so that I can analyze. But getting error in line I guess where I am creating object x_axis. pls help.
Much like the edit function in R, I would like to manually make changes to a data frame from within Shiny. I have been to the shiny website
http://shiny.rstudio.com/gallery/datatables-options.html
however, I have not find a place where I can manually change data.
you can do what you want with shinysky package. It offer functionality to edit your tables on demand. Below is a simple example with mtcars dataset where you can change the table content and then download the updates you introduced during the session. You can easily add the file input to read .csv files yourself
rm(list = ls())
library(shiny)
library(shinydashboard)
library(shinysky)
ui <- dashboardPage(
dashboardHeader(title = "How to edit a table"),
dashboardSidebar(sidebarMenu(id = "tabs",menuItem("Menu Item 1", tabName = "one", icon = icon("dashboard"))
)),
dashboardBody(
tabItems(tabItem(tabName = "one",hotable("hotable1"),downloadButton('downloadData', 'Download')))
))
server <- function(input, output) {
previous <- reactive({mtcars})
sample_data <- reactive({
if(is.null(input$hotable1)){return(previous())}
else if(!identical(previous(),input$hotable1))
{
sample_data <- as.data.frame(hot.to.df(input$hotable1))
sample_data
}
})
output$hotable1 <- renderHotable({sample_data()}, readOnly = F)
output$downloadData <- downloadHandler(filename = function() {paste(Sys.time(), '- My New Table.csv', sep='') },content = function(file) {write.csv(sample_data(), file, row.names = FALSE)})
}
shinyApp(ui, server)
The downloaded file looks like so: