How do I programmatically generate a data.frame from many Shiny inputs? - r

I'm trying to create a Shiny application that has ~100 user inputs, some of which may go untouched. I think that I could make a reactive dataframe within the app by writing input$user_input_1, input$user_input_2, etc. 100 times to call each input individually. Instead, I'm wondering if there is a way to programmatically generate that list of inputs using a concatenating function and then evaluate those inputs as if I had typed input$user_input_1. When I run the following code, however, I get a message telling me that Error: object 'input' not found.
My end goal is to be able to take these user inputs and use them in some behind-the-scenes transformations. If my current set up isn't the most efficient way to capture a large number of inputs, please let me know, but at the very least I'd like to understand what isn't working here.
I'd appreciate some help here. Thanks.
library(shiny)
library(tidyverse)
input_tbl <- tibble(inputs = c("x", "y", "z"))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
numericInput(
inputId = "number_x",
label = "Input X",
value = 30
),
numericInput(
inputId = "number_y",
label = "Input Y",
value = 60
),
numericInput(
inputId = "number_z",
label = "Input Z",
value = 90
)
),
mainPanel(
verbatimTextOutput("table")
)
)
)
server <- function(input, output) {
value_tbl <- eventReactive(
eventExpr = input$number_x,
valueExpr = {
tibble(
inputs = input_tbl %>% pull(),
values = input_tbl %>%
pull() %>%
modify(~ str_c("input$number_", .x)) %>%
map(~ parse(text = .x)) %>%
map(eval)
)
})
output$table <- renderPrint(value_tbl())
}
shinyApp(ui = ui, server = server)

If I got you right you could achieve your desired result with purrr::map_df in one line of code like so:
server <- function(input, output) {
value_tbl <- eventReactive(
eventExpr = input$number_x,
valueExpr = {
map_df(input_tbl$inputs, ~ data.frame(inputs = .x, values = input[[paste0("number_", .x)]]))
})
output$table <- renderPrint(value_tbl())
}

Turns out I was using the wrong evaluation function.
Instead of map(eval), I need to use map(eval.parent).

Related

R Shiny - calculation using data in csv and user numericInput

I am trying to create a Shiny app where a user can upload a csv file, input a numeric value, add the numeric value to a specific column of data in the csv, then save the raw + calculated data in a table. I generated a simplified Shiny script below using a simple csv file.
When I try and run the app, I get the error:
Problem with mutate() input sum.
[31mx[39m non-numeric argument to binary operator
[34mi[39m Input sum is A + C.
I tried looking up some examples of how to fix this, but was unable to find something that utilized eventReactive() to keep the data table updated with the new calculated data. Any help is greatly appreciated.
library(shiny)
library(dplyr)
ui <- fluidPage(
# Application title
titlePanel("Test"),
# Show a plot of the generated distribution
mainPanel(
fileInput(
inputId = "csvFile",
label = "Upload csv file",
accept = c(".csv")),
uiOutput("C"),
uiOutput("D"),
tableOutput("modifiedData")
)
)
server <- function(input, output) {
output$C <- renderUI(
{numericInput("C", "Variable C", 0)}
)
output$D <- renderUI(
{numericInput("D", "Variable D", 0)}
)
userData <- eventReactive(input$csvFile,
{
req(input$csvFile)
raw_df <- read.csv(input$csvFile$datapath)
calc_df <- raw_df %>%
mutate(sum = A + C)
})
output$modifiedData <- renderTable({userData()})
}
shinyApp(ui = ui, server = server)
You should be using the reactive variable input$C. Also, you should make the eventReactive dependent on input$C. Try this
ui <- fluidPage(
# Application title
titlePanel("Test"),
# Show a plot of the generated distribution
mainPanel(
fileInput(
inputId = "csvFile",
label = "Upload csv file",
accept = c(".csv")),
uiOutput("C"),
uiOutput("D"),
tableOutput("modifiedData")
)
)
server <- function(input, output) {
output$C <- renderUI(
{numericInput("C", "Variable C", 0)}
)
output$D <- renderUI(
{numericInput("D", "Variable D", 0)}
)
userData <- eventReactive(list(input$csvFile, input$C),
{
req(input$csvFile)
raw_df <- read.csv(input$csvFile$datapath)
calc_df <- raw_df %>%
mutate(sum = A + input$C)
})
output$modifiedData <- renderTable({userData()})
}
shinyApp(ui = ui, server = server)

RShiny: Refer to data from UI in server

Example Case: I have a function in my global.R called get_data which returns a list of many items. The reason I don't just put the data in global is so the data can automatically refresh after a certain amount of time
ui.R
my_data <- uiOutput("data") # Doesn't work
### Some more generic manipulation before final use
# The output of my_data will look like the following below.
my_data <- list()
my_data$first_entry <- c("a", "b", "d")
my_data$second_entry <- c("x", "y", "z") # and so on
shinyUI(navbarPage(theme=shinytheme("flatly"),
'App Name',
tabPanel('Title',
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
width=3,
# new box
checkboxGroupButtons(
'name',
'label:',
choices = sort(my_data$first_value),
status = 'primary',
selected = sort(my_data$first_value)[1],
size = 'xs'
# inline = TRUE
))
server.R
shinyServer(function(input, output, session) {
data <- reactive({
invalidateLater(100000,session)
get_data()
})
output$data <- renderUI({
data()
})
})
Two questions:
Is there any way of referencing my_data correctly?
If my function get_data is simply reading a (large) csv which is updated systematically. Is there a better way of doing it than I am currently doing it?
I think you're wondering how to define possible choices= for something within the UI element, when the data is both (1) undefined at the start, and (2) changing periodically. The answer to that is to define it "empty" and update it as the new data is found.
library(shiny)
library(shinyWidgets)
get_data <- function() as.list(mtcars[sample(nrow(mtcars), size=3), sample(ncol(mtcars), size=3)])
logg <- function(...) message(paste0("[", format(Sys.time()), "] ", ...))
shinyApp(
ui = fluidPage(
title = "Hello",
checkboxGroupButtons(inputId = "cb", label = "label:", choices = c("unk"), selected = NULL,
status = "primary", size = "xs"),
br(),
textOutput("txt"),
br(),
textAreaInput("txtarea", NULL, rows = 4)
),
server = function(input, output, session) {
data <- reactive({
logg("in 'data'")
invalidateLater(3000, session)
get_data()
})
observe({
logg("in 'observe'")
req(length(data()) > 0)
updateCheckboxGroupButtons(session = session, inputId = "cb", choices = names(data()))
updateTextAreaInput(session, "txtarea", value = paste(capture.output(str(data())), collapse = "\n"))
})
output$txt <- renderPrint({
logg("in 'txt'")
req(length(data()) > 0)
str(data())
})
}
)
Notice that the definition of checkboxGroupButtons starts with no real choices. I'd prefer to start it empty, but unlike selectInput and similar functions, it does not like starting with an empty vector. It is quickly (nearly-immediately) changed, so I do not see "unk" in the interface.
I demoed two options for "displaying" the data in its raw form: as an output "txt", and as an updatable input "txtarea". I like the latter because it deals well with fixed-width, but it requires an update* function (which is really not a big deal).

Use Shiny to choose column, equality, and value to filter by conditions

I'm creating a Shiny app where I'd like the user to be able to select a column and condition, resulting in the input$COLUMN input$CONDITION input$VALUE which can be used to filter a dataframe.
Desired Output
iris %>% filter(input$COLUMN input$CONDITION input$VALUE) == iris %>% filter(Sepal.Length > 4.7)
For this to work I need to use rlang for the input$COLUMN, I need to eval the input$CONDITION and I need the input$VALUE to be converted to a numeric when appropriate. (I'm attempting this in my verbatimTextOutput)
What is the best approach for achieving this? I thought making the whole expression a string to be parsed within a tidy pipeline may be the way to go but I am open to alternate suggestions!!
library(shiny)
library(tidyverse)
ui <- fluidPage(
# Sidebar with an input for column
# boolean input
# and value input
sidebarLayout(
sidebarPanel(
fluidRow(column(4, selectInput("COLUMN", "Filter By:", choices = colnames(iris))),
column(4, selectInput("CONDITION", "Boolean", choices = c("==", "!=", ">", "<"))),
column(4, uiOutput("COL_VALUE")))
),
# Show text generated by sidebar
# use text in tidy pipeline to create subsetted dataframe
mainPanel(
verbatimTextOutput("as_text"),
tableOutput("the_data")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$COL_VALUE <- renderUI({
x <- iris %>% select(!!sym(input$COLUMN))
selectInput("VALUE", "Value", choices = x)
})
filtering_string <- reactive ({
paste0("!!sym(", input$COLUMN, ") ", input$CONDITION, " ", input$VALUE)
})
output$as_text <- renderText({
filtering_string()
})
output$the_data <- renderTable({
iris %>%
eval(parse(text = filtering_string()))
})
}
# Run the application
shinyApp(ui = ui, server = server)
I am not too familiar with !!sym but you could do:
output$the_data <- renderTable({
# To hide error when no value is selected
if (input$VALUE == "") {
my_data <- ""
} else {
my_data <- iris %>%
filter(eval(parse(text = paste0(input$COLUMN, input$CONDITION, input$VALUE))))
}
return(my_data)
})

Using tapply in Shiny to find mean of a column

I am running into trouble using the tapply function. I am pulling two vectors from the same data frame which was created from a reactive variable. The first I am calling from a user inputted selection, and the second is one that I have created to keep my code generalisable and to use in my sort function. My sample code is shown below using the r-bloggers example. The data is here.
https://redirect.viglink.com/?format=go&jsonp=vglnk_150821851345614&key=949efb41171ac6ec1bf7f206d57e90b8&libId=j8v6cnh201021u9s000DAhzunvtas&loc=https%3A%2F%2Fwww.r-bloggers.com%2Fbuilding-shiny-apps-an-interactive-tutorial%2F&v=1&out=http%3A%2F%2Fdeanattali.com%2Ffiles%2Fbcl-data.csv&ref=https%3A%2F%2Fduckduckgo.com%2F&title=Building%20Shiny%20apps%20%E2%80%93%20an%20interactive%20tutorial%20%7C%20R-bloggers&txt=here
The error it throws is that they are not the same length, even though their attribute and class print outs are exactly the same.
I know that this is not the best code in the world, but I just threw together a quick example.
library(shiny)
library(tidyverse)
bcl <- read.csv("bcl-data.csv", stringsAsFactors = FALSE)
ui <- fluidPage(titlePanel("Sampling Strategies"),
sidebarLayout(
sidebarPanel(
selectInput("XDATA","xdata",
choices = c(names(bcl))),
selectInput("YDATA","ydata",
choices = c(names(bcl)))
),
mainPanel(
tabsetPanel(
tabPanel("The table",tableOutput("mytable"))
))
))
server <- function(input, output, session) {
filtered <- reactive({
bcl <- bcl %>% mutate(ID = 1:nrow(bcl))
})
output$mytable <- renderTable({
dataset <- filtered() %>% mutate(sampled = "white")
sample.rows <- sample(dataset$ID, 5, replace = FALSE)
dataset$sampled[sample.rows] <- "black"
final <- tapply(dataset[input$XDATA], list(dataset$sampled),mean)[["black"]]
return(final)
})
}
shinyApp(ui = ui, server = server)
Cheers
Edit* Sorry my bad, forgot to change over the drop list codes. All I am interested is one generic xdata vector that can be selected from the loaded data set. I then sample it, and want to find the mean value from the sampled indices.
One of the problems is in the subsetting. the [ still returns a data.frame. So, we need [[. If we look at ?tapply
tapply(X, INDEX, FUN = NULL, ..., default = NA, simplify = TRUE)
where
X is an atomic object, typically a vector
ui <- fluidPage(titlePanel("Sampling Strategies"),
sidebarLayout(
sidebarPanel(
selectInput("XDATA","xdata",
choices = c(names(bcl)[5:7])),
selectInput("YDATA","ydata",
choices = c(names(bcl)))
),
mainPanel(
tabsetPanel(
tabPanel("The table",tableOutput("mytable"))
))
))
server <- function(input, output, session) {
filtered <- reactive({
bcl <- bcl %>% mutate(ID = row_number())
})
output$mytable <- renderTable({
dataset <- filtered() %>% mutate(sampled = "white")
sample.rows <- sample(dataset$ID, 20, replace = FALSE)
dataset$sampled[sample.rows] <- "black"
final <- tapply(dataset[[input$XDATA]], list(dataset$sampled),mean, na.rm = TRUE, simplify = TRUE)
return(final)
})
}
shinyApp(ui = ui, server = server)
-output

Dynamic ggvis object in Shiny

I'm trying to add a dynamic ggvis plot to a Shiny app. First, user picks a dimension, and then adds items from that dimension.
For global.R and sample data, see https://gist.github.com/tts/a41c8581b9d77f131b31
server.R:
shinyServer(function(input, output, session) {
# Render a selectize drop-down selection box
output$items <- renderUI({
selectizeInput(
inputId = 'items',
label = 'Select max 4. Click to delete',
multiple = TRUE,
choices = aalto_all[ ,names(aalto_all) %in% input$dim],
options = list(maxItems = 4, placeholder = 'Start typing')
)
})
selected <- reactive({
if (is.null(input$items)) {
return(aalto_all)
}
df <- aalto_all[aalto_all[[input$dim]] %in% input$items, ]
df$keys <-seq(1, nrow(df))
df
})
selected %>%
ggvis(~WoS, ~NrOfAuthors, fill = ~School, key := ~keys) %>%
layer_points() %>%
add_tooltip(show_title) %>%
bind_shiny("gv")
show_title <- function(x=NULL) {
if(is.null(x)) return(NULL)
key <- x["keys"][[1]]
selected()$Title20[key]
}
})
ui.R:
shinyUI(fluidPage(
titlePanel('Some (alt)metric data for articles published since 2010'),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "dim",
label = "Dimension",
choices = dimensions,
selected = c("Title")),
uiOutput("items")
),
mainPanel(
tabsetPanel(
# I'll add more tabs
tabPanel("Plot with ggvis", ggvisOutput("gv"))
)
)
)
))
This is OK
in the beginning, when there are no items selected, and all data is plotted. This is a hack because the ggvis object throws an error if there is no data served.
when all selected items are deleted (which is the same as 1.) and another dimension is chosen
But when I try to switch to another dimension without deleting the items first, I get this:
Error in `$<-.data.frame`(`*tmp*`, "keys", value = c(1L, 0L)) :
replacement has 2 rows, data has 0
I understand that ggvis is very new and constantly developing, but I suspect that there is merely something in Shiny reactive values that is out of sync. If anyone could point out what I'm doing wrong, thanks a lot!
The error is caused because you have a data.frame with zero rows and have a resulting 1:0.
You can change your selected function to:
selected <- reactive({
if (is.null(input$items)) {
return(aalto_all)
}
df <- aalto_all[aalto_all[[input$dim]] %in% input$items, ]
df$keys <-seq_along(df[,1])
if(nrow(df) == 0){
return(aalto_all)
}
df
})

Resources