subset data and plot this subsetted data with Shiny - r

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

Related

No output in shiny

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()
}
}
)
}
)

Subseting object with reactive values will fail

List item
I have a problem with reactive values not working as i think it should work.
The small code below describes the problem. The function firstsub2 will basically subset an object into a smaller one by removing samples we donĀ“t want to keep for some reason (this is using the subset_samples function from the phyloseq biocondcutor package).
UI.R
myui <-
fluidPage(
navbarPage("Project",
## foldchanges
tabPanel("Foldchanges",
titlePanel("Permanova: Analysis of variance using distance matrices"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
actionButton("dofoldchanges", "Generate foldchanges")
),
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(id="foldchanges",type = "tabs",
tabPanel(title="Summary", value=1, verbatimTextOutput("summary_foldchanges"))
#tabPanel("Table pairwise",value=4, dataTableOutput("tablepermanovapw"))
)
)
)
)
)
)
SERVER.R
#To install phyloseq
#source("https://bioconductor.org/biocLite.R")
#biocLite("phyloseq")
library(shiny)
library(phyloseq)
myserver <- function(input, output, session) {
source("foldchanges.R", local = TRUE)
}
foldchanges.R
# Filter object
firstsub2 <- reactive({
values$rn <- as.character(sample_data(values$physeq)[,"Description"]$Description)))
cat(values$rn)
#The subset_samples function below will not work
filteredtaxo <- subset_samples(values$physeq, Description %in% values$rn)
return(filteredtaxo)
})
values <- reactiveValues()
observeEvent(input$dofoldchanges, {
rich_sparse_biom = system.file("extdata", "rich_sparse_otu_table.biom", package = "phyloseq")
physeq = import_biom(rich_sparse_biom, parseFunction = parse_taxonomy_greengenes)
print(physeq)
values$physeq <- physeq
values$filtered <- firstsub2()
})
This example will basically return the same object that was imported
Your code still has some errors, especially the server file (Some unnecessary commas).
I think your app works as it should, but it will always be the same file as the values$rn will always be the same. I included another line (uncommented right now) below your subset_sample, to test if the subsetting is working. If you change values$rn to "human skin" for example, you will see different results.
But I didn't figure out how to change it. When taking only the first element of values$rn for example, I'm getting an object not found error. But when I include "human skin" like that, it works.
But maybe that helps you already.
library(shiny)
# source("https://bioconductor.org/biocLite.R")
# biocLite("phyloseq")
library(phyloseq)
myui <- {fluidPage(
navbarPage("Project",
## foldchanges
tabPanel("Foldchanges",
titlePanel("Permanova: Analysis of variance using distance matrices"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
actionButton("dofoldchanges", "Generate foldchanges")
),
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(id="foldchanges",type = "tabs",
tabPanel(title="Summary", value=1,
verbatimTextOutput("summary_foldchanges"),
verbatimTextOutput("summary_physeq"))
)))))
)}
myserver <- function(input, output, session) {
values <- reactiveValues()
firstsub2 <- reactive({
req(values$physeq)
input$dofoldchanges
values$rn <- as.character(sample_data(values$physeq)[,"Description"]$Description)
cat(values$rn)
#The subset_samples function below will not work
filteredtaxo <- subset_samples(values$physeq, "Description" %in% values$rn)
## Change it to one of the next lines, to see that subsetting works.
# filteredtaxo <- subset_samples(values$physeq, Description %in% "human skin")
# filteredtaxo <- subset_samples(values$physeq, Description %in% "human gut")
return(filteredtaxo)
})
observeEvent(input$dofoldchanges, {
rich_sparse_biom = system.file("extdata", "rich_sparse_otu_table.biom", package = "phyloseq")
physeq = import_biom(rich_sparse_biom, parseFunction = parse_taxonomy_greengenes)
print(physeq)
values$physeq <- physeq
values$filtered <- firstsub2()
})
output$summary_physeq <- renderPrint({
req(values$physeq)
values$physeq
})
output$summary_foldchanges <- renderPrint({
req(values$filtered)
values$filtered
})
}
shinyApp(myui, myserver)

Error : argument "mainPanel" is missing, with no default

I am trying to run the below Shiny App but I am getting a:
"ERROR: argument "mainPanel" is missing, with no default"
message. Even though I take care of the mainPanel inside the sidebarLayout and outside the sidebarPanel and I tried another relative solutions available on the web, but the error remains the same.
Please help me in finding the error and rectifying it.
library(shiny)
shinyUI(pageWithSidebar(
titlePanel("Road Accident"),
sidebarLayout(
sidebarPanel(selectInput('geo', 'Country', choices = list("Unknown" ="AT","BE","CZ","DE","DK","EL","ES","FI","FR",
"IE","IT","LI","LU","NL","PT","RO","SE","UK","PL","SI","BG","CH","CY",
"EE","HU","IS","LV","MT","NO","SK","HR","LT")
)
),
mainPanel(
plotOutput("newHist"))
)
))
library(shiny)
library(ggplot2)
library(dplyr)
library(eurostat)
p1<-get_eurostat("tsdtr420",time_format="num")
p2<-p1[!rowSums(is.na(p1)),]
shinyServer(
function(input, output) {
output$newHist<- renderPlot({
p3 <- filter(p2, grepl(input$geo,geo))
p<-ggplot(data=p3,aes(time,values))+geom_bar(stat="identity")+theme_bw()+theme(legend.position="none")+
labs(title="Road Accidents",x="",y="Victims")
print(p)
output$geo
})
})
If you do it without sidebarLayout, it works fine. You just need to check the required arguments of your elements with ?pageWithSidebar, ?sidebarLayout and so on... pageWithSidebar requires three arguments, you were only giving two.
shinyUI(pageWithSidebar(
titlePanel("Road Accident"),
sidebarPanel(selectInput('geo', 'Country', choices = list("Unknown" ="AT","BE","CZ","DE","DK","EL","ES","FI","FR",
"IE","IT","LI","LU","NL","PT","RO","SE","UK","PL","SI","BG","CH","CY",
"EE","HU","IS","LV","MT","NO","SK","HR","LT"))),
mainPanel(plotOutput("newHist")))
)

Reactive column names in reactive data frame shiny

I want to create a reactive data frame with a reactive column name in shiny. However this is throwing error. I have provided the code below.. The error is being caused by an () followed by =, but I cant find a way around. Any help will be appreciated
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Tool"),
sidebarLayout(
sidebarPanel(
textInput("Item","Enter Item Name"),
div(class='row-fluid',
div(class='span6', numericInput("sales1","Enter Sales",value=0),numericInput("sales2","Enter Sales",value=0)),
div(class='span6', numericInput("prices1","Enter price",value=0),numericInput("prices2","Enter price",value=0))
)),
mainPanel(
dataTableOutput("table")
)
)
))
server.R
library(shiny)
shinyServer(function(input, output) {
prices<-reactive({
c(input$prices1,input$prices2)
})
sales<-reactive({
c(input$sales1,input$sales2)
})
combined<-reactive({
data.frame(prices(),sales())
})
combined_final<-reactive({
mutate(combined(),Rev=prices()*sales())
})
namerev<-reactive({
as.character(paste("Rev",input$Item,sep="_"))
})
combined_final_rename<-reactive({
rename_(combined_final(),namerev() ="Rev")
})
output$table<-renderDataTable({
combined_final_rename()
})
})
If I understood the question correctly, you might need something like that:
combined_final_rename<-reactive({
d <- combined_final()
colnames(d)[colnames(d)=='Rev'] <- namerev()
d
})

RStudio Shiny Conditional Plot

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)

Resources