I am trying to publish an R shiny app which includes an interactive Data Table with DT. The app runs locally on my server, without any errors and has full interactivity as expected. However, when I publish it to my shiny app io account, the DT does not render (The space where the table should be is blank).
Run the app locally, results are as expected:
The DT does not render at all (the space is blank) when the app is published to shiny apps io. This is the error message: "dataTableOutput and renderDataTable are masked from package: shiny."
Below I have added my code. I have indicated the start and end for the data table code with ##################DATA TABLE###############. This is where the problem is occurring. I think it has something to do with the if(interactive) statement, but I can not figure it out despite looking for answers.
The code for my UI is here:
rm(list = ls())
library(shiny)
library(shinydashboard)
library(leaflet)
library(tigris)
library(dplyr)
library(tidyverse)
library(leaflet.extras)
library(rmapshaper)
library(rsconnect)
library(plotly)
library(ggplot2)
library(rgdal)
library(htmltools)
library(shinyWidgets)
library(DT)
rsconnect::setAccountInfo(name='my-shiny-app-creation',
token='....1490B8EC60FD',
secret='....uzIrSxPA')
# Define UI
dashboardPage(
dashboardHeader(title = "UV and Cancer"),
dashboardSidebar(
sidebarMenu(
menuItem("Home", tabName = "Homepage", icon = icon("globe-americas")),
menuItem("SecondItem", tabName = "Second", icon = icon("search-location")),
#menuSubItem("Community Assets", tabName = "Local2", icon = icon("search-location")),
#menuSubItem("Local Insights", tabName = "Local3", icon = icon("search-location")),
menuItem("Resources", tabName = "Resources", icon = icon("book-reader")),
menuItem("About", tabName = "About", icon = icon("info")),
#)),
br(),
br(),
br(),
dropdown(label="South",
menuItem("Alabama", tabName="Alabama", icon=icon("gear")),
menuItem("Florida", tabName="FL", icon=icon("gear")),
menuItem("Georgia", tabName="GA", icon=icon("gear")),
menuItem("Mississippi", tabName="MI", icon=icon("gear")),
menuItem("South Carolina", tabName="MI", icon=icon("gear"))
),
dropdown(label="North",
menuItem("Maine", tabName="ME", icon=icon("gear")),
menuItem("New Hampshire", tabName="NH", icon=icon("gear")),
menuItem("Vermont", tabName="VT", icon=icon("gear")),
menuItem("Connecticut", tabName="CT", icon=icon("gear")),
menuItem("Rhode Island", tabName="RD", icon=icon("gear"))
),
dropdown(label="Central",
menuItem("North Dakota", tabName="ND", icon=icon("gear")),
menuItem("South Dakota", tabName="SD", icon=icon("gear")),
menuItem("Nebraska", tabName="NB", icon=icon("gear")),
menuItem("Oklahoma", tabName="OK", icon=icon("gear")),
menuItem("Texas", tabName="TX", icon=icon("gear"))
),
dropdown(label="West",
menuItem("Oregon", tabName="OR", icon=icon("gear")),
menuItem("California", tabName="CA", icon=icon("gear")),
menuItem("Nevada", tabName="NV", icon=icon("gear")),
menuItem("Washington", tabName="WA", icon=icon("gear")),
menuItem("Idaho", tabName="ID", icon=icon("gear"))
)
)
),
dashboardBody( # created dashboard body, where chart is displayed
tabItems(
tabItem(tabName = "Homepage",
# insert descriptive text here
h2("UV exposure and melanoma"),
h4("This is some text."),
br(),
h5("This is some subtext."),
br(),
h4("Here we can put some instructions."),
#tabBox(width = 100, height=500, h3("Discuss content"))
##################DATA TABLE###############
if(interactive()){
fluidRow(
column(2,
selectInput("State",
"State:",
c("All",
unique(as.character(combined_AL$State))))
),
column(2,
selectInput("Year",
"Year:",
c("All",
unique(as.character(combined_AL$Year))))
),
column(2,
selectInput("IncidValue",
"IncidValue:",
c("All",
unique(as.character(combined_AL$IncidValue))))
),
column(2,
selectInput("UVValue",
"UVValue:",
c("All",
unique(as.character(combined_AL$UVValue))))
),
box(DT::dataTableOutput("table"), width=12)
)},
),
##################DATA TABLE###############
tabItem(tabName = "Alabama",
h1("Alabama"),
h3("UV is the number one risk factor for skin cancer. Howver, in many cases, exposure is preventable.
This information is relevant at the local level. We can add more text here as necessary to elaborate."),
br(),
h2("Some stats about UV and cancer in melanoma:"),
fluidRow(
valueBox(1.6, "UV percent change from last year", icon = icon("thermometer-three-quarters"), color = "orange"),
valueBox(5, "This state's national rank", icon = icon("fire", lib = "font-awesome"), color = "yellow"),
valueBox(7, "Real time UV index value", icon = icon("notes-medical"), color = "red")
),
br(),
br(),
h2("Some stats about how this years measures compare to the nation:"),
fluidRow(
#box(plotlyOutput("count", width="120px", height="120px"), width=4,
# h4("Alabama counts compared to National for 2019.The gray shade is last years count for AL.")
# ),
box(plotlyOutput("rate", width="300px", height="120px"), width=12,
h4("Alabama rates compared to National for 2019. The gray shade is last years rate for AL."))
),
br(),
h3("Click on the tabs for more info:"),
tabBox(width = 12,
tabPanel(
h3("UV trends over time"),
h2("Average Year"),
plotlyOutput("Drill_Year"),
h2("Average month"),
plotlyOutput("Drill_Month")),
tabPanel(h3("Melanoma Incidence and UV"),
plotlyOutput("gender"),
br(),
plotlyOutput("race"),
br(),
plotlyOutput("UVmel"),
),
tabPanel(h3("Melanoma Mortality and UV")))
))
))
The code for the server is here:
#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(shinydashboard)
library(leaflet)
library(tigris)
library(dplyr)
library(tidyverse)
library(leaflet.extras)
library(rmapshaper)
library(rsconnect)
library(plotly)
library(ggplot2)
library(rgdal)
library(htmltools)
library(htmlwidgets)
library(shinyWidgets)
library(DT)
rsconnect::setAccountInfo(name='my-shiny-app-creation',
token='....B8EC60FD',
secret='....VwyHuzIrSxPA')
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
monthly <- read.csv(file="./Drilldown_Month.csv",
sep=",", header=TRUE)
annual <- read.csv(file="./Drilldown_Year.csv",
sep=",", header=TRUE)
monthly$Month <- factor(monthly$Month, levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
output$Drill_Year <-renderPlotly({
plot_ly(annual,
x=~Year,
y=~AvgYear,
type="scatter",
mode="lines")
})
output$Drill_Month <- renderPlotly ({
mouse_event <- event_data("plotly_hover") #tell plot where your mouse is. Make it reactive
#print(mouse_event)
#curveNumber pointNumber x y
#1 0 136 2016 1.22
#We want to store the year column, the third column, so:
year <- mouse_event[3] #x is the 3rd column which is year, so name it
#create a subset of the Month data. Select the months that correspond to the year mouseover event.
monthly_subset <- monthly[monthly$Year == year$x, ]
#plot
plot_ly(monthly_subset,
x=~Month,
y=~Value,
type="scatter",
mode="points+lines")
})
output$gender <-renderPlotly({
data2_AL <- read.csv("./Incidence_Gender_Alabama.csv")
mydata2 <- data2_AL %>%
spread(key=Gender, value=Value)
plot_ly(data=mydata2,
x = ~as.numeric(Year), y = ~Female,
type = "bar", name = "Female") %>%
add_trace(y = ~Male, name = "Male") %>%
layout(yaxis = list(title = "Incidence"), barmode = "group")
})
##################DATA TABLE###############
##output$combined_AL <-renderPlotly({ #NOT NEEDED in shiny app.
#server <- function(input, output) {
# output$table1 <- DT::renderDataTable({
# datatable(my_data)
# })
# }
# Filter data based on selections
#output$table <- DT::renderDataTable(DT::datatable({
output$table <- DT::renderDataTable({
combined_AL <- read.csv(file="./Combined_UV_Incid.csv",
sep=",", header=TRUE)
data <- combined_AL
if(input$State != "All") {
data <- data[combined_AL$State == input$State,]
}
if(input$Year != "All") {
data <- data[data$Year == input$Year,]
}
if(input$IncidValue != "All") {
data <- data[data$IncidValue == input$IncidValue,]
}
if(input$UVValue != "All") {
data <- data[data$UVValue == input$UVValue,]
}
datatable(data, extensions = 'Responsive')
})
#}) #Already above
##################DATA TABLE###############
output$race <-renderPlotly({
data3_AL <- read.csv("./Incidence_RaceEthnicity_Alabama.csv")
myREdata <- data3_AL %>%
spread(key=RaceEthnicity, value=Value)
plot_ly(data=myREdata,
x = ~as.numeric(Year), y = ~Black,
type = "bar", name = "Black") %>%
add_trace(y = ~Hispanic, name = "Hispanic") %>%
add_trace(y=~White, name="White")%>%
layout(yaxis = list(title = "Incidence"), barmode = "group")
})
output$UVmel <-renderPlotly({
data5_AL <- read.csv("./Combined_UV_Incid.csv")
data5_AL2 <- filter(data5_AL, State=='Alabama')
plot_ly(data5_AL2, x = ~Year, y = ~IncidValue,
text=~paste('Year: ', data5_AL2$Year, '<br> Incidence Value: ', data5_AL2$IncidValue),
#text=~paste('Date: ', format(secondaxis_v2_RTmock$Date, "%B %d")),
hoverinfo='text',
type = "scatter",
mode="lines+ markers",
#fill="tozeroy",
name = "Incidence",
fillcolor="#FF663380",
line=list(color="FF663380")) %>%
add_trace(x=~Year, y=~UVValue,
text=~paste('Year: ', data5_AL2$Year, '<br> UV Value: ', data5_AL2$UVValue),
#text=~paste('Date: ', format(secondaxis_v2_RTmock$Date, "%B %d")),
hoverinfo='text',
mode="lines",
yaxis = "y2",
type="scatter",
name="UV",
line=list(dash='dot', width=2, color="red"),
#line = list(color = "red"),
fillcolor="FF000000",
line=list(color="FF000000"))%>%
layout(
#xaxis=list(tickformat="%B"),
yaxis=list(title="Melanoma Incidence", useHTML=TRUE),
yaxis2 = list(overlaying = "y", side = "right",
title=(paste("UV daily dose")), useHTML=TRUE),
title=("Melanoma Incidence and UV concentration over time")
)
})
output$count <-renderPlotly({
plot_ly(
type = "indicator",
mode = "number+gauge+delta",
value = 220,
domain = list(x = c(0, 1), y= c(0, 1)),
title = list(text = "<b>Count</b>"),
delta = list(reference = 280),
gauge = list(
shape = "bullet",
axis = list(range = list(NULL, 300)),
threshold = list(
line = list(color = "red", width = 2),
thickness = 0.75,
value = 280),
steps = list(
list(range = c(0, 120), color = "lightgray"),
list(range = c(150, 250), color = "white"))),
height = 130, width = 670) %>%
layout(margin = list(l= 100, r= 50))
})
output$rate<-renderPlotly({
plot_ly(
type = "indicator",
mode = "number+gauge+delta",
value = 180,
domain = list(x = c(0, 1), y= c(0, 1)),
title = list(text = "<b>Rate</b>"),
delta = list(reference = 120),
gauge = list(
shape = "bullet",
axis = list(range = list(NULL, 300)),
threshold = list(
line = list(color = "red", width = 2),
thickness = 0.75,
value = 120),
steps = list(
list(range = c(0, 150), color = "lightgray"),
list(range = c(150, 250), color = "white"))),
height = 130, width = 670) %>%
layout(margin = list(l= 100, r= 50))
})
})