Here's the minimal reproducible example:
# This is a Shiny web application.
library(shiny)
# UI for application
ui <- fluidPage(
# Application title
titlePanel("A Problematic App - Part 2"),
# Sidebar with two slider inputs
sidebarLayout(
sidebarPanel(
sliderInput(
"NoOfSamples",
label = "Sample Size",
value = 100,
min = 10,
max = 150,
step = 10,
width = "40%"
),
sliderInput(
"KeepSamples",
label = "Samples to Keep",
value = 50,
min = 10,
max = 150,
step = 10,
width = "40%"
)
),
# Shows the resulting table
mainPanel(
tableOutput("table1"),
tableOutput("table2")
)
)
)
# Server logic
server <- function(input, output) {
# Using the iris dataset
datExpr <- as.data.frame(iris)
n = reactive({
input$NoOfSamples
})
datExpr0 <- reactive({
datExpr[1:n(), ]
})
output$table1 <- renderTable({
datExpr0()
})
# Displays the first table correctly if the rest is commented out
keepSamples = reactive({
input$KeepSamples
})
datExpr <- reactive({
datExpr0()[keepSamples(),]
})
output$table2 <- renderTable({
datExpr()
})
}
# Run the application
shinyApp(ui = ui, server = server)
I have created live examples for demonstration.
With the second part of the program commented out.
The complete program. [Shinyapps.io] is supressing the error details, so attached is a screenshot of a local run.
The error is object of type 'closure' is not subsettable. While many questions (and answers) regarding this error exist, I am yet to find any explaining the behaviour demonstrated above.
Why does this happen?
The normal (script-equivalent) works as expected.
datExpr <- as.data.frame(iris)
n = 50
datExpr0 <- datExpr[1:n, ]
datExpr0
keepSamples = 10
datExpr <- datExpr0[keepSamples,]
datExpr
Is there a way to achieve what the normal script does in the shiny app?
The issue is that you have both a dataframe and a reactive in your app called datExpr. Simply rename one of both (I decided for the reactive).
EDIT There is of course nothing special about that in shiny.
A simple example to illustrate the issue:
datExpr <- iris
datExpr <- function() {}
datExpr[1:2]
#> Error in datExpr[1:2]: object of type 'closure' is not subsettable
And you see that we get the famous object of type 'closure' is not subsettable error too. The general issue or lesson is that in R you can't have two different objects with the same name at the same time.
# This is a Shiny web application.
library(shiny)
# UI for application
ui <- fluidPage(
# Application title
titlePanel("A Problematic App - Part 2"),
# Sidebar with two slider inputs
sidebarLayout(
sidebarPanel(
sliderInput(
"NoOfSamples",
label = "Sample Size",
value = 100,
min = 10,
max = 150,
step = 10,
width = "40%"
),
sliderInput(
"KeepSamples",
label = "Samples to Keep",
value = 50,
min = 10,
max = 150,
step = 10,
width = "40%"
)
),
# Shows the resulting table
mainPanel(
tableOutput("table1"),
tableOutput("table2")
)
)
)
# Server logic
server <- function(input, output) {
# Using the iris dataset
datExpr <- as.data.frame(iris)
n = reactive({
input$NoOfSamples
})
datExpr0 <- reactive({
datExpr[1:n(), ]
})
output$table1 <- renderTable({
datExpr0()
})
# Displays the first table correctly if the rest is commented out
keepSamples = reactive({
input$KeepSamples
})
datExpr1 <- reactive({
datExpr0()[keepSamples(),]
})
output$table2 <- renderTable({
datExpr1()
})
}
# Run the application
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:3648
I'm trying to build my first Shiny app at the moment and and having some issues. Is it possible to get access to a variable from a different output object? I'm trying to print the table in the first tab and show the individual plots on separate tabs, even better if I can show all 3 on 1 tab.
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Table", tableOutput("dataTable")),
tabPanel("xy Chart", plotOutput("xyChart")),
tabPanel("yz Chart", plotOutput("yzChart"))
)
)
)
)
)
server <- function(input, output) {
output$dataTable <- renderTable({
x <- rnorm(100, mean = 1)
y <- rnorm(100, mean = 0)
z <- rnorm(100, mean = 0.5)
dataTable <- cbind(x,y,z)
})
output$xyChart <- renderPlot({
plot(x,y)
})
If you haven't already, would take a look at the shiny tutorials available.
Instead of including your data in a single output inside of server, you could declare these variables elsewhere. Since you are creating a shiny app, you might be interested in changing these variables, and having the other outputs automatically update.
If that is true, you might want to use reactiveValues or create a reactive function.
Here's an example below. By using reactiveValues, when you read a value from it (like x, y, or z) the calling expression takes a reactive dependency on that value (and will update with changes made to it). Whenever you modify those values, it will notify all reactive functions that depend on that value.
library(shiny)
ui <- fluidPage(
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)
)
)
server <- function(input, output) {
my_data <- reactiveValues(
x = rnorm(100, mean = 1),
y = rnorm(100, mean = 0),
z = rnorm(100, mean = 0.5)
)
output$table <- renderTable({
data.frame(my_data$x, my_data$y, my_data$z)
})
output$plot <- renderPlot({
plot(my_data$x, my_data$y)
})
output$summary <- renderText({
"Summary Goes Here"
})
}
shinyApp(ui = ui, server = server)
And if you want all 3 on one panel (as described in comments), use this for your ui:
ui <- fluidPage(
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("All 3",
plotOutput("plot"),
verbatimTextOutput("summary"),
tableOutput("table")
)
)
)
)
If you want to include your input$nRV (as mentioned in comments), use a reactive expression, and call it as my_data():
server <- function(input, output) {
my_data <- reactive({
a = rnorm(input$nRV, mean = 2)
b = rnorm(input$nRV, mean = 5)
x = rnorm(input$nRV, mean = 3)
y = rnorm(input$nRV, mean = 0)
z = rnorm(input$nRV, mean = 0.5)
data.frame(a, b, x, y, z)
})
output$table <- renderTable({ data.frame(my_data()$x, my_data()$y, my_data()$z)
})
output$plot <- renderPlot({ plot(my_data()$x, my_data()$y) })
}
I want to create a small shiny app to explore a scoring function that I am writing for a set of data observations. This is my first shiny app so bear with me.
What I want to show is the data table where one column is computed by a function (let's say f(x) = x^2 + y) where x is another (numeric) column in the table and y should be adjustable with a slider in the sidebar.
I want to make the table reactive, so that as soon as the slider is adjusted, the content that is displayed will be updated. Does anyone have a link to a tutorial (I could not find a similar problem) or a suggestion how to handle this. If so, please let me know!
This is the code I have so far:
library(shiny)
#### INIT ####
x <- 1
y <- 0.5
z <- 2
df <- data.frame(
a=1:10,
b=10:1
)
df['score'] <- df[,x]^y + z
#### UI ####
ui <- fluidPage(
title = "Examples of DataTables",
sidebarLayout(
sidebarPanel(
sliderInput("x", "x:",
min = 0, max = ncol(df),
value = 1),
sliderInput("y", "y:",
min = 1, max = 10,
value = 1),
sliderInput("z", "z:",
min = 1, max = 100,
value = 20)
),
mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("df", dataTableOutput("df"))
)
)
)
)
#### SERVER ####
server <- function(input, output) {
sliderValues <- reactive({
df['score'] <- df[,input$x]^input$y + input$z
})
sliderValues()
output$df<- renderDataTable(df)
}
#### RUN ####
shinyApp(ui = ui, server = server)
Just make the data.frame you actually plot reactive. For example
server <- function(input, output) {
calcualtedValues <- reactive({
df['score'] <- df[,input$x]^input$y + input$z
df
})
output$df<- renderDataTable(calcualtedValues())
}
Here the calcualtedValues reactive element returns a new data.frame when the input is updated, and then you actually render that updated data.frame rather than the original data.frame each time.
I am trying to make an app in Shiny which is dynamically subsetting a data-set 3 times by users input.
Let's assume that the dataset is that
Number<- c(10, 20, 30 , 40, 50 ,60, 70, 80, 90,100,110,120,130,140)
Att1 <- c('a','a','a','a','a','a','a','b','b','b','b','b','b','b')
Att2 <- c('c','c','c','d','d','d','d','e','e','e','g','g','g','g')
Index<-c('I1','I2','I3','I4', 'I5','I6','I7','I8','I9','I10', 'I11','I12','I13','I14')
df <- data.frame(Number, Att1 , Att2,Index)
What i want to do is to create a dropdown menu that gives you the choices a or b from att1 then the choice reacts with the second drop down where the choices of the att2 are displayed but subsetted for choice att1. Depending on the choice the user then the last drop down will give him the choices for which index to choose. Now after the choice of the index a dataframe have to return with only the numbers indicated by the index and this number will be used in next steps.
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(data.table)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("App"),
sidebarLayout(
sidebarPanel(
selectInput("Att1", "Choose Att1",choices= c(as.character(unique(df$Att1)) )),
uiOutput("c")),
# Show a plot of the generated distribution
mainPanel( DT::dataTableOutput("table")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
Number<- c(10, 20, 30 , 40, 50 ,60, 70, 80, 90,100,110,120,130,140)
Att1 <- c('a','a','a','a','a','a','a','b','b','b','b','b','b','b')
Att2 <- c('c','c','c','d','d','d','d','e','e','e','g','g','g','g')
Index<-c('I1','I2','I3','I4', 'I5','I6','I7','I8','I9','I10', 'I11','I12','I13','I14')
df <- data.frame(Number, Att1 , Att2,Index)
selectedData <- reactive({
Ddata<-subset(df,Att1==input$Att1)
})
output$c<-renderUI({selectInput("Att2", "Choose Att2",choices= c(as.character(unique(selectedData()$Att2)) ))})
selectedData2 <- reactive({
Vdata<-subset(selectedData(),Att2==input$c)
Vdata<-as.data.frame(Vdata)
Vdata
})
output$table <- DT::renderDataTable({
head(selectedData2(), n = 10)
})
}
# Run the application
shinyApp(ui = ui, server = server)
This is where I got as far but the problem is how can I use a reactive dataset second time in a reactive expression and also the output for the first 2 attributes is null. I am trying to solve this for days, any thoughts?
There is a specific shiny function to update the content of a SelectInput: updateSelectInput().
If used inside an observe it can be used exactly for what you are trying to do:
server <- function(input, output, session) {
observe({
input$Att1
x <- df[df$Att1 == input$Att1, 'Att2']
xs <- as.character(unique(x))
updateSelectInput(session, 'Att2', choices = xs)
})
selectedData <- reactive({
df[df$Att2 == input$Att2, ]
})
output$table <- DT::renderDataTable({
head(selectedData(), n = 10)
})
}
Here is the ui for completeness
ui <- fluidPage(
# Application title
titlePanel("App"),
sidebarLayout(
sidebarPanel(
selectInput("Att1", "Choose Att1",choices = as.character(unique(df$Att1)) ),
selectInput("Att2", "Choose Att2",choices = NULL, selected = 1)
),
# Show a plot of the generated distribution
mainPanel( DT::dataTableOutput("table")
)
)
)
Just continuing with what you have... I added "NULL" as a choice to the drop-downs, and if "NULL" is selected then the full data set is retained.
Number <- c(10, 20, 30 , 40, 50 ,60, 70, 80, 90,100,110,120,130,140)
Att1 <- c('a','a','a','a','a','a','a','b','b','b','b','b','b','b')
Att2 <- c('c','c','c','d','d','d','d','e','e','e','g','g','g','g')
Index <- c('I1','I2','I3','I4', 'I5','I6','I7','I8','I9','I10', 'I11','I12','I13','I14')
df <- data.frame(Number, Att1, Att2, Index)
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(data.table)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("App"),
sidebarLayout(
sidebarPanel(
selectInput("Att1", "Choose Att1", choices = c("NULL", as.character(unique(df$Att1))), selected = "NULL"),
uiOutput("c"),
uiOutput("d")),
# Show a plot of the generated distribution
mainPanel( DT::dataTableOutput("table")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
selectedData <- reactive({
if(input$Att1 == "NULL") Ddata <- df #Keep full data set if NULL
else Ddata <- subset(df, Att1 == input$Att1)
Ddata
})
######################
output$c <- renderUI({selectInput("Att2", "Choose Att2", choices = c("NULL", as.character(unique(selectedData()$Att2))), selected = "NULL")})
selectedData2 <- reactive({
if(input$Att2 == "NULL") Vdata <- selectedData()
else Vdata <- subset(selectedData(), Att2 == input$Att2)
Vdata
})
######################
#=====================
output$d <- renderUI({selectInput("Index", "Choose Index", choices = c("NULL", as.character(unique(selectedData2()$Index))), selected = "NULL")})
selectedData3 <- reactive({
if(input$Index == "NULL") Fdata <- selectedData2()
else Fdata <- subset(selectedData2(), Index == input$Index)
Fdata
})
#=====================
output$table <- DT::renderDataTable({
head(selectedData3(), n = 10)
})
}
# Run the application
runApp(shinyApp(ui = ui,
server = server), launch.browser=TRUE
)
My Shiny App has multiple inputs that depend on the number of variables used. A simplified version, though not working, is below. I was able to get the UI to update based upon the numericInput using a function called Make.UI which I used to make uiOutput, but getting the inputs back into the server is beyond my Shiny skill set! Any suggestions would be greatly appreciated.
gwynn
library(shiny)
D = matrix(runif(400), nrow = 20)
colnames(D) = labs = sapply(1:20, function(i) {paste0("col",i)})
# Define UI for application that summarises data
ui <- fluidPage(
# Application title
titlePanel("Summaries"),
# Select columns to get fed into summary
tabsetPanel(
tabPanel("Matching Variables Info",
sidebarPanel(
numericInput("NoVars","No. of variables to summarize",
value = 3, min = 2, max = dim(D)[2]),
uiOutput("VarsInput")
),
# Show summaries of columns choosen above
mainPanel(
verbatimTextOutput("dataInfo")
)
)
)
)
# Define the server code
server <- function(input, output){
Make.UI <- function(NoV){
C = sapply(1:NoV, function(i){paste0("cols",i)})
L = sapply(1:NoV, function(i){paste0("label",i)})
output = tagList()
for(i in seq_along(1:NoV)){
output[[i]] = tagList()
output[[i]][[1]] = selectInput(C[i], "Variable to summarize:", labs)
output[[i]][[2]] = textInput(L[i], label = "Label for variable:",
value = "Label for variable Here")
} ## for loop
output
} # closes Make.UI function
K <- reactive({
input$NoVars
})
output$VarsInput <- renderUI({
Make.UI(K())
})
output$dataInfo <- renderPrint({
C <- sapply(1:K(), function(i) {input[[paste0("cols",i)]]})
## the code in the line above doesn't work
summary(D[, C()])
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)
Like I wrote in the first comment, I am unsure about the Make.UI()function. If you really want to keep it as a seperate function you should make it reactive. Or just use it as I did in the code below.
Moreover, in output$dataInfo <- renderPrint({ C is not a reactive() function so you would need to remove brackets there.
library(shiny)
D = matrix(runif(400), nrow = 20)
colnames(D) = labs = sapply(1:20, function(i) {paste0("col",i)})
# Define UI for application that summarises data
ui <- fluidPage(
# Application title
titlePanel("Summaries"),
# Select columns to get fed into summary
tabsetPanel(
tabPanel("Matching Variables Info",
sidebarPanel(
numericInput("NoVars","No. of variables to summarize",
value = 3, min = 2, max = dim(D)[2]),
uiOutput("VarsInput")
),
# Show summaries of columns choosen above
mainPanel(
verbatimTextOutput("dataInfo")
)
)
)
)
# Define the server code
server <- function(input, output){
K <- reactive({
input$NoVars
})
output$VarsInput <- renderUI({
NoV = K()
C = sapply(1:NoV, function(i){paste0("cols",i)})
L = sapply(1:NoV, function(i){paste0("label",i)})
output = tagList()
for(i in seq_along(1:NoV)){
output[[i]] = tagList()
output[[i]][[1]] = selectInput(C[i], "Variable to summarize:", labs)
output[[i]][[2]] = textInput(L[i], label = "Label for variable:",
value = "Label for variable Here")
}
output
})
output$dataInfo <- renderPrint({
C <- sapply(1:K(), function(i) {input[[paste0("cols",i)]]})
## the code in the line above doesn't work
summary(D[, C])
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server)