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")))
)
Related
I have created the below Shiny web app in order to download currency data from Quandl and then plot the time series.
However, when I run the code, I get the error message: 'arg' must be NULL or a character vector.
I have looked at previous answers, and often the issue seemed to be that the reactive expression was not defined properly. However, to my knowledge, all the relevant columns are nested within the fluidRow so I cannot tell what is causing this error.
Any advice appreciated.
ui.R
library(shiny)
ui <- fluidPage(
titlePanel("Currency"),
fluidRow(
column(3,
selectInput("currencypairs",
h3("Currency Pairs"),
choices = c("EUR/USD" = "FRED/DEXUSEU",
"USD/GBP" = "FRED/DEXUSUK",
"USD/AUD" = "FRED/DEXUSAL"),
selected = "EUR/USD")),
column(3,
dateInput("start_date",
h3("Start Date"),
value = "2014-01-01")),
column(3,
dateInput("end_date",
h3("End Date"),
value = "2017-01-01"))
),
# Show a plot of the generated output
mainPanel(
plotOutput("CurrencyOutput")
)
)
server.R
library(shiny)
library(ggplot2)
library(scales)
require(Quandl)
# Shiny Application
shinyServer(function(input, output) {
output$CurrencyOutput <- renderPlot({
currency = Quandl(input$currencypairs,
input$start_date,input$end_date,type="xts")
currencydf<-data.frame(currency)
plot(currencydf$currency,type='l',col="blue")
})
})
I used your code and found a solution. You need to put start_date and end_date instead your input directly, so the code looks like:
currency = Quandl(input$currencypairs, start_date=input$start_date, end_date=input$end_date, type="xts")
I hope it helps
I apologise in advance for not having a minimal reproducible example. I tried to make one but didn't manage to reproduce the error.
So I have 2 scripts (ui.R and server.R) in the same folder. At the very start of the server script I want to make a vector a which will fill itself with inputs from 2 selectInputs in the ui-script.
#ui
shinyUI(fluidPage(
fluidRow(
column(selectInput(inputId = "input1",
label = "This is input 1",
choices = c("bad","neutral","good"))
column(selectInput(inputId = "input2",
label = "This is input 2",
choices = c("bad","neutral","good"))
#server
shinyServer(function(input, output) {
a <- c(input$input1, input$input2)
})
b <- b[a]
The user interface part is not the problem, the server part is. When I run the app I get this error:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
So I realize I have to make this vector a somehow reactive but I fail to see how. I tried using the function reactive:
#server
shinyServer(function(input, output) {
a <- reactive({
c(input$input1, input$input2)
})
})
But off course then I get this error because a is no longer a vector but a function.
invalid subscript type 'closure'
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
I'm trying to figure out how to get R to interact via shiny with other javascript elements, which I'm guessing means by having server.R serve a customised shiny object (ideally json formatted?) to ui.R, which is then converted to a javascript array. My work-in-progress code is:
server.R
library(shiny)
shinyServer(
function(input, output) {
output$species_table <- renderTable({ iris[iris$Species == input$species,] })
output$json <- RJSONIO::toJSON(iris[iris$Species == input$species,], byrow=T, colNames=T) # error line
}
)
ui.R
require(shiny)
specs = as.character(unique(iris$Species))
names(specs) = specs
pageWithSidebar(
headerPanel("minimal json handling example"),
sidebarPanel(selectInput("species", "Species", specs)),
mainPanel(
tableOutput("species_table")
)
)
Which returns the server error:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside
a reactive expression or observer.)
.. because it's obviously the wrong approach. Without server.R's line output$json <- ... the outcome works and looks like this, so the rest of the code is ok. But I also want to get the json (or any alternative format) across somehow and trigger a subsequent javascript action to read it in as an array object. Grateful for any pointers, and apologies in advance if my description is unclear.
For benefit of others this is the working formula. If anyone can suggest a more elegant solution I'd be grateful. Open the browser's javascript console log to see object 'data' being updated..
server.R
library(shiny)
iris <- datasets::iris
names(iris) <- gsub('[/.]','_',tolower(names(iris)))
shinyServer(
function(input, output) {
output$json <- reactive({
paste('<script>data=',
RJSONIO::toJSON(iris[iris$species == input$species,], byrow=T, colNames=T),
';console.log(data[0]);', # print 1 data line to console
'</script>')
})
}
)
ui.R
require(shiny)
iris <- datasets::iris
names(iris) <- gsub('[/.]','_',tolower(names(iris)))
specs <- as.character(unique(iris$species))
names(specs) <- specs
pageWithSidebar(
headerPanel("minimal json handling example"),
sidebarPanel(selectInput("species", "Species", specs)),
mainPanel(
htmlOutput("json")
)
)
So, that error generally means that you need to wrap reactive({}) around something, in this case your toJSON command. This works, and displays the JSON data.
ui.r
require(shiny)
specs = as.character(unique(iris$Species))
names(specs) = specs
pageWithSidebar(
headerPanel("minimal json handling example"),
sidebarPanel(selectInput("species", "Species", specs)),
mainPanel(
#tableOutput("species_table")
textOutput("json")
)
)
server.r
library(shiny)
shinyServer(
function(input, output) {
output$species_table <- renderTable({ iris[iris$Species == input$species,] })
output$json <-reactive({ RJSONIO::toJSON(iris[iris$Species == input$species,],
byrow=T, colNames=T) })# error line
}
)
Greetings and thanks for your help,
I can't use a data.frame (created on server.R) in ui.R.
The reason is obvious: a tableOutput() is not a data.frame nor a vector.
I need to do this because the selectInput("ops",...) receives that table from server.R, as it is the result of a function applied to an input there.
The function is long, so i just made this data.frame(c(1,2),c(3,4),c(5,6)). Anyhow, the point is the same.
Question:
Even though i know why is not working, I am not able to make it work.
What I am looking for is a way to get the information from the tableOutput() in a table-like way, to use it in the selectInput(choices = ...) dropdown list.
If you run this code you'll notice the selectInput() is just showing options: "name", "id", "class".
I want it to show: 1, 2. or 3, 4. or 5, 6...
Thank you.
library(shiny)
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel({p("")}),
mainPanel({
tabPanel("Asignar tipo",
selectInput(
"ops",
"Operations",
tableOutput("impsTabla"),
multiple=TRUE,
selectize=TRUE
),
br(),
tableOutput("impsTabla")
)
})
)
),
server = function(input, output){
output$impsTabla<- renderTable(
data.frame(c(1,2),c(3,4),c(5,6))
)
}
)