I try to print out text in information section, to plot graphs separately for cases and deaths of cover 19 and make a summary table for each month in summary section.
But now, only sidebar shows, text and graphs don't show up at all.
I guess there are mistakes in server.R
But,I check code several times and still have no idea what the problem is. Does anyone can help?
ui.R
library(shiny)
library(ggplot2)
library(dplyr)
data<-read.csv('https://raw.githubusercontent.com/lingling1123/covid-19-data/master/us.csv')
data$date<-as.numeric(as.Date(data$date,origin='2020-01-21'))-18281
data$cases<-data$cases/1000
data$deaths<-data$deaths/1000
Jan<-c(1:11)
Feb<-c(12:40)
Mar<-c(41:71)
Apr<-c(72:101)
May<-c(102:132)
Jun<-c(133:162)
Jul<-c(163:193)
Aug<-c(194:224)
Sep<-c(225:254)
Oct<-c(255:285)
Nov<-c(286:298)
# Define UI for application that draws a histogram
shinyUI(navbarPage('All you can know',
# Information
tabPanel('Information',
mainPanel(
textOutput("info"),
)
),
# Summaries
tabPanel('Summaries',
sidebarLayout(
sidebarPanel(
h4('You can create plots using the radio buttons below.'),
radioButtons('var',h5('Select the variable '),choices=list('cases','deaths')),
h4('You can create numerical summaries below.'),
selectizeInput("mon", h5('Select the month you want to check'),selected = "Jan", choices = c('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct'))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
tableOutput("table")
)
)
)))
server.R
shinyServer(function(input,output,session) {
output$info <- renderPrint({
print('For this project, I use Covid19 dataset that incluses date, cases and deaths to make a exposure notification app.')
})
value<-reactive({
variables<-input$var
})
output$distPlot <- renderPlot({
if(value()=='cases'){
ggplot(data,aes(x=date,y=cases))+geom_point()
}else{
ggplot(data,aes(x=date,y=deaths))+geom_point()
}
}
)
}
)
Move the data load section from your ui.r to server.r, e.g.
ui.r
library(shiny)
library(ggplot2)
library(dplyr)
# Define UI for application that draws a histogram
shinyUI(navbarPage('All you can know',
# Information
tabPanel('Information',
mainPanel(
textOutput("info"),
)
),
# Summaries
tabPanel('Summaries',
sidebarLayout(
sidebarPanel(
h4('You can create plots using the radio buttons below.'),
radioButtons('var',h5('Select the variable '),choices=list('cases','deaths')),
h4('You can create numerical summaries below.'),
selectizeInput("mon", h5('Select the month you want to check'),selected = "Jan", choices = c('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct'))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
tableOutput("table")
)
)
)))
server.r
data<-read.csv('https://raw.githubusercontent.com/lingling1123/covid-19-data/master/us.csv')
data$date<-as.numeric(as.Date(data$date,origin='2020-01-21'))-18281
data$cases<-data$cases/1000
data$deaths<-data$deaths/1000
Jan<-c(1:11)
Feb<-c(12:40)
Mar<-c(41:71)
Apr<-c(72:101)
May<-c(102:132)
Jun<-c(133:162)
Jul<-c(163:193)
Aug<-c(194:224)
Sep<-c(225:254)
Oct<-c(255:285)
Nov<-c(286:298)
shinyServer(function(input,output,session) {
output$info <- renderPrint({
print('For this project, I use Covid19 dataset that incluses date, cases and deaths to make a exposure notification app.')
})
value<-reactive({
variables<-input$var
})
output$distPlot <- renderPlot({
if(value()=='cases'){
ggplot(data,aes(x=date,y=cases))+geom_point()
}else{
ggplot(data,aes(x=date,y=deaths))+geom_point()
}
}
)
}
)
Related
UPDATE: Q2 has been answered. Thanks #Ben! Just looking for assistance with Q1 and selectize.
What I have: Using selectize the user can search a pension plan. Q1: How can I avoid a default plan and simply have a "enter here..."?
What I want: While the plan selection works fine, I'm trying to plot the age of plan members when that plan is selected. Q2: How can I make my output (age) reactive to the input (plan name)?
My ui.r:
fluidPage(
titlePanel("Atlantis Pension Dashboard"),
sidebarLayout(
sidebarPanel(
#Dynamic select type
selectizeInput('Plan_Name', 'Search plan by keyword', choices = unique(Active$Plan_Name)),
multiple = TRUE,
#Static drop down
selectInput('Plan_Name', "All plans",
choices=unique(Active$Plan_Name)),
hr(),
helpText("Data from...")
),
# Spot for hist
mainPanel(
plotOutput("agePlot")
)))
and my server.r:
function(input, output) {
Plan <- reactive(input$Plan_Name)
# Dynamic plan select
output$Plan_Name <- renderPrint({
str(sapply(sprintf('Plan_Name'), function(id) {
input[[id]]
}, simplify = FALSE))
})
# Age histogram dependant on the chosed plan
output$agePlot <- renderPlot({
req(Plan())
hist(Active[Plan()]$age)
}
)
}
What I've tried: damn near everything (being dumb doesn't help).
Can someone helps me out?
I already tried to look at different tutorials, and previous questions/answers on Stack. But nothing helped me out.
I am creating a Shiny app, which would show a different output (data table) based on the input value.
Here is my code so far:
library(shiny)
library(DT)
# Define UI for miles per gallon app ----
ui <- pageWithSidebar(
# App title ----
headerPanel("Clients per Township - Aggregation"),
# Sidebar panel for inputs ----
sidebarPanel(
helpText("Mean client's penetration (Number of Clients/Number of inhabitants) = 0.0089"),
selectInput("Choice", "Do you want to have a list of townships with client's penetration above or under the mean?", c(" ", "Above","Under"))),
# Main panel for displaying outputs ----
mainPanel(
conditionalPanel(
'input.Choice === "Above"',
DT::dataTableOutput("more_table")
),
conditionalPanel(
'input.Choice === "Under"',
DT::dataTableOutput("less_table")
)
)
)
# Define server logic to plot various variables against mpg ----
server <- function(input, output) {
more_table = DT::renderDataTable({
more_than_mean
})
less_table = DT::renderDataTable({
less_than_mean
})
}
shinyApp(ui, server)
The data tables more_than_mean, and less_than_mean were previously computed.
When I run the app, I don't get an error. BUT no output is showed.
Can someone helps me out? I don't understand why none of the two tables are showed when I run the app.
Thank you!
So after looking carefully again at my code, I found my (stupid) mistake.
I only forgot the "ouput$" before the name of the output tables!
library(shiny)
library(DT)
# Define UI for miles per gallon app ----
ui <- pageWithSidebar(
# App title ----
headerPanel("Clients per Township - Aggregation"),
# Sidebar panel for inputs ----
sidebarPanel(
helpText("Mean client's penetration (Number of Clients/Number of inhabitants) = 0.0089"),
selectInput("Choice", "Do you want to have a list of townships with client's penetration above or under the mean?", c(" ", "Above","Under"))),
# Main panel for displaying outputs ----
mainPanel(
conditionalPanel(
'input.Choice === "Above"',
DT::dataTableOutput("more_table")
),
conditionalPanel(
'input.Choice === "Under"',
DT::dataTableOutput("less_table")
)
)
)
# Define server logic to plot various variables against mpg ----
server <- function(input, output) {
output$more_table = DT::renderDataTable({
more_than_mean
})
output$less_table = DT::renderDataTable({
less_than_mean
})
}
shinyApp(ui, server)
I have a selectInpunt updated, and with this last value, I want to get information from a dataframe, but I can't get the value of the last selectInput, I have just the result "character(0)". The dataframe open but I can't get the value corresponding to input...The values of the first selectInput are the names of different data.frames. I can get the data.frame, but I can not extract the information corresponding to the input of the second selectInput.
library(shiny)
liste<-c("BCE","FED")
ui<-tagList(
navbarPage(
"Evolutions Economiques",
tabPanel("Observation",
# Application title
titlePanel("Evolutions Economiques"),
# Sidebar with a slider input for number of bins
#sidebarLayout(
sidebarPanel(
h1("Selection des donnees"),
selectInput("Source","Source :",
choices =liste),
selectInput("indic","Indicateur :",
choices = NULL)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(type="tabs",
tabPanel("Description",verbatimTextOutput("summary"),
)
))
)
))
library(shiny)
library(dplyr)
BCE<-data.frame(Indice=c("A","B"),Description=c("Alors","Pouipoui"))
FED<-data.frame(Indice=c("C","D"),Description=c("So","Birdyy"))
# Define server logic required to draw a histogram
shinyServer(function(input, output,session) {
observeEvent(input$Source,{
data<-get(input$Source)
updateSelectInput(session,"indic",
choices = data$Indice)
})
output$summary<-renderPrint({
data<-get(input$Source)
data<-filter(data,Indice==input$indic)
data<-data$Description
data
})
})
If I have the following data frame:
Hours<-c(2,3,4,2,1,1,3)
Project<-c("a","b","b","a","a","b","a")
cd=data.frame(Project,Hours)
What is wrong with the following Shiny code:
Why do I get the error:Error in tag("form", list(...)) : argument is missing, with no default
Server.R
##server.R
library(shiny)
library(ggplot2)
library(lattice)
# Define shiny server
shinyServer(function(input, output) {
#Simple test plot
output$testPlot = renderPlot( {
pdata=subset(cd, Project==input$proj)
densityplot(pdata$Hours,lwd=3)
})
})
ui.R
library(shiny)
ulist=levels(cd$Project)
names(ulist) = ulist
# Sidebar with a slider input for the number of bins
shinyUI(pageWithSidebar(
# Application title
headerPanel("Project Data"),
sidebarPanel(
#Which table do you want to view, based on the list of institution names?
selectInput("proj", "Project:",ulist),
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("testPlot"),
)
)
)
The error again is: Error in tag("form", list(...)) : argument is missing, with no default
This seems so simple but I don't know what's wrong. Any advice would be most appreciated.
You have superfluous comma's in your ui.R:
shinyUI(pageWithSidebar(
headerPanel("Project Data"),
sidebarPanel(
selectInput("proj", "Project:",ulist) # remove comma here
),
mainPanel(
plotOutput("testPlot") # remove comma here
)
) )
I think you have a couple of extra commas in the ui.R, namely after the selectInput and plotOutput commands
Im trying to create a web application with the new RStudio feature Shiny, which plots different stocks. I created the following example.
I want to select the dataset StockMarket then select eg the DAX and then finally the plot should appear.
Right now if you run this code the plot appears immediately
Could you help me please?
ui.R:
library(shiny)
library(ggplot2)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Plot1"),
sidebarPanel(
selectInput("dataset", "Dataset", list("mtcars"="cars", "StockMarket"="stocks")),
conditionalPanel(
condition = "input.dataset=='stocks'",
uiOutput("data")
)),
mainPanel(
plotOutput('plotstock')) ))
server.R:
library(shiny)
require(ggplot2)
library(datasets)
shinyServer(function(input, output) {
output$data<- reactiveUI(function() {
selectInput("data", "Choose Dataset", colnames(EuStockMarkets))
})
output$plotstock <- reactivePlot(function() {
data<-data.frame(EuStockMarkets)
p<- ggplot(data,aes(x=seq(1,length(data[,1])),y=DAX))+geom_line(size=1)+ylab("")+opts(title="Time Series")
print(p)
})})
In the reactivePlot function you can do something like
if (is.null(input$data))
return(NULL)
I would also add a blank entry to the dataset choices ("(Choose one)"="") and also have
if (!nzchar(input$dataset))
return(NULL)
in reactivePlot.
Also make sure that you check for empty strings
if (!nzchar(input$dataset) || input$dataset=='')
return(NULL)