I am trying to subset data frame of three column (StockCode,Price,label)
but I had to used reactive and my ask is how to render label
I need somethink like renderText(dataset()$label)
ui.R
library(shiny)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
uiOutput("codePanel")
),
# Main panel for displaying outputs ----
mainPanel(
textOutput("text")
)
)
)
server.R
server <- function(input, output) {
output$codePanel<-renderUI({
selectInput("codeInput",label ="choose code",choices =data$StockCode)
})
dataset<-reactive({
subset(data,data$StockCode==input$codeInput)
})
output$text<-renderText(dataset())
}
If we are looking to show the data.frame output use the renderDataTable from DT. For reproducibility, used the inbuilt dataset iris
library(shiny)
library(DT)
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
uiOutput("codePanel")
),
mainPanel(
DT::dataTableOutput("text")
)
)
)
server <- function(input, output) {
filt <- selectInput("codeInput",label ="choose code",
choices = as.list(unique(iris$Species)))
output$codePanel <- renderUI({ filt
})
dataset<-reactive({
subset(iris, Species == input$codeInput)
})
output$text<-renderDataTable(dataset())
}
shinyApp(ui = ui, server = server)
-output
The dataset rows can be pasted together to a string to be used in the renderText
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
uiOutput("codePanel")
),
mainPanel(
verbatimTextOutput("text")
)
)
)
server <- function(input, output) {
filt <- selectInput("codeInput",label ="choose code",
choices = as.list(unique(iris$Species)))
output$codePanel <- renderUI({ filt
})
iris$Species <- as.character(iris$Species)
dataset<-reactive({
do.call(paste, c(collapse = "\n", rbind(colnames(iris), subset(iris, Species == input$codeInput))))
})
output$text<-renderText(dataset())
}
shinyApp(ui = ui, server = server)
-output
Or use htmlOutput with renderUI
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
uiOutput("codePanel")
),
mainPanel(
htmlOutput("text")
)
)
)
server <- function(input, output) {
filt <- selectInput("codeInput",label ="choose code",
choices = as.list(unique(iris$Species)))
output$codePanel <- renderUI({ filt
})
dataset<-reactive({
do.call(paste, c(collapse = "<br/>", rbind(colnames(iris), subset(iris, Species == input$codeInput))))
})
output$text<-renderUI(HTML(dataset()))
}
shinyApp(ui = ui, server = server)
I could not get renderDataTable from DT to work. A message said some functions were masked. So, I removed DT and added in some code from the R Studio tutorial on tables and data.frames. This is what I came up with:
library(shiny)
ui <- fluidPage(
# App title ----
titlePanel("Subsetting Iris Dataset"),
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
uiOutput("codePanel"),
# Input: Numeric entry for number of obs to view
numericInput(inputId = "obs",
label = "Number of observations to view:",
value = 10)
),
mainPanel(
tableOutput("view")
)
)
)
server <- function(input, output) {
filt <- selectInput("codeInput", label = "choose code",
choices = unique(iris$Species))
output$codePanel <- renderUI({ filt
})
dataset <- reactive({
subset(iris, Species == input$codeInput)
})
output$view <- renderTable(head(dataset(), n = input$obs))
}
shinyApp(ui = ui, server = server)
Related
in Shiny I simply want to select which variable of a dataframe shall be plotted and I do not want to have to type all the variable names in the server switch part. Here is what I do:
ui <- fluidPage(
titlePanel("Hello World!"),
sidebarLayout(
sidebarPanel(
selectInput("variable", "Choose a variable:",
# choices = c("cyl", "mpg")),
choices = names(mtcars)),
),
mainPanel(
plotOutput(outputId = "BarPlot"),
)
)
)
server <- function(input, output) {
datasetInput <- reactive({
switch(input$variable,
"cyl" = mtcars[,"cyl"],
"mpg" = mtcars[,"mpg"])
})
output$BarPlot <- renderPlot({
x <- datasetInput()
barplot(table(x))
})
}
Instead of
switch(input$variable,
"cyl" = mtcars[,"cyl"],
"mpg" = mtcars[,"mpg"])
can I do something like
choices = mtcars[,get(choices)]
to cover all choices without having to type them one by one?
One approach is to use varSelectInput and pass the data frame as data (it will include all column names as the choices). Then you can extract the selected column from mtcars through mtcars[[input$variable]] in your example:
library(shiny)
ui <- fluidPage(
titlePanel("Hello World!"),
sidebarLayout(
sidebarPanel(
varSelectInput("variable",
"Choose a variable:",
data = mtcars),
),
mainPanel(
plotOutput(outputId = "BarPlot"),
)
)
)
server <- function(input, output) {
datasetInput <- reactive({
mtcars[[input$variable]]
})
output$BarPlot <- renderPlot({
x <- datasetInput()
barplot(table(x))
})
}
shinyApp(ui, server)
User new to Shiny here. It is a very easy mode. But it's not work and it doesn't show any errors.
The same result when I use subset function.
Here's my code:
library(shiny)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("variable", "Variable:",colnames(iris))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
iris %>% dplyr::filter(input$variable>=5)
})
}
# Run the application
shinyApp(ui = ui, server = server)
input$variable is a character you can use .data pronoun here to use it as column value.
library(dplyr)
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
varSelectInput("variable", "Variable:",iris)
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderTable({
iris %>% dplyr::filter(.data[[input$variable]]>=5)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I am new to R Shiny. I am attempting to create an app that allows a user to subset a data.frame based on multiple variables and then see the resulting data.
Here is a small example data set:
iter,wave,apples
1,1,600
1,1,500
1,1,400
1,2,300
1,2,200
1,2,100
2,1,1000
2,1,1100
2,1,1200
2,2,1300
2,2,1400
2,2,1500
3,1,1100
3,1,2200
3,1,3300
3,2,4400
3,2,5500
3,2,6600
I would like the user to be able to specify the value of iter and of wave and see the resulting data.
Here is my attempt at the Shiny code. I realize I must be making several silly mistakes.
Edit
Here is my revised code. The end result now comes pretty close to what I want. The sidebar is still not being displayed perfectly.
library(shiny)
setwd('C:/Users/mark_/Documents/simple_RShiny_files/explore')
apple.data <- read.csv('subset_data_based_on_multiple_variables.csv',
header = TRUE, stringsAsFactors = FALSE)
ui <- fluidPage(
titlePanel("Subsetting Apple Dataset"),
sidebarLayout(
sidebarPanel(
uiOutput("codePanel")
),
mainPanel(
tableOutput("view")
)
),
selectInput("codeInput", inputId ="data1", label = "Choose Iter", choices = unique(apple.data$iter)),
selectInput("codeInput", inputId ="data2", label = "Choose Wave", choices = unique(apple.data$wave))
)
server <- function(input, output) {
output$codePanel <- renderUI({
})
dataset <- reactive({
subset(apple.data, (iter == input$data1 & wave == input$data2))
})
output$view <- renderTable(dataset())
}
shinyApp(ui = ui, server = server)
The output
The problem is that both selectInputs have the same inputId. This works:
library(shiny)
apple.data <- data.frame(
iter = c(1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,
2L,3L,3L,3L,3L,3L,3L),
wave = c(1L,1L,1L,2L,2L,2L,1L,1L,1L,2L,2L,
2L,1L,1L,1L,2L,2L,2L),
apples = c(600L,500L,400L,300L,200L,100L,1000L,
1100L,1200L,1300L,1400L,1500L,1100L,2200L,3300L,4400L,
5500L,6600L)
)
ui <- fluidPage(
titlePanel("Subsetting Apple Dataset"),
sidebarLayout(
sidebarPanel(
selectInput("codeInput1", label = "Choose Iter", choices = unique(apple.data$iter)),
selectInput("codeInput2", label = "Choose Wave", choices = unique(apple.data$wave))
),
mainPanel(
tableOutput("view")
)
)
)
server <- function(input, output) {
dataset <- reactive({
return(subset(apple.data, (iter == input$codeInput1 & wave == input$codeInput2)))
})
output$view <- renderTable(dataset())
}
shinyApp(ui = ui, server = server)
Probably a simple one:
I have a data.frame such as this:
set.seed(1)
df <- data.frame(name=c("A","A","B","C","B","A"),id=1:6,rep1=rnorm(6),rep2=rnorm(6),rep3=rnorm(6))
In the UI part of the R shiny server I'd like to have a drop-down menu that lists unique(df$name), and then given that selection, in a second drop-down menu I'd like to list all df$id that correspond to that df$name selection (i.e., if the selected name is selected.name, this will be: dplyr::filter(df,name == selected.name)$id). Then given these two selections (which are a unique row in df) I'd like to execute server, which executes this function to plot the given selection:
plotData <- function(selected.df)
{
plot.df <- reshape2::melt(dplyr::select(selected.df,-name,-id))
ggplot2::ggplot(plot.df,ggplot2::aes(x=variable,y=value))+ggplot2::geom_point()+ggplot2::theme_minimal()
}
Here's the shiny code I'm trying:
server <- function(input, output)
{
output$id <- renderUI({
selectInput("id", "ID", choices = unique(dplyr::filter(df,name == input$name)$id))
})
output$plot <- renderPlot({
plotData(selected.df=dplyr::filter(df,name == input$name,id == output$id))
})
}
ui <- fluidPage(
# App title ----
titlePanel("Results Explorer"),
# Sidebar layout with a input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# select name
selectInput("name", "Name", choices = unique(df$name)),
uiOutput("id")
),
# Main panel for displaying outputs ----
mainPanel(
plotOutput("plot")
)
)
)
When I run: shinyApp(ui = ui, server = server), I get the error:
Evaluation error: Reading objects from shinyoutput object not allowed..
What's missing?
Here the option would be have renderUI in the 'server' and uiOuput in 'ui'
-ui
library(shiny)
library(dplyr)
library(ggplot2)
ui <- fluidPage(
# App title ----
titlePanel("Results Explorer"),
# Sidebar layout with a input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# select name
selectInput("name", "Name", choices = unique(df$name)),
uiOutput("idselection")
# select id - this is where I need help
),
# Main panel for displaying outputs ----
mainPanel(
# ShinyServer part
plotOutput("plot")
)
)
)
-server
server = function(input, output) {
output$idselection <- renderUI({
selectInput("id", "ID", choices = unique(df$id[df$name ==input$name]))
})
output$plot <- renderPlot({
df %>%
count(name) %>%
ggplot(., aes(x = name, y = n, fill = name)) +
geom_bar(stat = 'identity') +
theme_bw()
})
}
shinyApp(ui = ui, server = server)
-output
Ok, tiny fix:
Create data:
set.seed(1)
df <- data.frame(name=c("A","A","B","C","B","A"),id=1:6,rep1=rnorm(6),rep2=rnorm(6),rep3=rnorm(6))
Function that server will execute:
plotData <- function(selected.df)
{
plot.df <- reshape2::melt(dplyr::select(selected.df,-name,-id))
ggplot2::ggplot(plot.df,ggplot2::aes(x=variable,y=value))+ggplot2::geom_point()+ggplot2::theme_minimal()
}
shiny code:
server <- function(input, output)
{
output$id <- renderUI({
selectInput("id", "ID", choices = unique(dplyr::filter(df,name == input$name)$id))
})
output$plot <- renderPlot({
plotData(selected.df=dplyr::filter(df,name == input$name,id == input$id))
})
}
ui <- fluidPage(
# App title ----
titlePanel("Results Explorer"),
# Sidebar layout with a input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# select name
selectInput("name", "Name", choices = unique(df$name)),
uiOutput("id")
),
# Main panel for displaying outputs ----
mainPanel(
plotOutput("plot")
)
)
)
I get the following error:
Error in $.shinyoutput(*tmp*, X) :
Reading objects from shinyoutput object not allowed
when using the scripts below. ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Dynamic user interface-RenderUI"),
sidebarLayout(
sidebarPanel(
selectInput("data", "Select the Database of your choice",
c("iris"="Iris","mtcars"="mt","trees"="tree")),
br(),
helpText("The folowing SelectInput drop down choices are dynamically polulated based on dataset selected"),
br(),
uiOutput("X-Axis"),#X-Axis is coming from renderui inserver
br(),
uiOutput("Y-Axis")#Y-Axis is coming from renderui inserver
),
mainPanel(
plotOutput("p")
)
)
))
and server.R
library(shiny)
shinyServer(function(input, output) {
var <-reactive({
switch(input$data,
"iris"=names(iris),
"mtcars"=names(mtcars),
"trees"=names(trees)
)
})
output$X-Axis <- renderUI({
selectInput("x-axis", "Select the X-Axis variable",choices = var())
})
output$Y-Axis <- renderUI({
selectInput("y-axis", "Select the Y-Axis variable",choices = var())
})
output$p <- renderPlot({
attach(get(input$data))
plot(x=get(input$x-axis),y=get(input$y-axis),xlab =input$x-axis,ylab = input$y-axis )
})
})
You are using inappropriate names. If you use names such as x-axis you will need to refer to them as input$'x-axis' or maybe easier input[["x-axis"]]. In your selectInput your names are your objects and vice versa.
# UI.r
library(shiny)
shinyUI(fluidPage(
titlePanel("Dynamic user interface-RenderUI"),
sidebarLayout(
sidebarPanel(
selectInput("data", "Select the Database of your choice",
c("Iris"="iris","mt"="mtcars","tree"="trees")),
br(),
helpText("The folowing SelectInput drop down choices are dynamically polulated based on dataset selected"),
br(),
uiOutput("X-Axis"),#X-Axis is coming from renderui inserver
br(),
uiOutput("Y-Axis")#Y-Axis is coming from renderui inserver
),
mainPanel(
plotOutput("p")
)
)
))
server.R
library(shiny)
shinyServer(function(input, output) {
var <-reactive({
switch(input$data,
"iris"=names(iris),
"mtcars"=names(mtcars),
"trees"=names(trees)
)
})
output[["X-Axis"]] <- renderUI({
selectInput("x-axis", "Select the X-Axis variable",choices = var())
})
output[["Y-Axis"]] <- renderUI({
selectInput("y-axis", "Select the Y-Axis variable",choices = var())
})
output$p <- renderPlot({
attach(get(input$data))
plot(x=get(input[["x-axis"]]),y=get(input[["y-axis"]]),xlab =input[["x-axis"]],ylab = input[["y-axis"]] )
})
})